blob: 7e7689a61e078d605340eb1b64ad7754e2e7f72e [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
ziga-lunargb65dbfb2022-03-19 18:45:09 +01001408 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
1409 if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
1410 IsImageLayoutReadOnly(pImageMemoryBarriers[i].newLayout)) {
1411 skip |= LogWarning(device, kVUID_BestPractices_TransitionUndefinedToReadOnly,
1412 "VkImageMemoryBarrier is being submitted with oldLayout VK_IMAGE_LAYOUT_UNDEFINED and the contents "
1413 "may be discarded, but the newLayout is %s, which is read only.",
1414 string_VkImageLayout(pImageMemoryBarriers[i].newLayout));
1415 }
1416 }
1417
Nadav Gevaf0808442021-05-21 13:51:25 -04001418 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001419 auto num = num_barriers_objects_.load();
1420 if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001421 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001422 "%s Performance warning: In this frame, %" PRIu32
1423 " barriers were already submitted. Barriers have a high cost and can "
1424 "stall the GPU. "
1425 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1426 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04001427 }
1428
1429 std::array<VkImageLayout, 3> read_layouts = {
1430 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1431 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1432 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1433 };
1434
1435 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1436 // read to read barriers
1437 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1438 bool old_is_read_layout = found != read_layouts.end();
1439 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1440 bool new_is_read_layout = found != read_layouts.end();
1441 if (old_is_read_layout && new_is_read_layout) {
1442 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1443 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1444 "time you use it.",
1445 VendorSpecificTag(kBPVendorAMD));
1446 }
1447
1448 // general with no storage
1449 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1450 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1451 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1452 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1453 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1454 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1455 VendorSpecificTag(kBPVendorAMD));
1456 }
1457 }
1458 }
1459 }
1460
Camden5b184be2019-08-13 07:50:19 -06001461 return skip;
1462}
1463
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001464bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1465 const VkDependencyInfoKHR* pDependencyInfo) const {
1466 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1467}
1468
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001469bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1470 const VkDependencyInfo* pDependencyInfo) const {
1471 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1472}
1473
Camden5b184be2019-08-13 07:50:19 -06001474bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001475 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001476 bool skip = false;
1477
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001478 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1479
1480 return skip;
1481}
1482
1483bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1484 VkQueryPool queryPool, uint32_t query) const {
1485 bool skip = false;
1486
1487 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001488
1489 return skip;
1490}
1491
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001492bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1493 VkQueryPool queryPool, uint32_t query) const {
1494 bool skip = false;
1495
1496 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1497
1498 return skip;
1499}
1500
Sam Walls0961ec02020-03-31 16:39:15 +01001501void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1502 VkPipeline pipeline) {
1503 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1504
Nadav Gevaf0808442021-05-21 13:51:25 -04001505 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001506 PipelineUsedInFrame(pipeline);
Nadav Gevaf0808442021-05-21 13:51:25 -04001507
Sam Walls0961ec02020-03-31 16:39:15 +01001508 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001509 auto pipeline_state = Get<bp_state::Pipeline>(pipeline);
Sam Walls0961ec02020-03-31 16:39:15 +01001510 // check for depth/blend state tracking
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001511 if (pipeline_state) {
1512 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001513 assert(cb_node);
1514 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001515
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001516 render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments;
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001517 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001518
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001519 const auto* blend_state = pipeline_state->create_info.graphics.pColorBlendState;
1520 const auto* stencil_state = pipeline_state->create_info.graphics.pDepthStencilState;
Sam Walls0961ec02020-03-31 16:39:15 +01001521
1522 if (blend_state) {
1523 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001524 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001525 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1526 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001527 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001528 }
1529 }
1530 }
1531
1532 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001533 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001534
1535 if (stencil_state && stencil_state->depthTestEnable) {
1536 switch (stencil_state->depthCompareOp) {
1537 case VK_COMPARE_OP_EQUAL:
1538 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1539 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001540 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001541 break;
1542 default:
1543 break;
1544 }
1545 }
Sam Walls0961ec02020-03-31 16:39:15 +01001546 }
1547 }
1548}
1549
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001550static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1551 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1552 const auto& subpass_info = createInfo.pSubpasses[subpass];
1553 if (subpass_info.pResolveAttachments) {
1554 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1555 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1556 }
1557 }
1558 }
1559
1560 return false;
1561}
1562
Attilio Provenzano02859b22020-02-27 14:17:28 +00001563static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1564 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001565 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001566
1567 // If an attachment is ever used as a color attachment,
1568 // resolve attachment or depth stencil attachment,
1569 // it needs to exist on tile at some point.
1570
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001571 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1572 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001573 }
1574
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001575 if (subpass_info.pResolveAttachments) {
1576 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1577 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1578 }
1579 }
1580
1581 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001582 }
1583
1584 return false;
1585}
1586
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001587static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1588 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1589 return false;
1590 }
1591
1592 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001593 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001594
1595 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1596 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1597 return true;
1598 }
1599 }
1600 }
1601
1602 return false;
1603}
1604
Attilio Provenzano02859b22020-02-27 14:17:28 +00001605bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1606 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1607 bool skip = false;
1608
1609 if (!pRenderPassBegin) {
1610 return skip;
1611 }
1612
Gareth Webbdc6549a2021-06-16 03:52:24 +01001613 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1614 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1615 "This render pass has a zero-size render area. It cannot write to any attachments, "
1616 "and can only be used for side effects such as layout transitions.");
1617 }
1618
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001619 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001620 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001621 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001622 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001623 if (rpabi) {
1624 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1625 }
1626 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001627 // Check if any attachments have LOAD operation on them
1628 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001629 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001630
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001631 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001632 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001633 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001634 }
1635
1636 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001637 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001638 }
1639
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001640 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001641
1642 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001643 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1644 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001645 }
1646
1647 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001648 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1649 skip |= LogPerformanceWarning(
1650 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1651 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1652 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001653 "which will copy in total %u pixels (renderArea = "
1654 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001655 VendorSpecificTag(kBPVendorArm), att,
1656 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1657 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1658 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001659 }
1660 }
1661 }
1662
1663 return skip;
1664}
1665
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001666void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1667 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001668 if (view) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001669 auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state);
1670 QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001671 }
1672}
1673
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001674void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1675 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001676 // If we're viewing a 3D slice, ignore base array layer.
1677 // The entire 3D subresource is accessed as one atomic unit.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001678 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 +02001679
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001680 const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001681 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001682 const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel;
1683 const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001684
1685 for (uint32_t layer = 0; layer < array_layers; layer++) {
1686 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001687 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1688 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001689 }
1690 }
1691}
1692
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001693void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1694 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) {
1695 const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001696 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001697
1698 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001699 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001700 }
1701}
1702
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001703void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001704 std::shared_ptr<bp_state::Image> &state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001705 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001706 if (VendorCheckEnabled(kBPVendorArm)) {
1707 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](
1708 const ValidationStateTracker&, const QUEUE_STATE&, const CMD_BUFFER_STATE&) -> bool {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001709 ValidateImageInQueue(function_name, *state, usage, array_layer, mip_level);
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001710 return false;
1711 });
1712 }
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001713}
1714
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001715void BestPractices::ValidateImageInQueueArm(const char* function_name, const bp_state::Image& image,
1716 IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001717 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001718 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001719 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 -07001720 !image.IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001721 LogPerformanceWarning(
1722 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001723 "%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 +02001724 "image was used, it was written to with STORE_OP_STORE. "
1725 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1726 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001727 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001728 } 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 +02001729 LogPerformanceWarning(
1730 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001731 "%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 +02001732 "image was used, it was written to with vkCmdClear*Image(). "
1733 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1734 "tile-based architectures."
1735 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001736 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001737 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001738 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1739 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001740 const char *last_cmd = nullptr;
1741 const char *vuid = nullptr;
1742 const char *suggestion = nullptr;
1743
1744 switch (last_usage) {
1745 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1746 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1747 last_cmd = "vkCmdBlitImage";
1748 suggestion =
1749 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1750 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1751 "which avoids the memory roundtrip.";
1752 break;
1753 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1754 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1755 last_cmd = "vkCmdClear*Image";
1756 suggestion =
1757 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1758 "tile-based architectures. "
1759 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1760 break;
1761 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1762 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1763 last_cmd = "vkCmdCopy*Image";
1764 suggestion =
1765 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1766 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1767 "which avoids the memory roundtrip.";
1768 break;
1769 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1770 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1771 last_cmd = "vkCmdResolveImage";
1772 suggestion =
1773 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1774 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1775 "which avoids the memory roundtrip.";
1776 break;
1777 default:
1778 break;
1779 }
1780
1781 LogPerformanceWarning(
1782 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001783 "%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 +01001784 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001785 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001786 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001787}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001788
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001789void BestPractices::ValidateImageInQueue(const char* function_name, bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage,
1790 uint32_t array_layer, uint32_t mip_level) {
1791 auto last_usage = state.UpdateUsage(array_layer, mip_level, usage);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001792 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001793 ValidateImageInQueueArm(function_name, state, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001794 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001795}
1796
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001797void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) {
1798 cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(),
1799 cb.queue_submit_functions_after_render_pass.end());
1800 cb.queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001801}
1802
1803void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1804 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001805 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1806 if (cb_node) {
1807 AddDeferredQueueOperations(*cb_node);
1808 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001809}
1810
1811void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1812 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001813 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1814 if (cb_node) {
1815 AddDeferredQueueOperations(*cb_node);
1816 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001817}
1818
1819void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1820 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001821 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1822 if (cb_node) {
1823 AddDeferredQueueOperations(*cb_node);
1824 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001825}
1826
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001827void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1828 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001829 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001830 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001831 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1832}
1833
1834void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1835 const VkRenderPassBeginInfo* pRenderPassBegin,
1836 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1837 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1838 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1839}
1840
1841void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1842 const VkRenderPassBeginInfo* pRenderPassBegin,
1843 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1844 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1845 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1846}
1847
1848void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001849
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001850 if (!pRenderPassBegin) {
1851 return;
1852 }
1853
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001854 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001855
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001856 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001857 if (rp_state) {
1858 // Check load ops
1859 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001860 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001861
1862 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1863 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1864 continue;
1865 }
1866
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001867 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001868
1869 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1870 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001871 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001872 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1873 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001874 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001875 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001876 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001877 }
1878
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001879 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001880 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001881
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001882 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001883 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1884 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001885 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001886 }
1887 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001888 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001889 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001890
Jeremy Gebben9f537102021-10-05 16:37:12 -06001891 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001892 }
1893
1894 // Check store ops
1895 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001896 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001897
1898 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1899 continue;
1900 }
1901
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001902 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001903
1904 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1905 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001906 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001907 }
1908
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001909 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001910
Jeremy Gebben9f537102021-10-05 16:37:12 -06001911 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001912 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001913 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1914 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001915 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001916 }
1917 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001918 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001919 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001920
Jeremy Gebben9f537102021-10-05 16:37:12 -06001921 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001922 }
1923 }
1924}
1925
Attilio Provenzano02859b22020-02-27 14:17:28 +00001926bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1927 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001928 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1929 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001930 return skip;
1931}
1932
1933bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1934 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001935 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001936 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1937 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001938 return skip;
1939}
1940
1941bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001942 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001943 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1944 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001945 return skip;
1946}
1947
Sam Walls0961ec02020-03-31 16:39:15 +01001948void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1949 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001950 // Reset the renderpass state
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001951 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001952 cb->hasDrawCmd = false;
1953 assert(cb);
1954 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02001955 render_pass_state.touchesAttachments.clear();
1956 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001957 render_pass_state.numDrawCallsDepthOnly = 0;
1958 render_pass_state.numDrawCallsDepthEqualCompare = 0;
1959 render_pass_state.colorAttachment = false;
1960 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001961 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001962 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01001963
Jeremy Gebbenf4449392022-01-28 10:09:10 -07001964 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01001965
1966 // track depth / color attachment usage within the renderpass
1967 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
1968 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001969 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001970
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001971 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001972 }
1973}
1974
1975void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1976 VkSubpassContents contents) {
1977 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1978 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
1979}
1980
1981void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1982 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1983 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1984 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1985}
1986
1987void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1988 const VkRenderPassBeginInfo* pRenderPassBegin,
1989 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1990 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1991 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1992}
1993
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001994// Generic function to handle validation for all CmdDraw* type functions
1995bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
1996 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001997 const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001998 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001999 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2000 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002001 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002002
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002003 // Verify vertex binding
2004 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
2005 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002006 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002007 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002008 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2009 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002010 }
2011 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002012
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002013 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002014 if (pipe) {
2015 const auto* rp_state = pipe->rp_state.get();
2016 if (rp_state) {
2017 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2018 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002019 const auto& create_info = pipe->create_info.graphics;
2020 const uint32_t depth_stencil_attachment =
2021 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2022 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2023 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002024 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2025 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2026 }
2027 }
2028 }
2029 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002030 }
2031 return skip;
2032}
2033
Sam Walls0961ec02020-03-31 16:39:15 +01002034void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002035 auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002036 assert(cb_node);
Sam Walls0961ec02020-03-31 16:39:15 +01002037 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002038 RecordCmdDrawTypeArm(*cb_node, draw_count, caller);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002039 }
2040
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002041 if (cb_node->render_pass_state.drawTouchAttachments) {
2042 for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) {
2043 RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002044 }
2045 // No need to touch the same attachments over and over.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002046 cb_node->render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002047 }
2048}
2049
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002050void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) {
2051 auto& render_pass_state = cb_node.render_pass_state;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002052 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2053 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2054 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002055 }
2056}
2057
Camden5b184be2019-08-13 07:50:19 -06002058bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002059 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002060 bool skip = false;
2061
2062 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002063 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2064 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002065 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002066 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002067
2068 return skip;
2069}
2070
Sam Walls0961ec02020-03-31 16:39:15 +01002071void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2072 uint32_t firstVertex, uint32_t firstInstance) {
2073 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2074 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2075}
2076
Camden5b184be2019-08-13 07:50:19 -06002077bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002078 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002079 bool skip = false;
2080
2081 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002082 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2083 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002084 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002085 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2086
Attilio Provenzano02859b22020-02-27 14:17:28 +00002087 // Check if we reached the limit for small indexed draw calls.
2088 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002089 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002090 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002091 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2092 VendorCheckEnabled(kBPVendorArm)) {
2093 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002094 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002095 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2096 "You can try batching drawcalls or instancing when applicable.",
2097 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2098 }
2099
Sam Walls8e77e4f2020-03-16 20:47:40 +00002100 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002101 ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002102 }
2103
2104 return skip;
2105}
2106
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002107bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount,
Sam Walls8e77e4f2020-03-16 20:47:40 +00002108 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2109 bool skip = false;
2110
2111 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Sam Walls8e77e4f2020-03-16 20:47:40 +00002112
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002113 const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get();
2114 if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002115
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002116 const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002117 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002118 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2119 const void* ib_mem = ib_mem_state.p_driver_data;
2120 bool primitive_restart_enable = false;
2121
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002122 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002123 const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002124 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002125
Jeremy Gebben11af9792021-08-20 10:20:09 -06002126 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2127 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002128 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002129
2130 // 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 -06002131 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002132 uint32_t scan_stride;
2133 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2134 scan_stride = sizeof(uint8_t);
2135 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2136 scan_stride = sizeof(uint16_t);
2137 } else {
2138 scan_stride = sizeof(uint32_t);
2139 }
2140
2141 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2142 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2143
2144 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2145 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2146 // irrespective of whether or not they're part of the draw call.
2147
2148 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2149 uint32_t min_index = ~0u;
2150 // start with maximum as 0 and adjust to indices in the buffer
2151 uint32_t max_index = 0u;
2152
2153 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2154 // for the given index buffer
2155 uint32_t vertex_shade_count = 0;
2156
2157 PostTransformLRUCacheModel post_transform_cache;
2158
2159 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2160 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2161 // target architecture.
2162 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2163 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2164 post_transform_cache.resize(32);
2165
2166 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2167 uint32_t scan_index;
2168 uint32_t primitive_restart_value;
2169 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2170 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2171 primitive_restart_value = 0xFF;
2172 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2173 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2174 primitive_restart_value = 0xFFFF;
2175 } else {
2176 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2177 primitive_restart_value = 0xFFFFFFFF;
2178 }
2179
2180 max_index = std::max(max_index, scan_index);
2181 min_index = std::min(min_index, scan_index);
2182
2183 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2184 bool in_cache = post_transform_cache.query_cache(scan_index);
2185 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2186 if (!in_cache) vertex_shade_count++;
2187 }
2188 }
2189
2190 // 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 +01002191 // 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
2192 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002193
2194 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002195 skip |=
2196 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2197 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2198 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2199 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2200 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2201 VendorSpecificTag(kBPVendorArm),
2202 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002203 return skip;
2204 }
2205
2206 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2207 // 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 +01002208 const size_t refs_per_bucket = 64;
2209 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2210
2211 const uint32_t n_indices = max_index - min_index + 1;
2212 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2213 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2214
2215 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2216 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002217
2218 // To avoid using too much memory, we run over the indices again.
2219 // Knowing the size from the last scan allows us to record index usage with bitsets
2220 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2221 uint32_t scan_index;
2222 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2223 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2224 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2225 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2226 } else {
2227 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2228 }
2229 // keep track of the set of all indices used to reference vertices in the draw call
2230 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002231 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2232 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002233 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2234 }
2235
2236 uint32_t vertex_reference_count = 0;
2237 for (const auto& bitset : vertex_reference_buckets) {
2238 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2239 }
2240
2241 // 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 -07002242 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002243 // 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 -07002244 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002245
2246 if (utilization < 0.5f) {
2247 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2248 "%s The indices which were specified for the draw call only utilise approximately "
2249 "%.02f%% of the bound vertex buffer.",
2250 VendorSpecificTag(kBPVendorArm), utilization);
2251 }
2252
2253 if (cache_hit_rate <= 0.5f) {
2254 skip |=
2255 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2256 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2257 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2258 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2259 "recently shaded vertices.",
2260 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2261 }
2262 }
2263
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002264 return skip;
2265}
2266
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002267bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2268 const VkCommandBuffer* pCommandBuffers) const {
2269 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002270 const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002271 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002272 const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002273 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002274 continue;
2275 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002276 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002277 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002278 if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) {
2279 skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002280 }
2281 }
2282 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002283
2284 if (VendorCheckEnabled(kBPVendorAMD)) {
2285 if (commandBufferCount > 0) {
2286 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2287 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2288 VendorSpecificTag(kBPVendorAMD));
2289 }
2290 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002291 return skip;
2292}
2293
2294void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2295 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002296 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2297
2298 auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2299 if (!primary) {
2300 return;
2301 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002302
2303 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002304 auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]);
2305 if (!secondary) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002306 continue;
2307 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002308
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002309 for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) {
2310 if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2311 RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment,
2312 early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002313 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002314 RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002315 }
2316 }
2317
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002318 for (auto& touch : secondary->render_pass_state.touchesAttachments) {
2319 RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002320 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002321
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002322 primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare;
2323 primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002324
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002325 if (secondary->hasDrawCmd) {
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002326 primary->hasDrawCmd = true;
2327 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002328 }
2329
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002330}
2331
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002332void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2333 auto& state = cb_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002334 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002335 auto itr =
2336 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2337 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002338
2339 if (itr != state.touchesAttachments.end()) {
2340 itr->aspects |= aspects;
2341 } else {
2342 state.touchesAttachments.push_back({ fb_attachment, aspects });
2343 }
2344}
2345
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002346void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002347 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2348 const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002349 auto& state = cmd_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002350 // If we observe a full clear before any other access to a frame buffer attachment,
2351 // we have candidate for redundant clear attachments.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002352 auto itr =
2353 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2354 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002355
2356 uint32_t new_aspects = aspects;
2357 if (itr != state.touchesAttachments.end()) {
2358 new_aspects = aspects & ~itr->aspects;
2359 itr->aspects |= aspects;
2360 } else {
2361 state.touchesAttachments.push_back({ fb_attachment, aspects });
2362 }
2363
2364 if (new_aspects == 0) {
2365 return;
2366 }
2367
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002368 if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002369 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2370 // CmdExecuteCommands.
2371 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2372 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2373 }
2374}
2375
2376void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2377 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2378 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002379 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects);
2380
2381 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2382 auto* rp_state = cmd_state->activeRenderPass.get();
2383 auto* fb_state = cmd_state->activeFramebuffer.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002384 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2385
2386 if (rectCount == 0 || !rp_state) {
2387 return;
2388 }
2389
2390 if (!is_secondary && !fb_state) {
2391 return;
2392 }
2393
2394 // 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 -07002395 bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002396
ziga-lunarg885c6542022-03-07 01:08:25 +01002397 if (!rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) {
2398 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2399 for (uint32_t i = 0; i < attachmentCount; i++) {
2400 auto& attachment = pClearAttachments[i];
2401 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2402 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002403
ziga-lunarg885c6542022-03-07 01:08:25 +01002404 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2405 if (subpass.pDepthStencilAttachment) {
2406 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2407 }
2408 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2409 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002410 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002411
ziga-lunarg885c6542022-03-07 01:08:25 +01002412 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2413 if (full_clear) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002414 RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment,
ziga-lunarg885c6542022-03-07 01:08:25 +01002415 aspects, rectCount, pRects);
2416 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002417 RecordAttachmentAccess(*cmd_state, fb_attachment, aspects);
ziga-lunarg885c6542022-03-07 01:08:25 +01002418 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002419 }
2420 }
2421 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002422}
2423
Attilio Provenzano02859b22020-02-27 14:17:28 +00002424void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2425 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2426 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2427 firstInstance);
2428
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002429 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002430 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2431 cmd_state->small_indexed_draw_call_count++;
2432 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002433
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002434 ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002435}
2436
Sam Walls0961ec02020-03-31 16:39:15 +01002437void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2438 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2439 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2440 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2441}
2442
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002443bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2444 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2445 uint32_t maxDrawCount, uint32_t stride) const {
2446 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2447
2448 return skip;
2449}
2450
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002451bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2452 VkDeviceSize offset, VkBuffer countBuffer,
2453 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2454 uint32_t stride) const {
2455 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002456
2457 return skip;
2458}
2459
2460bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002461 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002462 bool skip = false;
2463
2464 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002465 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2466 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002467 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002468 }
2469
2470 return skip;
2471}
2472
Sam Walls0961ec02020-03-31 16:39:15 +01002473void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2474 uint32_t count, uint32_t stride) {
2475 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2476 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2477}
2478
Camden5b184be2019-08-13 07:50:19 -06002479bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002480 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002481 bool skip = false;
2482
2483 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002484 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2485 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002486 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002487 }
2488
2489 return skip;
2490}
2491
Sam Walls0961ec02020-03-31 16:39:15 +01002492void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2493 uint32_t count, uint32_t stride) {
2494 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2495 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2496}
2497
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002498void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) {
2499 for (auto descriptor_set : cb_state.validated_descriptor_sets) {
2500 const auto& layout = *descriptor_set->GetLayout();
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002501
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002502 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2503 // For bindless scenarios, we should not attempt to track descriptor set state.
2504 // It is highly uncertain which resources are actually bound.
2505 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2506 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2507 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2508 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2509 continue;
2510 }
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002511
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002512 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
2513 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
2514 VkImageView image_view{VK_NULL_HANDLE};
2515
2516 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2517 switch (descriptor->GetClass()) {
2518 case cvdescriptorset::DescriptorClass::Image: {
2519 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2520 image_view = image_descriptor->GetImageView();
2521 }
2522 break;
2523 }
2524 case cvdescriptorset::DescriptorClass::ImageSampler: {
2525 if (const auto image_sampler_descriptor =
2526 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2527 image_view = image_sampler_descriptor->GetImageView();
2528 }
2529 break;
2530 }
2531 default:
2532 break;
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002533 }
2534
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002535 if (image_view) {
2536 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
2537 QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(),
2538 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002539 }
2540 }
2541 }
2542 }
2543}
2544
2545void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2546 uint32_t firstVertex, uint32_t firstInstance) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002547 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2548 ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002549}
2550
2551void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2552 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002553 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2554 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002555}
2556
2557void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2558 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002559 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2560 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002561}
2562
Camden5b184be2019-08-13 07:50:19 -06002563bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002564 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002565 bool skip = false;
2566
2567 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002568 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2569 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2570 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2571 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002572 }
2573
2574 return skip;
2575}
Camden83a9c372019-08-14 11:41:38 -06002576
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002577bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2578 bool skip = false;
2579 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2580 skip |= ValidateCmdEndRenderPass(commandBuffer);
2581 return skip;
2582}
2583
2584bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2585 bool skip = false;
2586 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2587 skip |= ValidateCmdEndRenderPass(commandBuffer);
2588 return skip;
2589}
2590
Sam Walls0961ec02020-03-31 16:39:15 +01002591bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2592 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002593 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002594 skip |= ValidateCmdEndRenderPass(commandBuffer);
2595 return skip;
2596}
2597
2598bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2599 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002600 const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002601
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002602 if (cmd == nullptr) return skip;
2603 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002604
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002605 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2606 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2607 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002608 if (uses_depth) {
2609 skip |= LogPerformanceWarning(
2610 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2611 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2612 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2613 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2614 VendorSpecificTag(kBPVendorArm));
2615 }
2616
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002617 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2618
2619 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2620
2621 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2622 // it is redundant to have it be part of the render pass.
2623 // Only consider it redundant if it will actually consume bandwidth, i.e.
2624 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2625 // as is using pure input attachments.
2626 // CLEAR -> STORE might be considered a "useful" thing to do, but
2627 // the optimal thing to do is to defer the clear until you're actually
2628 // going to render to the image.
2629
2630 uint32_t num_attachments = rp->createInfo.attachmentCount;
2631 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002632 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2633 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002634 continue;
2635 }
2636
2637 auto& attachment = rp->createInfo.pAttachments[i];
2638
2639 VkImageAspectFlags bandwidth_aspects = 0;
2640
2641 if (!FormatIsStencilOnly(attachment.format) &&
2642 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2643 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2644 if (FormatHasDepth(attachment.format)) {
2645 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2646 } else {
2647 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2648 }
2649 }
2650
2651 if (FormatHasStencil(attachment.format) &&
2652 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2653 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2654 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2655 }
2656
2657 if (!bandwidth_aspects) {
2658 continue;
2659 }
2660
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002661 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002662 [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002663 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002664 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002665 untouched_aspects &= ~itr->aspects;
2666 }
2667
2668 if (untouched_aspects) {
2669 skip |= LogPerformanceWarning(
2670 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2671 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2672 "was never accessed by a pipeline or clear command. "
2673 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2674 "if the attachments are not intended to be accessed.",
2675 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2676 }
2677 }
2678 }
2679
Sam Walls0961ec02020-03-31 16:39:15 +01002680 return skip;
2681}
2682
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002683void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002684 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2685 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002686}
2687
2688void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002689 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2690 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002691}
2692
Camden Stocker9c051442019-11-06 14:28:43 -08002693bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2694 const char* api_name) const {
2695 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002696 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002697
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002698 if (bp_pd_state) {
2699 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2700 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2701 "Potential problem with calling %s() without first retrieving properties from "
2702 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2703 api_name);
2704 }
Camden Stocker9c051442019-11-06 14:28:43 -08002705 }
2706
2707 return skip;
2708}
2709
Camden83a9c372019-08-14 11:41:38 -06002710bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002711 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002712 bool skip = false;
2713
Camden Stocker9c051442019-11-06 14:28:43 -08002714 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002715
Camden Stocker9c051442019-11-06 14:28:43 -08002716 return skip;
2717}
2718
2719bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2720 uint32_t planeIndex,
2721 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2722 bool skip = false;
2723
2724 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2725
2726 return skip;
2727}
2728
2729bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2730 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2731 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2732 bool skip = false;
2733
2734 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002735
2736 return skip;
2737}
Camden05de2d42019-08-19 10:23:56 -06002738
2739bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002740 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002741 bool skip = false;
2742
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002743 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06002744
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002745 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002746 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002747 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002748 skip |=
2749 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2750 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2751 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002752 }
Camden05de2d42019-08-19 10:23:56 -06002753
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002754 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2755 skip |= LogWarning(
2756 device, kVUID_BestPractices_Swapchain_InvalidCount,
2757 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002758 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002759 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2760 }
2761 }
2762
Camden05de2d42019-08-19 10:23:56 -06002763 return skip;
2764}
2765
2766// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002767bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002768 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002769 const CALL_STATE call_state,
2770 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002771 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002772 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2773 if (UNCALLED == call_state) {
2774 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002775 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002776 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2777 "recommended "
2778 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2779 caller_name, caller_name);
2780 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002781 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2782 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002783 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2784 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2785 ". It is recommended to instead receive all the properties by calling %s with "
2786 "pQueueFamilyPropertyCount that was "
2787 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002788 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002789 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002790 }
2791
2792 return skip;
2793}
2794
Jeff Bolz5c801d12019-10-09 10:38:45 -05002795bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2796 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002797 bool skip = false;
2798
2799 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002800 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002801 if (!as_state->memory_requirements_checked) {
2802 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2803 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2804 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002805 skip |= LogWarning(
2806 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002807 "vkBindAccelerationStructureMemoryNV(): "
2808 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2809 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2810 }
2811 }
2812
2813 return skip;
2814}
2815
Camden05de2d42019-08-19 10:23:56 -06002816bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2817 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002818 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002819 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002820 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002821 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002822 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2823 "vkGetPhysicalDeviceQueueFamilyProperties()");
2824 }
2825 return false;
Camden05de2d42019-08-19 10:23:56 -06002826}
2827
Mike Schuchardt2df08912020-12-15 16:28:09 -08002828bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2829 uint32_t* pQueueFamilyPropertyCount,
2830 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002831 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002832 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002833 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002834 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2835 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2836 }
2837 return false;
Camden05de2d42019-08-19 10:23:56 -06002838}
2839
Jeff Bolz5c801d12019-10-09 10:38:45 -05002840bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002841 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002842 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002843 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002844 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002845 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2846 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2847 }
2848 return false;
Camden05de2d42019-08-19 10:23:56 -06002849}
2850
2851bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2852 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002853 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002854 if (!pSurfaceFormats) return false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002855 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002856 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002857 bool skip = false;
2858 if (call_state == UNCALLED) {
2859 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2860 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002861 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2862 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2863 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002864 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002865 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002866 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2867 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2868 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2869 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002870 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002871 }
2872 }
2873 return skip;
2874}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002875
2876bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002877 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002878 bool skip = false;
2879
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002880 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2881 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002882 // 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 -07002883 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002884 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2885 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002886 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002887 // 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 -07002888 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2889 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002890 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002891 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002892 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002893 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002894 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002895 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2896 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2897 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002898 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002899 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2900 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002901 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002902 }
2903 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002904 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002905 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002906 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002907 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2908 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002909 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002910 }
2911 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002912 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2913 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002914 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002915 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002916 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002917 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002918 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002919 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2920 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2921 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002922 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002923 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2924 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002925 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002926 }
2927 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002928 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002929 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002930 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002931 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2932 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002933 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002934 }
2935 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2936 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002937 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002938 }
2939 }
2940 }
2941 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002942 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2943 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002944 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002945 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002946 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2947 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002948 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002949 }
2950 }
2951 }
2952
2953 return skip;
2954}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002955
Mark Lobodzinski84101d72020-04-24 09:43:48 -06002956void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
2957 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002958 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002959 return;
2960 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002961
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002962 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2963 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
2964 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2965 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002966 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002967 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002968 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002969 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002970 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2971 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
2972 image_state->sparse_metadata_bound = true;
2973 }
2974 }
2975 }
2976 }
2977}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002978
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002979bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002980 const VkClearRect* pRects) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002981 if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002982 // We don't know the accurate render area in a secondary,
2983 // so assume we clear the entire frame buffer.
2984 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
2985 return true;
2986 }
2987
2988 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
2989 for (uint32_t i = 0; i < rectCount; i++) {
2990 auto& rect = pRects[i];
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002991 auto& render_area = cmd.activeRenderPassBeginInfo.renderArea;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002992 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
2993 return true;
2994 }
2995 }
2996
2997 return false;
2998}
2999
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003000bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment,
3001 VkImageAspectFlags aspects, bool secondary) const {
3002 const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003003 bool skip = false;
3004
3005 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3006 return skip;
3007 }
3008
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003009 const auto& rp_state = cmd.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003010
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003011 auto attachment_itr =
3012 std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
3013 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003014
3015 // Only report aspects which haven't been touched yet.
3016 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003017 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003018 new_aspects &= ~attachment_itr->aspects;
3019 }
3020
3021 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003022 if (!cmd.hasDrawCmd) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003023 skip |= LogPerformanceWarning(
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003024 cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003025 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3026 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003027 report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003028 }
3029
3030 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3031 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3032 skip |= LogPerformanceWarning(
3033 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3034 "%svkCmdClearAttachments() issued on %s for color attachment #%u 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.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003037 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003038 }
3039
3040 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3041 rp->createInfo.pAttachments[fb_attachment].loadOp == 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 depth 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 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3051 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003052 skip |=
3053 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3054 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3055 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3056 "it is more efficient.",
3057 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003058 }
3059
3060 return skip;
3061}
3062
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003063bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003064 const VkClearAttachment* pAttachments, uint32_t rectCount,
3065 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003066 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003067 const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003068 if (!cb_node) return skip;
3069
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003070 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3071 // Defer checks to ExecuteCommands.
3072 return skip;
3073 }
3074
3075 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003076 if (!ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003077 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003078 }
3079
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003080 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3081 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003082 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003083 if (rp) {
3084 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3085
3086 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003087 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003088
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003089 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3090 uint32_t color_attachment = attachment.colorAttachment;
3091 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003092 skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003093 }
3094
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003095 if (subpass.pDepthStencilAttachment &&
3096 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003097 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003098 skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003099 }
3100 }
3101 }
3102
Nadav Gevaf0808442021-05-21 13:51:25 -04003103 if (VendorCheckEnabled(kBPVendorAMD)) {
3104 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3105 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3106 bool black_check = false;
3107 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3108 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3109 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3110 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3111 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3112
3113 bool white_check = false;
3114 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3115 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3116 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3117 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3118 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3119
3120 if (black_check && white_check) {
3121 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3122 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3123 "Consider changing to one of the following:"
3124 "RGBA(0, 0, 0, 0) "
3125 "RGBA(0, 0, 0, 1) "
3126 "RGBA(1, 1, 1, 0) "
3127 "RGBA(1, 1, 1, 1)",
3128 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3129 }
3130 } else {
3131 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3132 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3133 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3134 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3135 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3136 "attachment %" PRId32 " is not a fast clear value."
3137 "Consider changing to one of the following:"
3138 "D=0.0f, S=0"
3139 "D=1.0f, S=0",
3140 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3141 }
3142 }
3143 }
3144 }
3145
Camden Stockerf55721f2019-09-09 11:04:49 -06003146 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003147}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003148
3149bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3150 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3151 const VkImageResolve* pRegions) const {
3152 bool skip = false;
3153
3154 skip |= VendorCheckEnabled(kBPVendorArm) &&
3155 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3156 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3157 "This is a very slow and extremely bandwidth intensive path. "
3158 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3159 VendorSpecificTag(kBPVendorArm));
3160
3161 return skip;
3162}
3163
Jeff Leger178b1e52020-10-05 12:22:23 -04003164bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3165 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3166 bool skip = false;
3167
3168 skip |= VendorCheckEnabled(kBPVendorArm) &&
3169 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3170 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3171 "This is a very slow and extremely bandwidth intensive path. "
3172 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3173 VendorSpecificTag(kBPVendorArm));
3174
3175 return skip;
3176}
3177
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003178bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3179 const VkResolveImageInfo2* pResolveImageInfo) const {
3180 bool skip = false;
3181
3182 skip |= VendorCheckEnabled(kBPVendorArm) &&
3183 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3184 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3185 "This is a very slow and extremely bandwidth intensive path. "
3186 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3187 VendorSpecificTag(kBPVendorArm));
3188
3189 return skip;
3190}
3191
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003192void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3193 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3194 const VkImageResolve* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003195 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003196 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003197 auto src = Get<bp_state::Image>(srcImage);
3198 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003199
3200 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003201 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3202 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003203 }
3204}
3205
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003206void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3207 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003208 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003209 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003210 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3211 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003212 uint32_t regionCount = pResolveImageInfo->regionCount;
3213
3214 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003215 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3216 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003217 }
3218}
3219
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003220void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3221 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003222 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003223 auto& funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003224 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3225 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003226 uint32_t regionCount = pResolveImageInfo->regionCount;
3227
3228 for (uint32_t i = 0; i < regionCount; i++) {
3229 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3230 pResolveImageInfo->pRegions[i].srcSubresource);
3231 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3232 pResolveImageInfo->pRegions[i].dstSubresource);
3233 }
3234}
3235
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003236void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3237 const VkClearColorValue* pColor, uint32_t rangeCount,
3238 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003239 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003240 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003241 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003242
3243 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003244 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003245 }
3246}
3247
3248void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3249 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3250 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003251 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003252 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003253 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003254
3255 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003256 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003257 }
3258}
3259
3260void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3261 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3262 const VkImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003263 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003264 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003265 auto src = Get<bp_state::Image>(srcImage);
3266 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003267
3268 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003269 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3270 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003271 }
3272}
3273
3274void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3275 VkImageLayout dstImageLayout, uint32_t regionCount,
3276 const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003277 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003278 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003279 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003280
3281 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003282 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003283 }
3284}
3285
3286void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3287 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003288 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003289 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003290 auto src = Get<bp_state::Image>(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003291
3292 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003293 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003294 }
3295}
3296
3297void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3298 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3299 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003300 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003301 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003302 auto src = Get<bp_state::Image>(srcImage);
3303 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003304
3305 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003306 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3307 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003308 }
3309}
3310
Attilio Provenzano02859b22020-02-27 14:17:28 +00003311bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3312 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3313 bool skip = false;
3314
3315 if (VendorCheckEnabled(kBPVendorArm)) {
3316 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3317 skip |= LogPerformanceWarning(
3318 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3319 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3320 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3321 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003322 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003323 }
3324
3325 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3326 skip |= LogPerformanceWarning(
3327 device, kVUID_BestPractices_CreateSampler_LodClamping,
3328 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3329 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3330 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3331 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3332 }
3333
3334 if (pCreateInfo->mipLodBias != 0.0f) {
3335 skip |=
3336 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3337 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3338 "descriptors being created and may cause reduced performance.",
3339 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3340 }
3341
3342 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3343 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3344 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3345 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3346 skip |= LogPerformanceWarning(
3347 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3348 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3349 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3350 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3351 VendorSpecificTag(kBPVendorArm));
3352 }
3353
3354 if (pCreateInfo->unnormalizedCoordinates) {
3355 skip |= LogPerformanceWarning(
3356 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3357 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3358 "descriptors being created and may cause reduced performance.",
3359 VendorSpecificTag(kBPVendorArm));
3360 }
3361
3362 if (pCreateInfo->anisotropyEnable) {
3363 skip |= LogPerformanceWarning(
3364 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3365 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3366 "and may cause reduced performance.",
3367 VendorSpecificTag(kBPVendorArm));
3368 }
3369 }
3370
3371 return skip;
3372}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003373
Nadav Gevaf0808442021-05-21 13:51:25 -04003374void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3375 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3376 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3377 void* cgpl_state) {
3378 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3379 pPipelines);
3380 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003381 num_pso_ += createInfoCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003382}
3383
3384bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3385 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3386 const VkCopyDescriptorSet* pDescriptorCopies) const {
3387 bool skip = false;
3388 if (VendorCheckEnabled(kBPVendorAMD)) {
3389 if (descriptorCopyCount > 0) {
3390 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3391 "%s Performance warning: copying descriptor sets is not recommended",
3392 VendorSpecificTag(kBPVendorAMD));
3393 }
3394 }
3395
3396 return skip;
3397}
3398
3399bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3400 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3401 const VkAllocationCallbacks* pAllocator,
3402 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3403 bool skip = false;
3404 if (VendorCheckEnabled(kBPVendorAMD)) {
3405 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3406 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3407 "vkUpdateDescriptorSet instead",
3408 VendorSpecificTag(kBPVendorAMD));
3409 }
3410
3411 return skip;
3412}
3413
3414bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3415 const VkClearColorValue* pColor, uint32_t rangeCount,
3416 const VkImageSubresourceRange* pRanges) const {
3417 bool skip = false;
3418 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003419 skip |= LogPerformanceWarning(
3420 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003421 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3422 "vkCmdClearAttachments instead",
3423 VendorSpecificTag(kBPVendorAMD));
3424 }
3425
3426 return skip;
3427}
3428
3429bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3430 VkImageLayout imageLayout,
3431 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3432 const VkImageSubresourceRange* pRanges) const {
3433 bool skip = false;
3434 if (VendorCheckEnabled(kBPVendorAMD)) {
3435 skip |= LogPerformanceWarning(
3436 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3437 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3438 "vkCmdClearAttachments instead",
3439 VendorSpecificTag(kBPVendorAMD));
3440 }
3441
3442 return skip;
3443}
3444
3445bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3446 const VkAllocationCallbacks* pAllocator,
3447 VkPipelineLayout* pPipelineLayout) const {
3448 bool skip = false;
3449 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003450 uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2;
Nadav Gevaf0808442021-05-21 13:51:25 -04003451 // Descriptor sets cost 1 DWORD each.
3452 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3453 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3454 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3455 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3456 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003457 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003458 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size;
Nadav Gevaf0808442021-05-21 13:51:25 -04003459 }
3460
3461 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3462 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3463 }
3464
3465 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3466 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3467 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3468 "Descriptor sets cost 1 DWORD each. "
3469 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3470 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3471 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3472 VendorSpecificTag(kBPVendorAMD));
3473 }
3474 }
3475
3476 return skip;
3477}
3478
3479bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3480 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3481 const VkImageCopy* pRegions) const {
3482 bool skip = false;
3483 std::stringstream src_image_hex;
3484 std::stringstream dst_image_hex;
3485 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3486 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3487
3488 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003489 auto src_state = Get<IMAGE_STATE>(srcImage);
3490 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003491
3492 if (src_state && dst_state) {
3493 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3494 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3495 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3496 skip |=
3497 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3498 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3499 "image (vkCmdCopyImageToBuffer) "
3500 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3501 "copies when converting between linear and optimal images",
3502 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3503 }
3504 }
3505 }
3506
3507 return skip;
3508}
3509
3510bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3511 VkPipeline pipeline) const {
3512 bool skip = false;
3513
3514 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003515 if (IsPipelineUsedInFrame(pipeline)) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003516 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3517 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3518 "for example, by sorting draw calls by pipeline.",
3519 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3520 }
3521 }
3522
3523 return skip;
3524}
3525
3526void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3527 VkFence fence, VkResult result) {
3528 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003529 num_queue_submissions_ += submitCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003530}
3531
3532bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3533 bool skip = false;
3534
3535 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003536 auto num = num_queue_submissions_.load();
3537 if (num > kNumberOfSubmissionWarningLimitAMD) {
3538 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3539 "%s Performance warning: command buffers submitted %" PRId32
3540 " times this frame. Submitting command buffers has a CPU "
3541 "and GPU overhead. Submit fewer times to incur less overhead.",
3542 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04003543 }
3544 }
3545
3546 return skip;
3547}
3548
3549void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3550 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3551 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3552 uint32_t bufferMemoryBarrierCount,
3553 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3554 uint32_t imageMemoryBarrierCount,
3555 const VkImageMemoryBarrier* pImageMemoryBarriers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003556 num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount);
Nadav Gevaf0808442021-05-21 13:51:25 -04003557}
3558
3559bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3560 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3561 bool skip = false;
3562 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003563 if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003564 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3565 "%s Performance warning: High number of vkSemaphore objects created."
3566 "Minimize the amount of queue synchronization that is used. "
3567 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3568 VendorSpecificTag(kBPVendorAMD));
3569 }
3570 }
3571
3572 return skip;
3573}
3574
3575bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3576 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3577 bool skip = false;
3578 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003579 if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003580 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3581 "%s Performance warning: High number of VkFence objects created."
3582 "Minimize the amount of CPU-GPU synchronization that is used. "
3583 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3584 VendorSpecificTag(kBPVendorAMD));
3585 }
3586 }
3587
3588 return skip;
3589}
3590
Sam Walls8e77e4f2020-03-16 20:47:40 +00003591void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3592
3593bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3594 // look for a cache hit
3595 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3596 if (hit != _entries.end()) {
3597 // mark the cache hit as being most recently used
3598 hit->age = iteration++;
3599 return true;
3600 }
3601
3602 // if there's no cache hit, we need to model the entry being inserted into the cache
3603 CacheEntry new_entry = {value, iteration};
3604 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3605 // if there is still space left in the cache, use the next available slot
3606 *(_entries.begin() + iteration) = new_entry;
3607 } else {
3608 // otherwise replace the least recently used cache entry
3609 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3610 *lru = new_entry;
3611 }
3612 iteration++;
3613 return false;
3614}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003615
3616bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3617 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003618 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003619 bool skip = false;
3620 if (swapchain_data && swapchain_data->images.size() == 0) {
3621 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3622 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3623 "vkGetSwapchainImagesKHR after swapchain creation.");
3624 }
3625 return skip;
3626}
3627
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003628void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3629 if (no_pointer) {
3630 if (UNCALLED == call_state) {
3631 call_state = QUERY_COUNT;
3632 }
3633 } else { // Save queue family properties
3634 call_state = QUERY_DETAILS;
3635 }
3636}
3637
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003638void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3639 uint32_t* pQueueFamilyPropertyCount,
3640 VkQueueFamilyProperties* pQueueFamilyProperties) {
3641 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3642 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003643 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003644 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003645 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3646 nullptr == pQueueFamilyProperties);
3647 }
3648}
3649
3650void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3651 uint32_t* pQueueFamilyPropertyCount,
3652 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3653 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3654 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003655 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003656 if (bp_pd_state) {
3657 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3658 nullptr == pQueueFamilyProperties);
3659 }
3660}
3661
3662void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3663 uint32_t* pQueueFamilyPropertyCount,
3664 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3665 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3666 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003667 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003668 if (bp_pd_state) {
3669 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3670 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003671 }
3672}
3673
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003674void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3675 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003676 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003677 if (bp_pd_state) {
3678 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3679 }
3680}
3681
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003682void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3683 VkPhysicalDeviceFeatures2* pFeatures) {
3684 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003685 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003686 if (bp_pd_state) {
3687 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3688 }
3689}
3690
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003691void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3692 VkPhysicalDeviceFeatures2* pFeatures) {
3693 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
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->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3697 }
3698}
3699
3700void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3701 VkSurfaceKHR surface,
3702 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3703 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003704 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003705 if (bp_pd_state) {
3706 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3707 }
3708}
3709
3710void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3711 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3712 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, 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::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3720 VkSurfaceKHR surface,
3721 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3722 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003723 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003724 if (bp_pd_state) {
3725 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3726 }
3727}
3728
3729void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3730 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3731 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003732 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003733 if (bp_pd_data) {
3734 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3735
3736 if (*pPresentModeCount) {
3737 if (call_state < QUERY_COUNT) {
3738 call_state = QUERY_COUNT;
3739 }
3740 }
3741 if (pPresentModes) {
3742 if (call_state < QUERY_DETAILS) {
3743 call_state = QUERY_DETAILS;
3744 }
3745 }
3746 }
3747}
3748
3749void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3750 uint32_t* pSurfaceFormatCount,
3751 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003752 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003753 if (bp_pd_data) {
3754 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3755
3756 if (*pSurfaceFormatCount) {
3757 if (call_state < QUERY_COUNT) {
3758 call_state = QUERY_COUNT;
3759 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003760 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003761 }
3762 if (pSurfaceFormats) {
3763 if (call_state < QUERY_DETAILS) {
3764 call_state = QUERY_DETAILS;
3765 }
3766 }
3767 }
3768}
3769
3770void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3771 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3772 uint32_t* pSurfaceFormatCount,
3773 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003774 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003775 if (bp_pd_data) {
3776 if (*pSurfaceFormatCount) {
3777 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3778 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3779 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003780 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003781 }
3782 if (pSurfaceFormats) {
3783 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3784 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3785 }
3786 }
3787 }
3788}
3789
3790void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3791 uint32_t* pPropertyCount,
3792 VkDisplayPlanePropertiesKHR* pProperties,
3793 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003794 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003795 if (bp_pd_data) {
3796 if (*pPropertyCount) {
3797 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3798 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3799 }
3800 }
3801 if (pProperties) {
3802 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3803 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3804 }
3805 }
3806 }
3807}
3808
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003809void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3810 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3811 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003812 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003813 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3814 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3815 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003816 }
3817 }
3818}
3819
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003820void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3821 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3822
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003823 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003824 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003825 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003826 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003827 auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003828 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003829 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003830 }
3831 }
3832 }
3833}