blob: 2ccc8d86065c0ad42e02de2f41478198ca9dbf1c [file] [log] [blame]
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
Nadav Geva41c12a22021-05-21 13:14:05 -04004 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Camdeneaa86ea2019-07-26 11:00:09 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Camden Stocker <camden@lunarg.com>
Nadav Geva41c12a22021-05-21 13:14:05 -040019 * Author: Nadav Geva <nadav.geva@amd.com>
Camdeneaa86ea2019-07-26 11:00:09 -060020 */
21
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070022#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060023#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060024#include "best_practices_error_enums.h"
Sam Wallsd7ab6db2020-06-19 20:41:54 +010025#include "shader_validation.h"
Jeremy Gebbena3705f42021-01-19 16:47:43 -070026#include "sync_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060027#include "cmd_buffer_state.h"
28#include "device_state.h"
29#include "render_pass_state.h"
Camden5b184be2019-08-13 07:50:19 -060030
31#include <string>
Sam Walls8e77e4f2020-03-16 20:47:40 +000032#include <bitset>
Sam Wallsd7ab6db2020-06-19 20:41:54 +010033#include <memory>
Camden5b184be2019-08-13 07:50:19 -060034
Attilio Provenzano19d6a982020-02-27 12:41:41 +000035struct VendorSpecificInfo {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060036 EnableFlags vendor_id;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000037 std::string name;
38};
39
LawG475463092022-02-22 10:45:54 +000040const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {{kBPVendorArm, {vendor_specific_arm, "Arm"}},
41 {kBPVendorAMD, {vendor_specific_amd, "AMD"}},
Rodrigo Locattic779cb32022-02-25 19:26:31 -030042 {kBPVendorIMG, {vendor_specific_img, "IMG"}},
43 {kBPVendorNVIDIA, {vendor_specific_nvidia, "NVIDIA"}}};
Attilio Provenzano19d6a982020-02-27 12:41:41 +000044
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
Camden5b184be2019-08-13 07:50:19 -0600205bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500206 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600207 bool skip = false;
208
209 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
210 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800211 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700212 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
213 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600214 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600215 uint32_t specified_version =
216 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
217 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700218 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200219 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600220 }
221
222 return skip;
223}
224
Camden5b184be2019-08-13 07:50:19 -0600225bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500226 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600227 bool skip = false;
228
229 // get API version of physical device passed when creating device.
230 VkPhysicalDeviceProperties physical_device_properties{};
231 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500232 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600233
234 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600235 if (api_version > device_api_version) {
236 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600237 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600238
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700239 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
240 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
241 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600242 }
243
Rodrigo Locattic2d5cf42022-03-01 18:05:26 -0300244 std::vector<std::string> extensions;
245 {
246 uint32_t property_count = 0;
247 if (DispatchEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &property_count, nullptr) == VK_SUCCESS) {
248 std::vector<VkExtensionProperties> property_list(property_count);
249 if (DispatchEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &property_count, property_list.data()) == VK_SUCCESS) {
250 extensions.reserve(property_list.size());
251 for (const VkExtensionProperties& properties : property_list) {
252 extensions.push_back(properties.extensionName);
253 }
254 }
255 }
256 }
257
Camden5b184be2019-08-13 07:50:19 -0600258 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300259 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
260
261 if (white_list(extension_name, kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800262 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700263 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300264 extension_name);
Camden5b184be2019-08-13 07:50:19 -0600265 }
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300266
267 skip |= ValidateDeprecatedExtensions("CreateDevice", extension_name, api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700268 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300269 skip |= ValidateSpecialUseExtensions("CreateDevice", extension_name, kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600270 }
271
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700272 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600273 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700274 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
275 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600276 }
277
LawG43f848c72022-02-23 09:35:21 +0000278 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorIMG)) &&
279 (pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
Szilard Papp7d2c7952020-06-22 14:38:13 +0100280 skip |= LogPerformanceWarning(
281 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
LawG4015be1c2022-03-01 10:37:52 +0000282 "%s %s %s: vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100283 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
284 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
285 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
LawG43f848c72022-02-23 09:35:21 +0000286 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorIMG));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100287 }
288
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300289 const bool enabled_pageable_device_local_memory = IsExtEnabled(device_extensions.vk_ext_pageable_device_local_memory);
290 if (VendorCheckEnabled(kBPVendorNVIDIA) && !enabled_pageable_device_local_memory &&
291 std::find(extensions.begin(), extensions.end(), VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME) != extensions.end()) {
292 skip |= LogPerformanceWarning(
293 device, kVUID_BestPractices_CreateDevice_PageableDeviceLocalMemory,
294 "%s vkCreateDevice() called without pageable device local memory. "
295 "Use pageableDeviceLocalMemory from VK_EXT_pageable_device_local_memory when it is available.",
296 VendorSpecificTag(kBPVendorNVIDIA));
297 }
298
Camden5b184be2019-08-13 07:50:19 -0600299 return skip;
300}
301
302bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500303 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600304 bool skip = false;
305
306 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700307 std::stringstream buffer_hex;
308 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600309
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700310 skip |= LogWarning(
311 device, kVUID_BestPractices_SharingModeExclusive,
312 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
313 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700314 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600315 }
316
317 return skip;
318}
319
320bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500321 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600322 bool skip = false;
323
324 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700325 std::stringstream image_hex;
326 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600327
328 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700329 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
330 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
331 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700332 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600333 }
334
ziga-lunarg6df3d102022-03-18 17:02:14 +0100335 if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
336 skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags,
337 "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not "
338 "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the "
339 "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect.");
340 }
341
LawG4655f59c2022-02-23 13:55:55 +0000342 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000343 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
344 skip |= LogPerformanceWarning(
345 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
LawG4655f59c2022-02-23 13:55:55 +0000346 "%s %s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
Attilio Provenzano02859b22020-02-27 14:17:28 +0000347 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
348 "and do not need to be backed by physical storage. "
349 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
LawG4655f59c2022-02-23 13:55:55 +0000350 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG));
Attilio Provenzano02859b22020-02-27 14:17:28 +0000351 }
352 }
353
LawG4ba113892022-02-23 14:39:02 +0000354 if (VendorCheckEnabled(kBPVendorArm) && pCreateInfo->samples > kMaxEfficientSamplesArm) {
355 skip |= LogPerformanceWarning(
356 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
357 "%s vkCreateImage(): Trying to create an image with %u samples. "
358 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
359 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
360 }
361
362 if (VendorCheckEnabled(kBPVendorIMG) && pCreateInfo->samples > kMaxEfficientSamplesImg) {
363 skip |= LogPerformanceWarning(
364 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
365 "%s vkCreateImage(): Trying to create an image with %u samples. "
366 "The device may not have full support for true multisampling for images with more than %u samples. "
367 "XT devices support up to 8 samples, XE up to 4 samples.",
368 VendorSpecificTag(kBPVendorIMG), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesImg);
369 }
370
LawG4db16f802022-03-21 17:33:39 +0000371 if (VendorCheckEnabled(kBPVendorIMG) && (pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG ||
372 pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG ||
373 pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG ||
374 pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG ||
375 pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG ||
376 pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG ||
377 pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG ||
378 pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG)) {
379 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Texture_Format_PVRTC_Outdated,
380 "%s vkCreateImage(): Trying to create an image with a PVRTC format. Both PVRTC1 and PVRTC2 "
381 "are slower than standard image formats on PowerVR GPUs, prefer ETC, BC, ASTC, etc.",
382 VendorSpecificTag(kBPVendorIMG));
383 }
384
Nadav Gevaf0808442021-05-21 13:51:25 -0400385 if (VendorCheckEnabled(kBPVendorAMD)) {
386 std::stringstream image_hex;
387 image_hex << "0x" << std::hex << HandleToUint64(pImage);
388
389 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
390 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
391 skip |= LogPerformanceWarning(device,
392 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
393 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
394 "Using a SHARING_MODE_CONCURRENT "
395 "is not recommended with color and depth targets",
396 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
397 }
398
399 if ((pCreateInfo->usage &
400 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
401 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
402 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
403 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
404 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
405 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
406 }
407
408 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
409 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
410 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
411 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
412 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
413 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
414 }
415 }
416
Rodrigo Locatti5466f9d2022-03-09 18:20:38 -0300417 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
418 std::stringstream image_hex;
419 image_hex << "0x" << std::hex << HandleToUint64(pImage);
420
421 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) {
422 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateImage_TilingLinear,
423 "%s Performance warning: image (%s) is created with tiling VK_IMAGE_TILING_LINEAR. "
424 "Use VK_IMAGE_TILING_OPTIMAL instead.",
425 VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str());
426 }
Rodrigo Locatti3290c2b2022-03-09 18:25:56 -0300427
428 if (pCreateInfo->format == VK_FORMAT_D32_SFLOAT || pCreateInfo->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
429 skip |= LogPerformanceWarning(
430 device, kVUID_BestPractices_CreateImage_Depth32Format,
431 "%s Performance warning: image (%s) is created with a 32-bit depth format. Use VK_FORMAT_D24_UNORM_S8_UINT or "
432 "VK_FORMAT_D16_UNORM instead, unless the extra precision is needed.",
433 VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str());
434 }
Rodrigo Locatti5466f9d2022-03-09 18:20:38 -0300435 }
436
Camden5b184be2019-08-13 07:50:19 -0600437 return skip;
438}
439
440bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500441 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600442 bool skip = false;
443
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600444 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600445 if (bp_pd_state) {
446 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
447 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
448 "vkCreateSwapchainKHR() called before getting surface capabilities from "
449 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
450 }
Camden83a9c372019-08-14 11:41:38 -0600451
Shannon McPherson73e58c82021-03-05 17:14:26 -0700452 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
453 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600454 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
455 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
456 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
457 }
Camden83a9c372019-08-14 11:41:38 -0600458
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600459 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
460 skip |= LogWarning(
461 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
462 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
463 }
Camden83a9c372019-08-14 11:41:38 -0600464 }
465
Camden5b184be2019-08-13 07:50:19 -0600466 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700467 skip |=
468 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600469 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700470 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
471 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600472 }
473
ziga-lunarg79beba62022-03-30 01:17:30 +0200474 const auto present_mode = pCreateInfo->presentMode;
475 if (((present_mode == VK_PRESENT_MODE_MAILBOX_KHR) || (present_mode == VK_PRESENT_MODE_FIFO_KHR)) &&
476 (pCreateInfo->minImageCount == 2)) {
Szilard Papp48a6da32020-06-10 14:41:59 +0100477 skip |= LogPerformanceWarning(
478 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
479 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
480 ", which means double buffering is going "
481 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
482 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
483 "3 to use triple buffering to maximize performance in such cases.",
484 pCreateInfo->minImageCount);
485 }
486
Szilard Pappd5f0f812020-06-22 09:01:29 +0100487 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
488 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
489 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
490 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
491 "Presentation modes which are not FIFO will present the latest available frame and discard other "
492 "frame(s) if any.",
493 VendorSpecificTag(kBPVendorArm));
494 }
495
Camden5b184be2019-08-13 07:50:19 -0600496 return skip;
497}
498
499bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
500 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500501 const VkAllocationCallbacks* pAllocator,
502 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600503 bool skip = false;
504
505 for (uint32_t i = 0; i < swapchainCount; i++) {
506 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700507 skip |= LogWarning(
508 device, kVUID_BestPractices_SharingModeExclusive,
509 "Warning: A shared swapchain (index %" PRIu32
510 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
511 "queues (queueFamilyIndexCount of %" PRIu32 ").",
512 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600513 }
514 }
515
516 return skip;
517}
518
519bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500520 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600521 bool skip = false;
522
523 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
524 VkFormat format = pCreateInfo->pAttachments[i].format;
525 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
526 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
527 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700528 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
529 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
530 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
531 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
532 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600533 }
534 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700535 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
536 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
537 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
538 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
539 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600540 }
541 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000542
543 const auto& attachment = pCreateInfo->pAttachments[i];
544 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
545 bool access_requires_memory =
546 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
547
548 if (FormatHasStencil(format)) {
549 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
550 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
551 }
552
553 if (access_requires_memory) {
554 skip |= LogPerformanceWarning(
555 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
556 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
557 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
558 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
559 i, static_cast<uint32_t>(attachment.samples));
560 }
561 }
Camden5b184be2019-08-13 07:50:19 -0600562 }
563
564 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
565 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
566 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
567 }
568
569 return skip;
570}
571
Tony-LunarG767180f2020-04-23 14:03:59 -0600572bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
573 const VkImageView* image_views) const {
574 bool skip = false;
575
576 // Check for non-transient attachments that should be transient and vice versa
577 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200578 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600579 bool attachment_should_be_transient =
580 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
581
582 if (FormatHasStencil(attachment.format)) {
583 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
584 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
585 }
586
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600587 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600588 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600589 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600590
591 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
592
593 // The check for an image that should not be transient applies to all GPUs
594 if (!attachment_should_be_transient && image_is_transient) {
595 skip |= LogPerformanceWarning(
596 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
597 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
598 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
599 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
600 i);
601 }
602
603 bool supports_lazy = false;
604 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
605 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
606 supports_lazy = true;
607 }
608 }
609
610 // The check for an image that should be transient only applies to GPUs supporting
611 // lazily allocated memory
612 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
613 skip |= LogPerformanceWarning(
614 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
615 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
616 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
617 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
618 i);
619 }
620 }
621 }
622 return skip;
623}
624
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000625bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
626 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
627 bool skip = false;
628
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600629 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800630 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600631 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000632 }
633
634 return skip;
635}
636
Sam Wallse746d522020-03-16 21:20:23 +0000637bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
638 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
639 bool skip = false;
640 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
641
642 if (!skip) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700643 const auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000644 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
645 // this warning is specific to Arm
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700646 if (VendorCheckEnabled(kBPVendorArm) && pool_state && (pool_state->freed_count > 0)) {
Sam Wallse746d522020-03-16 21:20:23 +0000647 skip |= LogPerformanceWarning(
648 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
649 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
650 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
651 VendorSpecificTag(kBPVendorArm));
652 }
ziga-lunarg5a76c442022-04-17 18:04:08 +0200653
654 if (IsExtEnabled(device_extensions.vk_khr_maintenance1)) {
655 // Track number of descriptorSets allowable in this pool
656 if (pool_state->GetAvailableSets() < pAllocateInfo->descriptorSetCount) {
657 skip |= LogWarning(pool_state->Handle(), kVUID_BestPractices_EmptyDescriptorPool,
658 "vkAllocateDescriptorSets(): Unable to allocate %" PRIu32 " descriptorSets from %s"
659 ". This pool only has %" PRIu32 " descriptorSets remaining.",
660 pAllocateInfo->descriptorSetCount, report_data->FormatHandle(pool_state->Handle()).c_str(),
661 pool_state->GetAvailableSets());
662 }
663 }
Sam Wallse746d522020-03-16 21:20:23 +0000664 }
665
666 return skip;
667}
668
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600669void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
670 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000671 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700672 auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
673 if (pool_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000674 // we record successful allocations by subtracting the allocation count from the last recorded free count
675 const auto alloc_count = pAllocateInfo->descriptorSetCount;
676 // clamp the unsigned subtraction to the range [0, last_free_count]
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700677 if (pool_state->freed_count > alloc_count) {
678 pool_state->freed_count -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700679 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700680 pool_state->freed_count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700681 }
Sam Wallse746d522020-03-16 21:20:23 +0000682 }
683 }
684}
685
686void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
687 const VkDescriptorSet* pDescriptorSets, VkResult result) {
688 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
689 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700690 auto pool_state = Get<bp_state::DescriptorPool>(descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000691 // we want to track frees because we're interested in suggesting re-use
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700692 if (pool_state) {
693 pool_state->freed_count += descriptorSetCount;
Sam Wallse746d522020-03-16 21:20:23 +0000694 }
695 }
696}
697
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300698void BestPractices::PreCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
699 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) {
700 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
701 WriteLockGuard guard{memory_free_events_lock_};
702
703 // Release old allocations to avoid overpopulating the container
704 const auto now = std::chrono::high_resolution_clock::now();
705 const auto last_old = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [now](const MemoryFreeEvent& event) {
706 return now - event.time > kAllocateMemoryReuseTimeThresholdNVIDIA;
707 });
708 memory_free_events_.erase(memory_free_events_.begin(), last_old.base());
709 }
710}
711
Camden5b184be2019-08-13 07:50:19 -0600712bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500713 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600714 bool skip = false;
715
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700716 if ((Count<DEVICE_MEMORY_STATE>() + 1) > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700717 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
718 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600719 }
720
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000721 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
722 skip |= LogPerformanceWarning(
723 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600724 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
725 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000726 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
727 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
728 }
729
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300730 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
731 if (!device_extensions.vk_ext_pageable_device_local_memory &&
732 !LvlFindInChain<VkMemoryPriorityAllocateInfoEXT>(pAllocateInfo->pNext)) {
733 skip |= LogPerformanceWarning(
734 device, kVUID_BestPractices_AllocateMemory_SetPriority,
735 "%s Use VkMemoryPriorityAllocateInfoEXT to provide the operating system information on the allocations that "
736 "should stay in video memory and which should be demoted first when video memory is limited. "
737 "The highest priority should be given to GPU-written resources like color attachments, depth attachments, "
738 "storage images, and buffers written from the GPU.",
739 VendorSpecificTag(kBPVendorNVIDIA));
740 }
741
742 {
743 // Size in bytes for an allocation to be considered "compatible"
744 static constexpr VkDeviceSize size_threshold = VkDeviceSize{1} << 20;
745
746 ReadLockGuard guard{memory_free_events_lock_};
747
748 const auto now = std::chrono::high_resolution_clock::now();
749 const VkDeviceSize alloc_size = pAllocateInfo->allocationSize;
750 const uint32_t memory_type_index = pAllocateInfo->memoryTypeIndex;
751 const auto latest_event = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [&](const MemoryFreeEvent& event) {
752 return (memory_type_index == event.memory_type_index) && (alloc_size <= event.allocation_size) &&
753 (alloc_size - event.allocation_size <= size_threshold) && (now - event.time < kAllocateMemoryReuseTimeThresholdNVIDIA);
754 });
755
756 if (latest_event != memory_free_events_.rend()) {
757 const auto time_delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - latest_event->time);
758 if (time_delta < std::chrono::milliseconds{5}) {
759 skip |=
760 LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_ReuseAllocations,
761 "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation "
762 "has just been released, and it could have been reused in place of this allocation.",
763 VendorSpecificTag(kBPVendorNVIDIA));
764 } else {
765 const uint32_t seconds = static_cast<uint32_t>(time_delta.count() / 1000);
766 const uint32_t milliseconds = static_cast<uint32_t>(time_delta.count() % 1000);
767
768 skip |= LogPerformanceWarning(
769 device, kVUID_BestPractices_AllocateMemory_ReuseAllocations,
770 "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation has been released "
771 "%" PRIu32 ".%03" PRIu32 " seconds ago, and it could have been reused in place of this allocation.",
772 VendorSpecificTag(kBPVendorNVIDIA), seconds, milliseconds);
773 }
774 }
775 }
Rodrigo Locattie4f8d522022-03-15 16:30:49 -0300776 }
777
Camden83a9c372019-08-14 11:41:38 -0600778 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
779
780 return skip;
781}
782
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600783void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
784 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
785 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700786 if (result != VK_SUCCESS) {
787 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
788 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800789 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700790 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600791 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700792 return;
793 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700794}
Camden Stocker9738af92019-10-16 13:54:03 -0700795
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600796void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
797 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700798 auto error = std::find(error_codes.begin(), error_codes.end(), result);
799 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000800 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
801 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
802
803 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
804 if (common_failure != common_failure_codes.end()) {
805 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
806 } else {
807 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
808 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700809 return;
810 }
811 auto success = std::find(success_codes.begin(), success_codes.end(), result);
812 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600813 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
814 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500815 }
816}
817
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300818void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
819 if (memory != VK_NULL_HANDLE && VendorCheckEnabled(kBPVendorNVIDIA)) {
820 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
821
822 // Exclude memory free events on dedicated allocations, or imported/exported allocations.
823 if (!mem_info->IsDedicatedBuffer() && !mem_info->IsDedicatedImage() && !mem_info->IsExport() && !mem_info->IsImport()) {
824 MemoryFreeEvent event;
825 event.time = std::chrono::high_resolution_clock::now();
826 event.memory_type_index = mem_info->alloc_info.memoryTypeIndex;
827 event.allocation_size = mem_info->alloc_info.allocationSize;
828
829 WriteLockGuard guard{memory_free_events_lock_};
830 memory_free_events_.push_back(event);
831 }
832 }
833
834 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
835}
836
Jeff Bolz5c801d12019-10-09 10:38:45 -0500837bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
838 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700839 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600840 bool skip = false;
841
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700842 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600843
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700844 for (const auto& item : mem_info->ObjectBindings()) {
845 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600846 LogObjectList objlist(device);
847 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600848 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600849 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 -0600850 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600851 }
852
Camden5b184be2019-08-13 07:50:19 -0600853 return skip;
854}
855
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000856bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600857 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700858 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600859
sfricke-samsunge2441192019-11-06 14:07:57 -0800860 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700861 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
862 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
863 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600864 }
865
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700866 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000867
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300868 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000869 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
870 skip |= LogPerformanceWarning(
871 device, kVUID_BestPractices_SmallDedicatedAllocation,
872 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600873 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
874 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000875 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
876 }
877
Rodrigo Locatti66b23352022-03-15 17:28:32 -0300878 skip |= ValidateBindMemory(device, memory);
879
Camden Stockerb603cc82019-09-03 10:09:02 -0600880 return skip;
881}
882
883bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500884 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600885 bool skip = false;
886 const char* api_name = "BindBufferMemory()";
887
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000888 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600889
890 return skip;
891}
892
893bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500894 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600895 char api_name[64];
896 bool skip = false;
897
898 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +0200899 snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000900 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600901 }
902
903 return skip;
904}
Camden Stockerb603cc82019-09-03 10:09:02 -0600905
906bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500907 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600908 char api_name[64];
909 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600910
Camden Stocker8b798ab2019-09-03 10:33:28 -0600911 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +0200912 snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000913 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600914 }
915
916 return skip;
917}
918
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000919bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600920 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700921 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600922
sfricke-samsung71bc6572020-04-29 15:49:43 -0700923 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600924 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700925 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
926 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
927 api_name, report_data->FormatHandle(image).c_str());
928 }
929 } else {
930 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
931 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600932 }
933
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700934 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000935
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600936 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000937 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
938 skip |= LogPerformanceWarning(
939 device, kVUID_BestPractices_SmallDedicatedAllocation,
940 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600941 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
942 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000943 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
944 }
945
946 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
947 // make sure this type is actually used.
948 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
949 // (i.e.most tile - based renderers)
950 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
951 bool supports_lazy = false;
952 uint32_t suggested_type = 0;
953
954 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600955 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000956 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
957 supports_lazy = true;
958 suggested_type = i;
959 break;
960 }
961 }
962 }
963
964 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
965
966 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
967 skip |= LogPerformanceWarning(
968 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600969 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000970 "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 -0600971 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600972 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000973 }
974 }
975
Rodrigo Locatti66b23352022-03-15 17:28:32 -0300976 skip |= ValidateBindMemory(device, memory);
977
Camden Stocker8b798ab2019-09-03 10:33:28 -0600978 return skip;
979}
980
981bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500982 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600983 bool skip = false;
984 const char* api_name = "vkBindImageMemory()";
985
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000986 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600987
988 return skip;
989}
990
991bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500992 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600993 char api_name[64];
994 bool skip = false;
995
996 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +0200997 snprintf(api_name, sizeof(api_name), "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700998 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600999 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
1000 }
Camden Stocker8b798ab2019-09-03 10:33:28 -06001001 }
1002
1003 return skip;
1004}
1005
1006bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001007 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -06001008 char api_name[64];
1009 bool skip = false;
1010
1011 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +02001012 snprintf(api_name, sizeof(api_name), "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +00001013 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -06001014 }
1015
1016 return skip;
1017}
Camden83a9c372019-08-14 11:41:38 -06001018
Rodrigo Locatti66b23352022-03-15 17:28:32 -03001019void BestPractices::PreCallRecordSetDeviceMemoryPriorityEXT(VkDevice device, VkDeviceMemory memory, float priority) {
1020 auto mem_info = std::static_pointer_cast<bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory));
1021 mem_info->dynamic_priority.emplace(priority);
1022}
1023
Attilio Provenzano02859b22020-02-27 14:17:28 +00001024static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
1025 switch (format) {
1026 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
1027 case VK_FORMAT_R16_SFLOAT:
1028 case VK_FORMAT_R16G16_SFLOAT:
1029 case VK_FORMAT_R16G16B16_SFLOAT:
1030 case VK_FORMAT_R16G16B16A16_SFLOAT:
1031 case VK_FORMAT_R32_SFLOAT:
1032 case VK_FORMAT_R32G32_SFLOAT:
1033 case VK_FORMAT_R32G32B32_SFLOAT:
1034 case VK_FORMAT_R32G32B32A32_SFLOAT:
1035 return false;
1036
1037 default:
1038 return true;
1039 }
1040}
1041
1042bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
1043 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
1044 bool skip = false;
1045
1046 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001047 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001048
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001049 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
1050 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
1051 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +00001052 return skip;
1053 }
1054
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001055 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001056 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001057
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001058 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1059 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
1060
1061 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001062 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001063 uint32_t att = subpass.pColorAttachments[j].attachment;
1064
1065 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
1066 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
1067 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
1068 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
1069 "color attachment #%u makes use "
1070 "of a format which cannot be blended at full throughput when using MSAA.",
1071 VendorSpecificTag(kBPVendorArm), i, j);
1072 }
1073 }
1074 }
1075 }
1076
1077 return skip;
1078}
1079
Nadav Gevaf0808442021-05-21 13:51:25 -04001080void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1081 const VkComputePipelineCreateInfo* pCreateInfos,
1082 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1083 VkResult result, void* pipe_state) {
1084 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001085 pipeline_cache_ = pipelineCache;
Nadav Gevaf0808442021-05-21 13:51:25 -04001086}
1087
Camden5b184be2019-08-13 07:50:19 -06001088bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1089 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001090 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001091 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001092 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1093 pAllocator, pPipelines, cgpl_state_data);
ziga-lunarg08c81582022-03-08 17:33:45 +01001094 if (skip) {
1095 return skip;
1096 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001097 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001098
1099 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001100 skip |= LogPerformanceWarning(
1101 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1102 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
1103 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001104 }
1105
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001106 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001107 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001108
Tony-LunarGb6a2daf2022-07-29 11:30:55 -06001109 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV) && create_info.pVertexInputState) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001110 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001111 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001112 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
1113 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001114 count++;
1115 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001116 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001117 if (count > kMaxInstancedVertexBuffers) {
1118 skip |= LogPerformanceWarning(
1119 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
1120 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
1121 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
1122 count, kMaxInstancedVertexBuffers);
1123 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001124 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001125
Szilard Pappaaf2da32020-06-22 10:37:35 +01001126 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
1127 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001128 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
1129 VendorCheckEnabled(kBPVendorArm)) {
1130 skip |= LogPerformanceWarning(
1131 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
1132 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
1133 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
1134 "efficiency during rasterization. Consider disabling depthBias or increasing either "
1135 "depthBiasConstantFactor or depthBiasSlopeFactor.",
1136 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +01001137 }
1138
Attilio Provenzano02859b22020-02-27 14:17:28 +00001139 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001140 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001141 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001142 auto prev_pipeline = pipeline_cache_.load();
1143 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001144 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001145 "%s %s Performance Warning: A second pipeline cache is in use. "
1146 "Consider using only one pipeline cache to improve cache hit rate.",
1147 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04001148 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001149 }
1150 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001151 if (num_pso_ > kMaxRecommendedNumberOfPSOAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001152 skip |=
1153 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
1154 "%s Performance warning: Too many pipelines created, consider consolidation",
1155 VendorSpecificTag(kBPVendorAMD));
1156 }
1157
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -06001158 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001159 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
1160 "%s Performance warning: Use of primitive restart is not recommended",
1161 VendorSpecificTag(kBPVendorAMD));
1162 }
1163
1164 // TODO: this might be too aggressive of a check
1165 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
1166 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
1167 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1168 VendorSpecificTag(kBPVendorAMD));
1169 }
1170 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001171
Camden5b184be2019-08-13 07:50:19 -06001172 return skip;
1173}
1174
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001175static std::vector<bp_state::AttachmentInfo> GetAttachmentAccess(const safe_VkGraphicsPipelineCreateInfo& create_info,
1176 std::shared_ptr<const RENDER_PASS_STATE>& rp) {
1177 std::vector<bp_state::AttachmentInfo> result;
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06001178 if (!rp || rp->UsesDynamicRendering()) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001179 return result;
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001180 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001181
1182 const auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1183
1184 // NOTE: see PIPELINE_LAYOUT and safe_VkGraphicsPipelineCreateInfo constructors. pColorBlendState and pDepthStencilState
1185 // are only non-null if they are enabled.
1186 if (create_info.pColorBlendState) {
1187 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1188 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info.pColorBlendState->attachmentCount);
1189 for (uint32_t j = 0; j < num_color_attachments; j++) {
1190 if (create_info.pColorBlendState->pAttachments[j].colorWriteMask != 0) {
1191 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1192 if (attachment != VK_ATTACHMENT_UNUSED) {
1193 result.push_back({attachment, VK_IMAGE_ASPECT_COLOR_BIT});
1194 }
1195 }
1196 }
1197 }
1198
1199 if (create_info.pDepthStencilState &&
1200 (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable ||
1201 create_info.pDepthStencilState->stencilTestEnable)) {
1202 uint32_t attachment = subpass.pDepthStencilAttachment ? subpass.pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
1203 if (attachment != VK_ATTACHMENT_UNUSED) {
1204 VkImageAspectFlags aspects = 0;
1205 if (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable) {
1206 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1207 }
1208 if (create_info.pDepthStencilState->stencilTestEnable) {
1209 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1210 }
1211 result.push_back({attachment, aspects});
1212 }
1213 }
1214 return result;
1215}
1216
1217bp_state::Pipeline::Pipeline(const ValidationStateTracker* state_data, const VkGraphicsPipelineCreateInfo* pCreateInfo,
1218 std::shared_ptr<const RENDER_PASS_STATE>&& rpstate,
1219 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout)
1220 : PIPELINE_STATE(state_data, pCreateInfo, std::move(rpstate), std::move(layout)),
1221 access_framebuffer_attachments(GetAttachmentAccess(create_info.graphics, rp_state)) {}
1222
1223std::shared_ptr<PIPELINE_STATE> BestPractices::CreateGraphicsPipelineState(
1224 const VkGraphicsPipelineCreateInfo* pCreateInfo, std::shared_ptr<const RENDER_PASS_STATE>&& render_pass,
1225 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) const {
1226 return std::static_pointer_cast<PIPELINE_STATE>(
1227 std::make_shared<bp_state::Pipeline>(this, pCreateInfo, std::move(render_pass), std::move(layout)));
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001228}
1229
Sam Walls0961ec02020-03-31 16:39:15 +01001230void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1231 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1232 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1233 VkResult result, void* cgpl_state_data) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001234 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001235 pipeline_cache_ = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001236}
1237
Camden5b184be2019-08-13 07:50:19 -06001238bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1239 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001240 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001241 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001242 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1243 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001244
1245 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001246 skip |= LogPerformanceWarning(
1247 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1248 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1249 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001250 }
1251
Nadav Gevaf0808442021-05-21 13:51:25 -04001252 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001253 auto prev_pipeline = pipeline_cache_.load();
1254 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
1255 skip |= LogPerformanceWarning(
1256 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1257 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
Nadav Gevaf0808442021-05-21 13:51:25 -04001258 "improve cache hit rate",
1259 VendorSpecificTag(kBPVendorAMD));
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001260 }
1261 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001262
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001263 for (uint32_t i = 0; i < createInfoCount; i++) {
1264 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1265 if (VendorCheckEnabled(kBPVendorArm)) {
1266 skip |= ValidateCreateComputePipelineArm(createInfo);
1267 }
sfricke-samsung86d055a2022-02-11 14:43:50 -08001268
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001269 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1270 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1271 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1272 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1273 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1274 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1275 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1276 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1277 i);
sfricke-samsung86d055a2022-02-11 14:43:50 -08001278 }
1279 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001280 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001281 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001282
1283 return skip;
1284}
1285
1286bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1287 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001288 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001289 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001290 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1291 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001292
1293 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001294 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001295
1296 uint32_t thread_count = x * y * z;
1297
1298 // Generate a priori warnings about work group sizes.
1299 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1300 skip |= LogPerformanceWarning(
1301 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1302 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1303 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1304 "groups with less than %u threads, especially when using barrier() or shared memory.",
1305 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1306 }
1307
1308 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1309 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1310 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1311 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1312 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1313 "%u, %u) is not aligned to %u "
1314 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1315 "leave threads idle on the shader "
1316 "core.",
1317 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1318 kThreadGroupDispatchCountAlignmentArm);
1319 }
1320
sfricke-samsungef15e482022-01-26 11:32:49 -08001321 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1322 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001323
1324 unsigned dimensions = 0;
1325 if (x > 1) dimensions++;
1326 if (y > 1) dimensions++;
1327 if (z > 1) dimensions++;
1328 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1329 dimensions = std::max(dimensions, 1u);
1330
1331 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1332 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1333 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1334 bool accesses_2d = false;
1335 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001336 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001337 if (dim < 0) continue;
1338 auto spvdim = spv::Dim(dim);
1339 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1340 }
1341
1342 if (accesses_2d && dimensions < 2) {
1343 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1344 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1345 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1346 "exhibiting poor spatial locality with respect to one or more shader resources.",
1347 VendorSpecificTag(kBPVendorArm), x, y, z);
1348 }
1349
Camden5b184be2019-08-13 07:50:19 -06001350 return skip;
1351}
1352
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001353bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001354 bool skip = false;
1355
1356 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001357 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1358 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001359 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001360 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1361 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001362 }
1363
1364 return skip;
1365}
1366
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001367bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1368 bool skip = false;
1369
1370 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1371 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1372 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1373 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1374 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1375 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1376 }
1377
1378 return skip;
1379}
1380
1381bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1382 bool skip = false;
1383 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1384
1385 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1386 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1387
1388 return skip;
1389}
1390
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001391void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001392 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1393 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1394 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1395 LogPerformanceWarning(
1396 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1397 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1398 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1399 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1400 "convenient opportunity.",
1401 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1402 }
1403 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001404
1405 // AMD best practice
1406 // end-of-frame cleanup
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001407 num_queue_submissions_ = 0;
1408 num_barriers_objects_ = 0;
1409 ClearPipelinesUsedInFrame();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001410}
1411
Jeff Bolz5c801d12019-10-09 10:38:45 -05001412bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1413 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001414 bool skip = false;
1415
1416 for (uint32_t submit = 0; submit < submitCount; submit++) {
1417 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1418 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1419 }
ziga-lunargc77f0c02022-04-18 00:15:16 +02001420 if (pSubmits[submit].signalSemaphoreCount == 0 && pSubmits[submit].pSignalSemaphores != nullptr) {
1421 skip |=
1422 LogWarning(device, kVUID_BestPractices_SemaphoreCount,
1423 "pSubmits[%" PRIu32 "].pSignalSemaphores is set, but pSubmits[%" PRIu32 "].signalSemaphoreCount is 0.",
1424 submit, submit);
1425 }
1426 if (pSubmits[submit].waitSemaphoreCount == 0 && pSubmits[submit].pWaitSemaphores != nullptr) {
1427 skip |= LogWarning(device, kVUID_BestPractices_SemaphoreCount,
1428 "pSubmits[%" PRIu32 "].pWaitSemaphores is set, but pSubmits[%" PRIu32 "].waitSemaphoreCount is 0.",
1429 submit, submit);
1430 }
Camden5b184be2019-08-13 07:50:19 -06001431 }
1432
1433 return skip;
1434}
1435
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001436bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1437 VkFence fence) const {
1438 bool skip = false;
1439
1440 for (uint32_t submit = 0; submit < submitCount; submit++) {
1441 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1442 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1443 }
1444 }
1445
1446 return skip;
1447}
1448
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001449bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1450 VkFence fence) const {
1451 bool skip = false;
1452
1453 for (uint32_t submit = 0; submit < submitCount; submit++) {
1454 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1455 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1456 }
1457 }
1458
1459 return skip;
1460}
1461
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001462bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1463 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1464 bool skip = false;
1465
1466 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1467 skip |= LogPerformanceWarning(
1468 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1469 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1470 "pool instead.");
1471 }
1472
1473 return skip;
1474}
1475
1476bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1477 const VkCommandBufferBeginInfo* pBeginInfo) const {
1478 bool skip = false;
1479
1480 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1481 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1482 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1483 }
1484
Rodrigo Locattife5172b2022-03-22 18:49:29 -03001485 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorNVIDIA)) {
1486 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) {
1487 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
1488 "%s %s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1489 "For best performance on Mali and NVIDIA GPUs, consider setting ONE_TIME_SUBMIT by default.",
1490 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorNVIDIA));
1491 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001492 }
1493
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001494 return skip;
1495}
1496
Jeff Bolz5c801d12019-10-09 10:38:45 -05001497bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001498 bool skip = false;
1499
1500 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1501
1502 return skip;
1503}
1504
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001505bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1506 const VkDependencyInfoKHR* pDependencyInfo) const {
1507 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1508}
1509
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001510bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1511 const VkDependencyInfo* pDependencyInfo) const {
1512 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1513}
1514
Jeff Bolz5c801d12019-10-09 10:38:45 -05001515bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1516 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001517 bool skip = false;
1518
1519 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1520
1521 return skip;
1522}
1523
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001524bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1525 VkPipelineStageFlags2KHR stageMask) const {
1526 bool skip = false;
1527
1528 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1529
1530 return skip;
1531}
1532
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001533bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1534 VkPipelineStageFlags2 stageMask) const {
1535 bool skip = false;
1536
1537 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1538
1539 return skip;
1540}
1541
Camden5b184be2019-08-13 07:50:19 -06001542bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1543 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1544 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1545 uint32_t bufferMemoryBarrierCount,
1546 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1547 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001548 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001549 bool skip = false;
1550
1551 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1552 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1553
1554 return skip;
1555}
1556
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001557bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1558 const VkDependencyInfoKHR* pDependencyInfos) const {
1559 bool skip = false;
1560 for (uint32_t i = 0; i < eventCount; i++) {
1561 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1562 }
1563
1564 return skip;
1565}
1566
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001567bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1568 const VkDependencyInfo* pDependencyInfos) const {
1569 bool skip = false;
1570 for (uint32_t i = 0; i < eventCount; i++) {
1571 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1572 }
1573
1574 return skip;
1575}
1576
Camden5b184be2019-08-13 07:50:19 -06001577bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1578 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1579 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1580 uint32_t bufferMemoryBarrierCount,
1581 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1582 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001583 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001584 bool skip = false;
1585
1586 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1587 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1588
ziga-lunargb65dbfb2022-03-19 18:45:09 +01001589 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
1590 if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
1591 IsImageLayoutReadOnly(pImageMemoryBarriers[i].newLayout)) {
1592 skip |= LogWarning(device, kVUID_BestPractices_TransitionUndefinedToReadOnly,
1593 "VkImageMemoryBarrier is being submitted with oldLayout VK_IMAGE_LAYOUT_UNDEFINED and the contents "
1594 "may be discarded, but the newLayout is %s, which is read only.",
1595 string_VkImageLayout(pImageMemoryBarriers[i].newLayout));
1596 }
1597 }
1598
Nadav Gevaf0808442021-05-21 13:51:25 -04001599 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001600 auto num = num_barriers_objects_.load();
1601 if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001602 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001603 "%s Performance warning: In this frame, %" PRIu32
1604 " barriers were already submitted. Barriers have a high cost and can "
1605 "stall the GPU. "
1606 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1607 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04001608 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001609 }
1610 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
1611 static constexpr std::array<VkImageLayout, 3> read_layouts = {
Nadav Gevaf0808442021-05-21 13:51:25 -04001612 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1613 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1614 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1615 };
1616
1617 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1618 // read to read barriers
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001619 const auto &image_barrier = pImageMemoryBarriers[i];
1620 bool old_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.oldLayout) != read_layouts.end();
1621 bool new_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.newLayout) != read_layouts.end();
1622
Nadav Gevaf0808442021-05-21 13:51:25 -04001623 if (old_is_read_layout && new_is_read_layout) {
1624 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001625 "%s %s Performance warning: Don't issue read-to-read barriers. "
1626 "Get the resource in the right state the first time you use it.",
1627 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04001628 }
1629
1630 // general with no storage
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001631 if (VendorCheckEnabled(kBPVendorAMD) && image_barrier.newLayout == VK_IMAGE_LAYOUT_GENERAL) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001632 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1633 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1634 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1635 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1636 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1637 VendorSpecificTag(kBPVendorAMD));
1638 }
1639 }
1640 }
1641 }
1642
Camden5b184be2019-08-13 07:50:19 -06001643 return skip;
1644}
1645
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001646bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1647 const VkDependencyInfoKHR* pDependencyInfo) const {
1648 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1649}
1650
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001651bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1652 const VkDependencyInfo* pDependencyInfo) const {
1653 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1654}
1655
Camden5b184be2019-08-13 07:50:19 -06001656bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001657 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001658 bool skip = false;
1659
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001660 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1661
1662 return skip;
1663}
1664
1665bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1666 VkQueryPool queryPool, uint32_t query) const {
1667 bool skip = false;
1668
1669 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001670
1671 return skip;
1672}
1673
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001674bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1675 VkQueryPool queryPool, uint32_t query) const {
1676 bool skip = false;
1677
1678 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1679
1680 return skip;
1681}
1682
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03001683void BestPractices::PreCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1684 VkPipeline pipeline) {
1685 StateTracker::PreCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1686
1687 auto pipeline_info = Get<PIPELINE_STATE>(pipeline);
1688 auto cb = Get<bp_state::CommandBuffer>(commandBuffer);
1689
1690 assert(pipeline_info);
1691 assert(cb);
1692
1693 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS && VendorCheckEnabled(kBPVendorNVIDIA)) {
1694 using TessGeometryMeshState = bp_state::CommandBufferStateNV::TessGeometryMesh::State;
1695 auto& tgm = cb->nv.tess_geometry_mesh;
1696
1697 // Make sure the message is only signaled once per command buffer
1698 tgm.threshold_signaled = tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA;
1699
1700 // Track pipeline switches with tessellation, geometry, and/or mesh shaders enabled, and disabled
1701 auto tgm_stages = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT |
1702 VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_TASK_BIT_NV | VK_SHADER_STAGE_MESH_BIT_NV;
1703 auto new_tgm_state = (pipeline_info->active_shaders & tgm_stages) != 0
1704 ? TessGeometryMeshState::Enabled
1705 : TessGeometryMeshState::Disabled;
1706 if (tgm.state != new_tgm_state && tgm.state != TessGeometryMeshState::Unknown) {
1707 tgm.num_switches++;
1708 }
1709 tgm.state = new_tgm_state;
1710 }
1711}
1712
Sam Walls0961ec02020-03-31 16:39:15 +01001713void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1714 VkPipeline pipeline) {
1715 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1716
Nadav Gevaf0808442021-05-21 13:51:25 -04001717 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001718 PipelineUsedInFrame(pipeline);
Nadav Gevaf0808442021-05-21 13:51:25 -04001719
Sam Walls0961ec02020-03-31 16:39:15 +01001720 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001721 auto pipeline_state = Get<bp_state::Pipeline>(pipeline);
Sam Walls0961ec02020-03-31 16:39:15 +01001722 // check for depth/blend state tracking
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001723 if (pipeline_state) {
1724 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001725 assert(cb_node);
1726 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001727
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001728 render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments;
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001729 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001730
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001731 const auto* blend_state = pipeline_state->ColorBlendState();
1732 const auto* stencil_state = pipeline_state->DepthStencilState();
Sam Walls0961ec02020-03-31 16:39:15 +01001733
1734 if (blend_state) {
1735 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001736 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001737 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1738 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001739 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001740 }
1741 }
1742 }
1743
1744 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001745 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001746
1747 if (stencil_state && stencil_state->depthTestEnable) {
1748 switch (stencil_state->depthCompareOp) {
1749 case VK_COMPARE_OP_EQUAL:
1750 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1751 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001752 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001753 break;
1754 default:
1755 break;
1756 }
1757 }
Sam Walls0961ec02020-03-31 16:39:15 +01001758 }
1759 }
1760}
1761
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001762static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1763 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1764 const auto& subpass_info = createInfo.pSubpasses[subpass];
1765 if (subpass_info.pResolveAttachments) {
1766 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1767 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1768 }
1769 }
1770 }
1771
1772 return false;
1773}
1774
Attilio Provenzano02859b22020-02-27 14:17:28 +00001775static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1776 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001777 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001778
1779 // If an attachment is ever used as a color attachment,
1780 // resolve attachment or depth stencil attachment,
1781 // it needs to exist on tile at some point.
1782
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001783 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1784 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001785 }
1786
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001787 if (subpass_info.pResolveAttachments) {
1788 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1789 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1790 }
1791 }
1792
1793 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001794 }
1795
1796 return false;
1797}
1798
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001799static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1800 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1801 return false;
1802 }
1803
1804 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001805 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001806
1807 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1808 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1809 return true;
1810 }
1811 }
1812 }
1813
1814 return false;
1815}
1816
Attilio Provenzano02859b22020-02-27 14:17:28 +00001817bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1818 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1819 bool skip = false;
1820
1821 if (!pRenderPassBegin) {
1822 return skip;
1823 }
1824
Gareth Webbdc6549a2021-06-16 03:52:24 +01001825 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1826 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1827 "This render pass has a zero-size render area. It cannot write to any attachments, "
1828 "and can only be used for side effects such as layout transitions.");
1829 }
1830
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001831 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001832 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001833 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001834 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001835 if (rpabi) {
1836 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1837 }
1838 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001839 // Check if any attachments have LOAD operation on them
1840 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001841 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001842
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001843 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001844 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001845 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001846 }
1847
1848 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001849 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001850 }
1851
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001852 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001853
1854 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001855 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1856 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001857 }
1858
1859 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
LawG47747b322022-02-23 16:12:10 +00001860 if (attachment_needs_readback && (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) {
1861 skip |=
1862 LogPerformanceWarning(device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
LawG4015be1c2022-03-01 10:37:52 +00001863 "%s %s: Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
LawG47747b322022-02-23 16:12:10 +00001864 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001865 "which will copy in total %u pixels (renderArea = "
LawG47747b322022-02-23 16:12:10 +00001866 "{ %" PRId32 ", %" PRId32 ", %" PRIu32 ", %" PRIu32 " }) to the tile buffer.",
1867 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), att,
1868 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1869 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1870 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001871 }
1872 }
paul-lunarg7089e272022-06-20 22:19:37 +02001873
1874 // Check if renderpass has at least one VK_ATTACHMENT_LOAD_OP_CLEAR
1875
1876 bool clearing = false;
1877
1878 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
1879 const auto& attachment = rp_state->createInfo.pAttachments[att];
1880
1881 if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1882 clearing = true;
1883 break;
1884 }
1885 }
1886
1887 // Check if there are ClearValues passed to BeginRenderPass even though no attachments will be cleared
1888 if (!clearing && pRenderPassBegin->clearValueCount > 0) {
1889 // Flag as warning because nothing will happen per spec, and pClearValues will be ignored
1890 skip |= LogWarning(
1891 device, kVUID_BestPractices_ClearValueWithoutLoadOpClear,
1892 "This render pass does not have VkRenderPassCreateInfo.pAttachments->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR "
1893 "but VkRenderPassBeginInfo.clearValueCount > 0. VkRenderPassBeginInfo.pClearValues will be ignored and no "
paul-lunarga0a149c2022-06-23 16:18:51 +02001894 "attachments will be cleared.");
paul-lunarg7089e272022-06-20 22:19:37 +02001895 }
paul-lunarga0a149c2022-06-23 16:18:51 +02001896
1897 // Check if there are more clearValues than attachments
1898 if(pRenderPassBegin->clearValueCount > rp_state->createInfo.attachmentCount) {
1899 // Flag as warning because the overflowing clearValues will be ignored and could even be undefined on certain platforms.
1900 // This could signal a bug and there seems to be no reason for this to happen on purpose.
1901 skip |= LogWarning(
1902 device, kVUID_BestPractices_ClearValueCountHigherThanAttachmentCount,
1903 "This render pass has VkRenderPassBeginInfo.clearValueCount > VkRenderPassCreateInfo.attachmentCount "
1904 "(%" PRIu32 " > %" PRIu32 ") and as such the clearValues that do not have a corresponding attachment will be ignored.",
1905 pRenderPassBegin->clearValueCount, rp_state->createInfo.attachmentCount);
1906 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001907 }
1908
1909 return skip;
1910}
1911
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001912void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1913 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001914 if (view) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001915 auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state);
1916 QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001917 }
1918}
1919
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001920void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1921 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001922 // If we're viewing a 3D slice, ignore base array layer.
1923 // The entire 3D subresource is accessed as one atomic unit.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001924 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 +02001925
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001926 const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001927 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001928 const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel;
1929 const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001930
1931 for (uint32_t layer = 0; layer < array_layers; layer++) {
1932 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001933 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1934 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001935 }
1936 }
1937}
1938
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001939void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1940 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) {
1941 const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001942 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001943
1944 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001945 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001946 }
1947}
1948
paul-lunarg5eb52062022-06-27 18:57:15 +02001949void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1950 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer, uint32_t mip_level) {
1951 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker&, const QUEUE_STATE&,
1952 const CMD_BUFFER_STATE&) -> bool {
1953 ValidateImageInQueue(function_name, *state, usage, array_layer, mip_level);
1954 return false;
1955 });
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001956}
1957
LawG44d414ba2022-02-23 15:35:41 +00001958void BestPractices::ValidateImageInQueueArmImg(const char* function_name, const bp_state::Image& image,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001959 IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001960 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001961 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001962 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 -07001963 !image.IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001964 LogPerformanceWarning(
1965 device, kVUID_BestPractices_RenderPass_RedundantStore,
LawG4015be1c2022-03-01 10:37:52 +00001966 "%s %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 +02001967 "image was used, it was written to with STORE_OP_STORE. "
1968 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1969 "architectures.",
LawG44d414ba2022-02-23 15:35:41 +00001970 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001971 } 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 +02001972 LogPerformanceWarning(
1973 device, kVUID_BestPractices_RenderPass_RedundantClear,
LawG4015be1c2022-03-01 10:37:52 +00001974 "%s %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 +02001975 "image was used, it was written to with vkCmdClear*Image(). "
1976 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
LawG44d414ba2022-02-23 15:35:41 +00001977 "tile-based architectures.",
1978 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001979 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001980 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1981 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001982 const char *last_cmd = nullptr;
1983 const char *vuid = nullptr;
1984 const char *suggestion = nullptr;
1985
1986 switch (last_usage) {
1987 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1988 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1989 last_cmd = "vkCmdBlitImage";
1990 suggestion =
1991 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1992 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1993 "which avoids the memory roundtrip.";
1994 break;
1995 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1996 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1997 last_cmd = "vkCmdClear*Image";
1998 suggestion =
1999 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
2000 "tile-based architectures. "
2001 "Use LOAD_OP_CLEAR instead to clear the image for free.";
2002 break;
2003 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
2004 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
2005 last_cmd = "vkCmdCopy*Image";
2006 suggestion =
2007 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
2008 "Rather than copying, just render the source image in a fragment shader in this render pass, "
2009 "which avoids the memory roundtrip.";
2010 break;
2011 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
2012 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
2013 last_cmd = "vkCmdResolveImage";
2014 suggestion =
2015 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
2016 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
2017 "which avoids the memory roundtrip.";
2018 break;
2019 default:
2020 break;
2021 }
2022
2023 LogPerformanceWarning(
2024 device, vuid,
LawG4015be1c2022-03-01 10:37:52 +00002025 "%s %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 +01002026 "time image was used, it was written to with %s. %s",
LawG44d414ba2022-02-23 15:35:41 +00002027 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level, last_cmd,
2028 suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002029 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002030}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002031
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002032void BestPractices::ValidateImageInQueue(const char* function_name, bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage,
2033 uint32_t array_layer, uint32_t mip_level) {
2034 auto last_usage = state.UpdateUsage(array_layer, mip_level, usage);
paul-lunarg5eb52062022-06-27 18:57:15 +02002035
2036 // When image was discarded with StoreOpDontCare but is now being read with LoadOpLoad
2037 if (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED &&
2038 usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE) {
2039 LogWarning(device, kVUID_BestPractices_StoreOpDontCareThenLoadOpLoad,
2040 "Trying to load an attachment with LOAD_OP_LOAD that was previously stored with STORE_OP_DONT_CARE. This may "
2041 "result in undefined behaviour.");
2042 }
2043
LawG44d414ba2022-02-23 15:35:41 +00002044 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) {
2045 ValidateImageInQueueArmImg(function_name, state, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002046 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002047}
2048
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002049void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) {
2050 cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(),
2051 cb.queue_submit_functions_after_render_pass.end());
2052 cb.queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002053}
2054
2055void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2056 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002057 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2058 if (cb_node) {
2059 AddDeferredQueueOperations(*cb_node);
2060 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002061}
2062
2063void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
2064 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002065 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2066 if (cb_node) {
2067 AddDeferredQueueOperations(*cb_node);
2068 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002069}
2070
2071void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
2072 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002073 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2074 if (cb_node) {
2075 AddDeferredQueueOperations(*cb_node);
2076 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002077}
2078
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002079void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
2080 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002081 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002082 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002083 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2084}
2085
2086void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
2087 const VkRenderPassBeginInfo* pRenderPassBegin,
2088 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2089 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2090 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2091}
2092
2093void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2094 const VkRenderPassBeginInfo* pRenderPassBegin,
2095 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2096 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2097 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2098}
2099
2100void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002101
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002102 if (!pRenderPassBegin) {
2103 return;
2104 }
2105
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002106 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002107
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002108 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002109 if (rp_state) {
2110 // Check load ops
2111 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002112 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002113
2114 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
2115 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
2116 continue;
2117 }
2118
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002119 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002120
2121 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
2122 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002123 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02002124 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
2125 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002126 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02002127 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002128 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002129 }
2130
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002131 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002132 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002133
Tony-LunarGb3ab3572021-07-02 09:45:17 -06002134 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002135 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
2136 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002137 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002138 }
2139 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002140 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002141 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002142
Jeremy Gebben9f537102021-10-05 16:37:12 -06002143 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002144 }
2145
2146 // Check store ops
2147 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002148 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002149
2150 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
2151 continue;
2152 }
2153
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002154 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002155
2156 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
2157 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002158 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002159 }
2160
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002161 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002162
Jeremy Gebben9f537102021-10-05 16:37:12 -06002163 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06002164 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002165 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
2166 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002167 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002168 }
2169 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002170 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002171 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002172
Jeremy Gebben9f537102021-10-05 16:37:12 -06002173 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002174 }
2175 }
2176}
2177
Attilio Provenzano02859b22020-02-27 14:17:28 +00002178bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2179 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002180 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2181 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002182 return skip;
2183}
2184
2185bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2186 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08002187 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002188 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2189 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002190 return skip;
2191}
2192
2193bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08002194 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002195 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2196 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002197 return skip;
2198}
2199
Sam Walls0961ec02020-03-31 16:39:15 +01002200void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
2201 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002202 // Reset the renderpass state
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002203 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
sjfricke52defd42022-08-08 16:37:46 +09002204 // TODO - move this logic to the Render Pass state as cb->has_draw_cmd should stay true for lifetime of command buffer
2205 cb->has_draw_cmd = false;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002206 assert(cb);
2207 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002208 render_pass_state.touchesAttachments.clear();
2209 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002210 render_pass_state.numDrawCallsDepthOnly = 0;
2211 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2212 render_pass_state.colorAttachment = false;
2213 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002214 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002215 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002216
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03002217 // Reset NV state
2218 cb->nv = {};
2219
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002220 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002221
2222 // track depth / color attachment usage within the renderpass
2223 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2224 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002225 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002226
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002227 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002228 }
2229}
2230
2231void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2232 VkSubpassContents contents) {
2233 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2234 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2235}
2236
2237void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2238 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2239 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2240 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2241}
2242
2243void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2244 const VkRenderPassBeginInfo* pRenderPassBegin,
2245 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2246 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2247 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2248}
2249
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002250// Generic function to handle validation for all CmdDraw* type functions
2251bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2252 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002253 const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002254 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002255 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2256 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002257 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002258
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002259 // Verify vertex binding
Tony-LunarG2ffe1f52022-04-11 15:13:30 -06002260 if (pipeline_state && pipeline_state->vertex_input_state &&
2261 pipeline_state->vertex_input_state->binding_descriptions.size() <= 0) {
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002262 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002263 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002264 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002265 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2266 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002267 }
2268 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002269
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002270 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002271 if (pipe) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002272 const auto& rp_state = pipe->RenderPassState();
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002273 if (rp_state) {
2274 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2275 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002276 const auto* ds_state = pipe->DepthStencilState();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002277 const uint32_t depth_stencil_attachment =
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002278 GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
2279 const auto* raster_state = pipe->RasterizationState();
2280 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && raster_state &&
2281 raster_state->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002282 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2283 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2284 }
2285 }
2286 }
2287 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002288 }
2289 return skip;
2290}
2291
Sam Walls0961ec02020-03-31 16:39:15 +01002292void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002293 auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002294 assert(cb_node);
Sam Walls0961ec02020-03-31 16:39:15 +01002295 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002296 RecordCmdDrawTypeArm(*cb_node, draw_count, caller);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002297 }
2298
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002299 if (cb_node->render_pass_state.drawTouchAttachments) {
2300 for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) {
2301 RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002302 }
2303 // No need to touch the same attachments over and over.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002304 cb_node->render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002305 }
2306}
2307
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002308void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) {
2309 auto& render_pass_state = cb_node.render_pass_state;
LawG4b21485c2022-02-28 13:46:48 +00002310 // Each TBDR vendor requires a depth pre-pass draw call to have a minimum number of vertices/indices before it counts towards
2311 // depth prepass warnings First find the lowest enabled draw count
2312 uint32_t lowestEnabledMinDrawCount = 0;
2313 lowestEnabledMinDrawCount = VendorCheckEnabled(kBPVendorArm) * kDepthPrePassMinDrawCountArm;
2314 if (VendorCheckEnabled(kBPVendorIMG) && kDepthPrePassMinDrawCountIMG < lowestEnabledMinDrawCount)
2315 lowestEnabledMinDrawCount = kDepthPrePassMinDrawCountIMG;
2316
2317 if (draw_count >= lowestEnabledMinDrawCount) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002318 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2319 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002320 }
2321}
2322
Camden5b184be2019-08-13 07:50:19 -06002323bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002324 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002325 bool skip = false;
2326
2327 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002328 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2329 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002330 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002331 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002332
2333 return skip;
2334}
2335
Sam Walls0961ec02020-03-31 16:39:15 +01002336void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2337 uint32_t firstVertex, uint32_t firstInstance) {
2338 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2339 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2340}
2341
Camden5b184be2019-08-13 07:50:19 -06002342bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002343 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002344 bool skip = false;
2345
2346 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002347 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2348 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002349 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002350 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2351
Attilio Provenzano02859b22020-02-27 14:17:28 +00002352 // Check if we reached the limit for small indexed draw calls.
2353 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002354 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002355 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002356 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
LawG4ff42d722022-03-01 10:28:25 +00002357 (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002358 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
LawG4ff42d722022-03-01 10:28:25 +00002359 "%s %s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002360 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2361 "You can try batching drawcalls or instancing when applicable.",
LawG4ff42d722022-03-01 10:28:25 +00002362 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), kMaxSmallIndexedDrawcalls,
2363 kSmallIndexedDrawcallIndices);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002364 }
2365
Sam Walls8e77e4f2020-03-16 20:47:40 +00002366 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002367 ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002368 }
2369
2370 return skip;
2371}
2372
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002373bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount,
Sam Walls8e77e4f2020-03-16 20:47:40 +00002374 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2375 bool skip = false;
2376
2377 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Sam Walls8e77e4f2020-03-16 20:47:40 +00002378
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002379 const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get();
2380 if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002381
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002382 const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002383 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002384 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2385 const void* ib_mem = ib_mem_state.p_driver_data;
2386 bool primitive_restart_enable = false;
2387
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002388 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002389 const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002390 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002391
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002392 const auto* ia_state = pipeline_state ? pipeline_state->InputAssemblyState() : nullptr;
2393 if (ia_state) {
2394 primitive_restart_enable = ia_state->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002395 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002396
2397 // 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 -06002398 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002399 uint32_t scan_stride;
2400 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2401 scan_stride = sizeof(uint8_t);
2402 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2403 scan_stride = sizeof(uint16_t);
2404 } else {
2405 scan_stride = sizeof(uint32_t);
2406 }
2407
2408 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2409 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2410
2411 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2412 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2413 // irrespective of whether or not they're part of the draw call.
2414
2415 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2416 uint32_t min_index = ~0u;
2417 // start with maximum as 0 and adjust to indices in the buffer
2418 uint32_t max_index = 0u;
2419
2420 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2421 // for the given index buffer
2422 uint32_t vertex_shade_count = 0;
2423
2424 PostTransformLRUCacheModel post_transform_cache;
2425
2426 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2427 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2428 // target architecture.
2429 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2430 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2431 post_transform_cache.resize(32);
2432
2433 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2434 uint32_t scan_index;
2435 uint32_t primitive_restart_value;
2436 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2437 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2438 primitive_restart_value = 0xFF;
2439 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2440 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2441 primitive_restart_value = 0xFFFF;
2442 } else {
2443 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2444 primitive_restart_value = 0xFFFFFFFF;
2445 }
2446
2447 max_index = std::max(max_index, scan_index);
2448 min_index = std::min(min_index, scan_index);
2449
2450 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2451 bool in_cache = post_transform_cache.query_cache(scan_index);
2452 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2453 if (!in_cache) vertex_shade_count++;
2454 }
2455 }
2456
2457 // 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 +01002458 // 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
2459 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002460
2461 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002462 skip |=
2463 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2464 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2465 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2466 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2467 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2468 VendorSpecificTag(kBPVendorArm),
2469 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002470 return skip;
2471 }
2472
2473 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2474 // 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 +01002475 const size_t refs_per_bucket = 64;
2476 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2477
2478 const uint32_t n_indices = max_index - min_index + 1;
2479 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2480 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2481
2482 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2483 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002484
2485 // To avoid using too much memory, we run over the indices again.
2486 // Knowing the size from the last scan allows us to record index usage with bitsets
2487 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2488 uint32_t scan_index;
2489 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2490 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2491 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2492 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2493 } else {
2494 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2495 }
2496 // keep track of the set of all indices used to reference vertices in the draw call
2497 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002498 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2499 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002500 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2501 }
2502
2503 uint32_t vertex_reference_count = 0;
2504 for (const auto& bitset : vertex_reference_buckets) {
2505 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2506 }
2507
2508 // 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 -07002509 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002510 // 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 -07002511 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002512
2513 if (utilization < 0.5f) {
2514 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2515 "%s The indices which were specified for the draw call only utilise approximately "
2516 "%.02f%% of the bound vertex buffer.",
2517 VendorSpecificTag(kBPVendorArm), utilization);
2518 }
2519
2520 if (cache_hit_rate <= 0.5f) {
2521 skip |=
2522 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2523 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2524 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2525 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2526 "recently shaded vertices.",
2527 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2528 }
2529 }
2530
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002531 return skip;
2532}
2533
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002534bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2535 const VkCommandBuffer* pCommandBuffers) const {
2536 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002537 const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002538 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002539 const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002540 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002541 continue;
2542 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002543 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002544 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002545 if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) {
2546 skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002547 }
2548 }
2549 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002550
2551 if (VendorCheckEnabled(kBPVendorAMD)) {
2552 if (commandBufferCount > 0) {
2553 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2554 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2555 VendorSpecificTag(kBPVendorAMD));
2556 }
2557 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002558 return skip;
2559}
2560
2561void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2562 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002563 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2564
2565 auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2566 if (!primary) {
2567 return;
2568 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002569
2570 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002571 auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]);
2572 if (!secondary) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002573 continue;
2574 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002575
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002576 for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) {
2577 if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2578 RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment,
2579 early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002580 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002581 RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002582 }
2583 }
2584
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002585 for (auto& touch : secondary->render_pass_state.touchesAttachments) {
2586 RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002587 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002588
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002589 primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare;
2590 primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002591 }
2592
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002593}
2594
Rodrigo Locatti7d716e12022-03-09 19:15:17 -03002595bool BestPractices::PreCallValidateCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer,
2596 const VkAccelerationStructureInfoNV* pInfo,
2597 VkBuffer instanceData, VkDeviceSize instanceOffset,
2598 VkBool32 update, VkAccelerationStructureNV dst,
2599 VkAccelerationStructureNV src, VkBuffer scratch,
2600 VkDeviceSize scratchOffset) const {
2601 return ValidateBuildAccelerationStructure(commandBuffer);
2602}
2603
2604bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresIndirectKHR(
2605 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
2606 const VkDeviceAddress* pIndirectDeviceAddresses, const uint32_t* pIndirectStrides,
2607 const uint32_t* const* ppMaxPrimitiveCounts) const {
2608 return ValidateBuildAccelerationStructure(commandBuffer);
2609}
2610
2611bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresKHR(
2612 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
2613 const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) const {
2614 return ValidateBuildAccelerationStructure(commandBuffer);
2615}
2616
2617bool BestPractices::ValidateBuildAccelerationStructure(VkCommandBuffer commandBuffer) const {
2618 bool skip = false;
2619 auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
2620 assert(cb_node);
2621
2622 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
2623 if ((cb_node->GetQueueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) {
2624 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_AccelerationStructure_NotAsync,
2625 "%s Performance warning: Prefer building acceleration structures on an asynchronous "
2626 "compute queue, instead of using the universal graphics queue.",
2627 VendorSpecificTag(kBPVendorNVIDIA));
2628 }
2629 }
2630
2631 return skip;
2632}
2633
Rodrigo Locatti66b23352022-03-15 17:28:32 -03002634bool BestPractices::ValidateBindMemory(VkDevice device, VkDeviceMemory memory) const {
2635 bool skip = false;
2636
2637 if (VendorCheckEnabled(kBPVendorNVIDIA) && device_extensions.vk_ext_pageable_device_local_memory) {
2638 auto mem_info = std::static_pointer_cast<const bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory));
2639 if (!mem_info->dynamic_priority) {
2640 skip |=
2641 LogPerformanceWarning(device, kVUID_BestPractices_BindMemory_NoPriority,
2642 "%s Use vkSetDeviceMemoryPriorityEXT to provide the OS with information on which allocations "
2643 "should stay in memory and which should be demoted first when video memory is limited. The "
2644 "highest priority should be given to GPU-written resources like color attachments, depth "
2645 "attachments, storage images, and buffers written from the GPU.",
2646 VendorSpecificTag(kBPVendorNVIDIA));
2647 }
2648 }
2649
2650 return skip;
2651}
2652
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002653void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2654 auto& state = cb_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002655 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002656 auto itr =
2657 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2658 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002659
2660 if (itr != state.touchesAttachments.end()) {
2661 itr->aspects |= aspects;
2662 } else {
2663 state.touchesAttachments.push_back({ fb_attachment, aspects });
2664 }
2665}
2666
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002667void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002668 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2669 const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002670 auto& state = cmd_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002671 // If we observe a full clear before any other access to a frame buffer attachment,
2672 // we have candidate for redundant clear attachments.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002673 auto itr =
2674 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2675 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002676
2677 uint32_t new_aspects = aspects;
2678 if (itr != state.touchesAttachments.end()) {
2679 new_aspects = aspects & ~itr->aspects;
2680 itr->aspects |= aspects;
2681 } else {
2682 state.touchesAttachments.push_back({ fb_attachment, aspects });
2683 }
2684
2685 if (new_aspects == 0) {
2686 return;
2687 }
2688
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002689 if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002690 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2691 // CmdExecuteCommands.
2692 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2693 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2694 }
2695}
2696
2697void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2698 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2699 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002700 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects);
2701
2702 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2703 auto* rp_state = cmd_state->activeRenderPass.get();
2704 auto* fb_state = cmd_state->activeFramebuffer.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002705 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2706
2707 if (rectCount == 0 || !rp_state) {
2708 return;
2709 }
2710
2711 if (!is_secondary && !fb_state) {
2712 return;
2713 }
2714
2715 // 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 -07002716 bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002717
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06002718 if (!rp_state->UsesDynamicRendering()) {
ziga-lunarg885c6542022-03-07 01:08:25 +01002719 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2720 for (uint32_t i = 0; i < attachmentCount; i++) {
2721 auto& attachment = pClearAttachments[i];
2722 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2723 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002724
ziga-lunarg885c6542022-03-07 01:08:25 +01002725 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2726 if (subpass.pDepthStencilAttachment) {
2727 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2728 }
2729 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2730 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002731 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002732
ziga-lunarg885c6542022-03-07 01:08:25 +01002733 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2734 if (full_clear) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002735 RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment,
ziga-lunarg885c6542022-03-07 01:08:25 +01002736 aspects, rectCount, pRects);
2737 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002738 RecordAttachmentAccess(*cmd_state, fb_attachment, aspects);
ziga-lunarg885c6542022-03-07 01:08:25 +01002739 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002740 }
2741 }
2742 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002743}
2744
Attilio Provenzano02859b22020-02-27 14:17:28 +00002745void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2746 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2747 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2748 firstInstance);
2749
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002750 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002751 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2752 cmd_state->small_indexed_draw_call_count++;
2753 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002754
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002755 ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002756}
2757
Sam Walls0961ec02020-03-31 16:39:15 +01002758void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2759 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2760 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2761 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2762}
2763
Camden5b184be2019-08-13 07:50:19 -06002764bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002765 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002766 bool skip = false;
2767
2768 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002769 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2770 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002771 }
2772
Rodrigo Locatti8419cde2022-03-30 18:45:13 -03002773 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
2774
Camden5b184be2019-08-13 07:50:19 -06002775 return skip;
2776}
2777
Sam Walls0961ec02020-03-31 16:39:15 +01002778void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2779 uint32_t count, uint32_t stride) {
2780 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2781 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2782}
2783
Camden5b184be2019-08-13 07:50:19 -06002784bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002785 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002786 bool skip = false;
2787
2788 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002789 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2790 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002791 }
2792
Rodrigo Locatti8419cde2022-03-30 18:45:13 -03002793 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
2794
Camden5b184be2019-08-13 07:50:19 -06002795 return skip;
2796}
2797
Sam Walls0961ec02020-03-31 16:39:15 +01002798void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2799 uint32_t count, uint32_t stride) {
2800 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2801 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2802}
2803
Rodrigo Locatti467344a2022-03-30 18:48:13 -03002804bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2805 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2806 uint32_t maxDrawCount, uint32_t stride) const {
2807 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2808
2809 return skip;
2810}
2811
2812void BestPractices::PostCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2813 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2814 uint32_t maxDrawCount, uint32_t stride) {
2815 StateTracker::PostCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
2816 maxDrawCount, stride);
2817 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCount()");
2818}
2819
2820bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
2821 VkDeviceSize offset, VkBuffer countBuffer,
2822 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2823 uint32_t stride) const {
2824 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountAMD");
2825
2826 return skip;
2827}
2828
2829void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
2830 VkDeviceSize offset, VkBuffer countBuffer,
2831 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2832 uint32_t stride) {
2833 StateTracker::PostCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
2834 maxDrawCount, stride);
2835 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountAMD()");
2836}
2837
2838bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2839 VkDeviceSize offset, VkBuffer countBuffer,
2840 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2841 uint32_t stride) const {
2842 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR");
2843
2844 return skip;
2845}
2846
2847void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2848 VkDeviceSize offset, VkBuffer countBuffer,
2849 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2850 uint32_t stride) {
2851 StateTracker::PostCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
2852 maxDrawCount, stride);
2853 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountKHR()");
2854}
2855
2856bool BestPractices::PreCallValidateCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount,
2857 uint32_t firstInstance, VkBuffer counterBuffer,
2858 VkDeviceSize counterBufferOffset, uint32_t counterOffset,
2859 uint32_t vertexStride) const {
2860 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectByteCountEXT");
2861
2862 return skip;
2863}
2864
2865void BestPractices::PostCallRecordCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount,
2866 uint32_t firstInstance, VkBuffer counterBuffer,
2867 VkDeviceSize counterBufferOffset, uint32_t counterOffset,
2868 uint32_t vertexStride) {
2869 StateTracker::PostCallRecordCmdDrawIndirectByteCountEXT(commandBuffer, instanceCount, firstInstance, counterBuffer,
2870 counterBufferOffset, counterOffset, vertexStride);
2871 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectByteCountEXT()");
2872}
2873
2874bool BestPractices::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2875 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2876 uint32_t stride) const {
2877 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCount");
2878
2879 return skip;
2880}
2881
2882void BestPractices::PostCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2883 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2884 uint32_t stride) {
2885 StateTracker::PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
2886 stride);
2887 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCount()");
2888}
2889
2890bool BestPractices::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2891 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2892 uint32_t maxDrawCount, uint32_t stride) const {
2893 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountAMD");
2894
2895 return skip;
2896}
2897
2898void BestPractices::PostCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2899 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2900 uint32_t maxDrawCount, uint32_t stride) {
2901 StateTracker::PostCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
2902 stride);
2903 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountAMD()");
2904}
2905
2906bool BestPractices::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2907 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2908 uint32_t maxDrawCount, uint32_t stride) const {
2909 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountKHR");
2910
2911 return skip;
2912}
2913
2914void BestPractices::PostCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2915 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2916 uint32_t maxDrawCount, uint32_t stride) {
2917 StateTracker::PostCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
2918 stride);
2919 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountKHR()");
2920}
2921
2922bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
2923 VkDeviceSize offset, VkBuffer countBuffer,
2924 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2925 uint32_t stride) const {
2926 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectCountNV");
2927
2928 return skip;
2929}
2930
2931void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
2932 VkDeviceSize offset, VkBuffer countBuffer,
2933 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2934 uint32_t stride) {
2935 StateTracker::PostCallRecordCmdDrawMeshTasksIndirectCountNV(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
2936 maxDrawCount, stride);
2937 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectCountNV()");
2938}
2939
2940bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2941 uint32_t drawCount, uint32_t stride) const {
2942 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectNV");
2943
2944 return skip;
2945}
2946
2947void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2948 uint32_t drawCount, uint32_t stride) {
2949 StateTracker::PostCallRecordCmdDrawMeshTasksIndirectNV(commandBuffer, buffer, offset, drawCount, stride);
2950 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectNV()");
2951}
2952
2953bool BestPractices::PreCallValidateCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) const {
2954 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksNV");
2955
2956 return skip;
2957}
2958
2959void BestPractices::PostCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) {
2960 StateTracker::PostCallRecordCmdDrawMeshTasksNV(commandBuffer, taskCount, firstTask);
2961 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksNV()");
2962}
2963
2964bool BestPractices::PreCallValidateCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
2965 const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount,
2966 uint32_t firstInstance, uint32_t stride,
2967 const int32_t* pVertexOffset) const {
2968 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiIndexedEXT");
2969
2970 return skip;
2971}
2972
2973void BestPractices::PostCallRecordCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
2974 const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount,
2975 uint32_t firstInstance, uint32_t stride, const int32_t* pVertexOffset) {
2976 StateTracker::PostCallRecordCmdDrawMultiIndexedEXT(commandBuffer, drawCount, pIndexInfo, instanceCount, firstInstance, stride,
2977 pVertexOffset);
2978 uint32_t count = 0;
2979 for (uint32_t i = 0; i < drawCount; ++i) {
2980 count += pIndexInfo[i].indexCount;
2981 }
2982 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiIndexedEXT()");
2983}
2984
2985bool BestPractices::PreCallValidateCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, const VkMultiDrawInfoEXT* pVertexInfo,
2986 uint32_t instanceCount, uint32_t firstInstance, uint32_t stride) const {
2987 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiEXT");
2988
2989 return skip;
2990}
2991
2992void BestPractices::PostCallRecordCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
2993 const VkMultiDrawInfoEXT* pVertexInfo, uint32_t instanceCount,
2994 uint32_t firstInstance, uint32_t stride) {
2995 StateTracker::PostCallRecordCmdDrawMultiEXT(commandBuffer, drawCount, pVertexInfo, instanceCount, firstInstance, stride);
2996 uint32_t count = 0;
2997 for (uint32_t i = 0; i < drawCount; ++i) {
2998 count += pVertexInfo[i].vertexCount;
2999 }
3000 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiEXT()");
3001}
3002
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003003void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) {
3004 for (auto descriptor_set : cb_state.validated_descriptor_sets) {
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003005 for (const auto& binding : *descriptor_set) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003006 // For bindless scenarios, we should not attempt to track descriptor set state.
3007 // It is highly uncertain which resources are actually bound.
3008 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003009 if (binding->binding_flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
3010 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003011 continue;
3012 }
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01003013
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003014 for (uint32_t i = 0; i < binding->count; ++i) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003015 VkImageView image_view{VK_NULL_HANDLE};
3016
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003017 auto descriptor = binding->GetDescriptor(i);
ziga-lunarg33d806c2022-05-05 17:00:52 +02003018 if (!descriptor) {
3019 continue;
3020 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003021 switch (descriptor->GetClass()) {
3022 case cvdescriptorset::DescriptorClass::Image: {
3023 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
3024 image_view = image_descriptor->GetImageView();
3025 }
3026 break;
3027 }
3028 case cvdescriptorset::DescriptorClass::ImageSampler: {
3029 if (const auto image_sampler_descriptor =
3030 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
3031 image_view = image_sampler_descriptor->GetImageView();
3032 }
3033 break;
3034 }
3035 default:
3036 break;
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01003037 }
3038
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003039 if (image_view) {
3040 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
3041 QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(),
3042 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003043 }
3044 }
3045 }
3046 }
3047}
3048
3049void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3050 uint32_t firstVertex, uint32_t firstInstance) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003051 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3052 ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003053}
3054
3055void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3056 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003057 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3058 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003059}
3060
3061void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3062 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003063 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3064 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003065}
3066
Camden5b184be2019-08-13 07:50:19 -06003067bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003068 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06003069 bool skip = false;
3070
3071 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003072 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
3073 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
3074 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
3075 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06003076 }
3077
3078 return skip;
3079}
Camden83a9c372019-08-14 11:41:38 -06003080
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003081bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
3082 bool skip = false;
3083 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3084 skip |= ValidateCmdEndRenderPass(commandBuffer);
3085 return skip;
3086}
3087
3088bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
3089 bool skip = false;
3090 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3091 skip |= ValidateCmdEndRenderPass(commandBuffer);
3092 return skip;
3093}
3094
Sam Walls0961ec02020-03-31 16:39:15 +01003095bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3096 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01003097 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003098 skip |= ValidateCmdEndRenderPass(commandBuffer);
3099 return skip;
3100}
3101
3102bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3103 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003104 const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01003105
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003106 if (cmd == nullptr) return skip;
3107 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01003108
LawG4b21485c2022-02-28 13:46:48 +00003109 // Does the number of draw calls classified as depth only surpass the vendor limit for a specified vendor
3110 bool depth_only_arm = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
3111 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
3112 bool depth_only_img = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsIMG &&
3113 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsIMG;
3114
3115 // Only send the warning when the vendor is enabled and a depth prepass is detected
LawG498ec4502022-04-05 09:08:25 +01003116 bool uses_depth =
3117 (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
LawG45507e142022-04-08 09:36:54 +01003118 ((depth_only_arm && VendorCheckEnabled(kBPVendorArm)) || (depth_only_img && VendorCheckEnabled(kBPVendorIMG)));
LawG4b21485c2022-02-28 13:46:48 +00003119
Sam Walls0961ec02020-03-31 16:39:15 +01003120 if (uses_depth) {
3121 skip |= LogPerformanceWarning(
3122 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
LawG4015be1c2022-03-01 10:37:52 +00003123 "%s %s: Depth pre-passes may be in use. In general, this is not recommended in tile-based deferred "
LawG4b21485c2022-02-28 13:46:48 +00003124 "renderering architectures; such as those in Arm Mali or PowerVR GPUs. Since they can remove geometry "
3125 "hidden by other opaque geometry. Mali has Forward Pixel Killing (FPK), PowerVR has Hiden Surface "
3126 "Remover (HSR) in which case, using depth pre-passes for hidden surface removal may worsen performance.",
3127 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG));
Sam Walls0961ec02020-03-31 16:39:15 +01003128 }
3129
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003130 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3131
LawG40da9c3d2022-03-01 09:51:01 +00003132 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) && rp) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003133 // If we use an attachment on-tile, we should access it in some way. Otherwise,
3134 // it is redundant to have it be part of the render pass.
3135 // Only consider it redundant if it will actually consume bandwidth, i.e.
3136 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
3137 // as is using pure input attachments.
3138 // CLEAR -> STORE might be considered a "useful" thing to do, but
3139 // the optimal thing to do is to defer the clear until you're actually
3140 // going to render to the image.
3141
3142 uint32_t num_attachments = rp->createInfo.attachmentCount;
3143 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02003144 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
3145 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003146 continue;
3147 }
3148
3149 auto& attachment = rp->createInfo.pAttachments[i];
3150
3151 VkImageAspectFlags bandwidth_aspects = 0;
3152
3153 if (!FormatIsStencilOnly(attachment.format) &&
3154 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
3155 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
3156 if (FormatHasDepth(attachment.format)) {
3157 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
3158 } else {
3159 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
3160 }
3161 }
3162
3163 if (FormatHasStencil(attachment.format) &&
3164 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
3165 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
3166 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
3167 }
3168
3169 if (!bandwidth_aspects) {
3170 continue;
3171 }
3172
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003173 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003174 [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003175 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003176 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003177 untouched_aspects &= ~itr->aspects;
3178 }
3179
3180 if (untouched_aspects) {
3181 skip |= LogPerformanceWarning(
3182 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
LawG4015be1c2022-03-01 10:37:52 +00003183 "%s %s: Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003184 "was never accessed by a pipeline or clear command. "
LawG40da9c3d2022-03-01 09:51:01 +00003185 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the "
LawG4015be1c2022-03-01 10:37:52 +00003186 "render pass if the attachments are not intended to be accessed.",
LawG40da9c3d2022-03-01 09:51:01 +00003187 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), i, attachment.format, untouched_aspects);
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003188 }
3189 }
3190 }
3191
Sam Walls0961ec02020-03-31 16:39:15 +01003192 return skip;
3193}
3194
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003195void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003196 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3197 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003198}
3199
3200void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003201 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3202 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003203}
3204
Camden Stocker9c051442019-11-06 14:28:43 -08003205bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
3206 const char* api_name) const {
3207 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003208 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08003209
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003210 if (bp_pd_state) {
3211 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
3212 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
3213 "Potential problem with calling %s() without first retrieving properties from "
3214 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
3215 api_name);
3216 }
Camden Stocker9c051442019-11-06 14:28:43 -08003217 }
3218
3219 return skip;
3220}
3221
Camden83a9c372019-08-14 11:41:38 -06003222bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003223 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06003224 bool skip = false;
3225
Camden Stocker9c051442019-11-06 14:28:43 -08003226 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06003227
Camden Stocker9c051442019-11-06 14:28:43 -08003228 return skip;
3229}
3230
3231bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
3232 uint32_t planeIndex,
3233 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
3234 bool skip = false;
3235
3236 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
3237
3238 return skip;
3239}
3240
3241bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
3242 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
3243 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
3244 bool skip = false;
3245
3246 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06003247
3248 return skip;
3249}
Camden05de2d42019-08-19 10:23:56 -06003250
3251bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003252 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06003253 bool skip = false;
3254
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003255 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06003256
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003257 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06003258 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003259 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003260 skip |=
3261 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
3262 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
3263 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06003264 }
Camden05de2d42019-08-19 10:23:56 -06003265
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06003266 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
3267 skip |= LogWarning(
3268 device, kVUID_BestPractices_Swapchain_InvalidCount,
3269 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04003270 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06003271 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
3272 }
3273 }
3274
Camden05de2d42019-08-19 10:23:56 -06003275 return skip;
3276}
3277
3278// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06003279bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003280 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003281 const CALL_STATE call_state,
3282 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06003283 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003284 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
3285 if (UNCALLED == call_state) {
3286 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06003287 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003288 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
3289 "recommended "
3290 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
3291 caller_name, caller_name);
3292 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06003293 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
3294 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003295 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
3296 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
3297 ". It is recommended to instead receive all the properties by calling %s with "
3298 "pQueueFamilyPropertyCount that was "
3299 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06003300 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003301 caller_name);
Camden05de2d42019-08-19 10:23:56 -06003302 }
3303
3304 return skip;
3305}
3306
Jeff Bolz5c801d12019-10-09 10:38:45 -05003307bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
3308 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06003309 bool skip = false;
3310
3311 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003312 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06003313 if (!as_state->memory_requirements_checked) {
3314 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
3315 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
3316 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003317 skip |= LogWarning(
3318 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06003319 "vkBindAccelerationStructureMemoryNV(): "
3320 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
3321 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
3322 }
3323 }
3324
3325 return skip;
3326}
3327
Camden05de2d42019-08-19 10:23:56 -06003328bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3329 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003330 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003331 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003332 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003333 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003334 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3335 "vkGetPhysicalDeviceQueueFamilyProperties()");
3336 }
3337 return false;
Camden05de2d42019-08-19 10:23:56 -06003338}
3339
Mike Schuchardt2df08912020-12-15 16:28:09 -08003340bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3341 uint32_t* pQueueFamilyPropertyCount,
3342 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003343 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003344 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003345 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003346 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3347 "vkGetPhysicalDeviceQueueFamilyProperties2()");
3348 }
3349 return false;
Camden05de2d42019-08-19 10:23:56 -06003350}
3351
Jeff Bolz5c801d12019-10-09 10:38:45 -05003352bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08003353 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003354 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003355 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003356 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003357 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3358 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
3359 }
3360 return false;
Camden05de2d42019-08-19 10:23:56 -06003361}
3362
3363bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3364 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003365 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06003366 if (!pSurfaceFormats) return false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003367 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003368 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06003369 bool skip = false;
3370 if (call_state == UNCALLED) {
3371 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
3372 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003373 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
3374 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
3375 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06003376 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003377 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003378 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
3379 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
3380 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
3381 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003382 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06003383 }
3384 }
3385 return skip;
3386}
Camden Stocker23cc47d2019-09-03 14:53:57 -06003387
3388bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003389 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003390 bool skip = false;
3391
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003392 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3393 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06003394 // 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 -07003395 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003396 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
3397 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07003398 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06003399 // 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 -07003400 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
3401 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003402 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003403 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003404 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003405 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06003406 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003407 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
3408 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
3409 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003410 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003411 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
3412 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003413 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003414 }
3415 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003416 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003417 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003418 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003419 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
3420 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003421 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003422 }
3423 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003424 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3425 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003426 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003427 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003428 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003429 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06003430 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003431 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
3432 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
3433 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003434 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003435 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
3436 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003437 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003438 }
3439 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003440 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003441 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003442 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003443 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
3444 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003445 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003446 }
3447 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3448 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003449 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003450 }
3451 }
3452 }
3453 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003454 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
3455 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003456 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003457 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003458 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
3459 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003460 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003461 }
3462 }
3463 }
3464
Rodrigo Locatti7ab778d2022-03-09 18:57:15 -03003465 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3466 auto queue_state = Get<QUEUE_STATE>(queue);
3467 if (queue_state && queue_state->queueFamilyProperties.queueFlags != (VK_QUEUE_TRANSFER_BIT | VK_QUEUE_SPARSE_BINDING_BIT)) {
3468 skip |= LogPerformanceWarning(queue, kVUID_BestPractices_QueueBindSparse_NotAsync,
3469 "vkQueueBindSparse() issued on queue %s. All binds should happen on an asynchronous copy "
3470 "queue to hide the OS scheduling and submit costs.",
3471 report_data->FormatHandle(queue).c_str());
3472 }
3473 }
3474
Camden Stocker23cc47d2019-09-03 14:53:57 -06003475 return skip;
3476}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003477
Mark Lobodzinski84101d72020-04-24 09:43:48 -06003478void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
3479 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003480 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003481 return;
3482 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003483
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003484 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3485 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
3486 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3487 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003488 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003489 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003490 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003491 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003492 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3493 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
3494 image_state->sparse_metadata_bound = true;
3495 }
3496 }
3497 }
3498 }
3499}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003500
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003501bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003502 const VkClearRect* pRects) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003503 if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003504 // We don't know the accurate render area in a secondary,
3505 // so assume we clear the entire frame buffer.
3506 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
3507 return true;
3508 }
3509
3510 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
3511 for (uint32_t i = 0; i < rectCount; i++) {
3512 auto& rect = pRects[i];
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003513 auto& render_area = cmd.activeRenderPassBeginInfo.renderArea;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003514 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
3515 return true;
3516 }
3517 }
3518
3519 return false;
3520}
3521
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003522bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment,
3523 VkImageAspectFlags aspects, bool secondary) const {
3524 const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003525 bool skip = false;
3526
3527 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3528 return skip;
3529 }
3530
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003531 const auto& rp_state = cmd.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003532
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003533 auto attachment_itr =
3534 std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
3535 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003536
3537 // Only report aspects which haven't been touched yet.
3538 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003539 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003540 new_aspects &= ~attachment_itr->aspects;
3541 }
3542
3543 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
sjfricke52defd42022-08-08 16:37:46 +09003544 if (!cmd.has_draw_cmd) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003545 skip |= LogPerformanceWarning(
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003546 cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003547 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3548 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003549 report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003550 }
3551
3552 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3553 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3554 skip |= LogPerformanceWarning(
3555 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3556 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3557 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3558 "it is more efficient.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003559 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003560 }
3561
3562 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3563 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003564 skip |=
3565 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3566 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3567 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3568 "it is more efficient.",
3569 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003570 }
3571
3572 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3573 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003574 skip |=
3575 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3576 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3577 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3578 "it is more efficient.",
3579 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003580 }
3581
3582 return skip;
3583}
3584
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003585bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003586 const VkClearAttachment* pAttachments, uint32_t rectCount,
3587 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003588 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003589 const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003590 if (!cb_node) return skip;
3591
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003592 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3593 // Defer checks to ExecuteCommands.
3594 return skip;
3595 }
3596
3597 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003598 if (!ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003599 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003600 }
3601
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003602 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3603 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003604 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003605 if (rp) {
3606 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3607
3608 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003609 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003610
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003611 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3612 uint32_t color_attachment = attachment.colorAttachment;
3613 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003614 skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003615 }
3616
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003617 if (subpass.pDepthStencilAttachment &&
3618 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003619 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003620 skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003621 }
3622 }
3623 }
3624
Nadav Gevaf0808442021-05-21 13:51:25 -04003625 if (VendorCheckEnabled(kBPVendorAMD)) {
3626 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3627 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3628 bool black_check = false;
3629 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3630 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3631 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3632 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3633 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3634
3635 bool white_check = false;
3636 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3637 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3638 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3639 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3640 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3641
3642 if (black_check && white_check) {
3643 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3644 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3645 "Consider changing to one of the following:"
3646 "RGBA(0, 0, 0, 0) "
3647 "RGBA(0, 0, 0, 1) "
3648 "RGBA(1, 1, 1, 0) "
3649 "RGBA(1, 1, 1, 1)",
3650 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3651 }
3652 } else {
3653 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3654 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3655 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3656 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3657 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3658 "attachment %" PRId32 " is not a fast clear value."
3659 "Consider changing to one of the following:"
3660 "D=0.0f, S=0"
3661 "D=1.0f, S=0",
3662 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3663 }
3664 }
3665 }
3666 }
3667
Camden Stockerf55721f2019-09-09 11:04:49 -06003668 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003669}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003670
3671bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3672 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3673 const VkImageResolve* pRegions) const {
3674 bool skip = false;
3675
3676 skip |= VendorCheckEnabled(kBPVendorArm) &&
3677 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3678 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3679 "This is a very slow and extremely bandwidth intensive path. "
3680 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3681 VendorSpecificTag(kBPVendorArm));
3682
3683 return skip;
3684}
3685
Jeff Leger178b1e52020-10-05 12:22:23 -04003686bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3687 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3688 bool skip = false;
3689
3690 skip |= VendorCheckEnabled(kBPVendorArm) &&
3691 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3692 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3693 "This is a very slow and extremely bandwidth intensive path. "
3694 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3695 VendorSpecificTag(kBPVendorArm));
3696
3697 return skip;
3698}
3699
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003700bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3701 const VkResolveImageInfo2* pResolveImageInfo) const {
3702 bool skip = false;
3703
3704 skip |= VendorCheckEnabled(kBPVendorArm) &&
3705 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3706 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3707 "This is a very slow and extremely bandwidth intensive path. "
3708 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3709 VendorSpecificTag(kBPVendorArm));
3710
3711 return skip;
3712}
3713
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003714void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3715 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3716 const VkImageResolve* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003717 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003718 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003719 auto src = Get<bp_state::Image>(srcImage);
3720 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003721
3722 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003723 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3724 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003725 }
3726}
3727
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003728void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3729 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003730 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003731 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003732 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3733 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003734 uint32_t regionCount = pResolveImageInfo->regionCount;
3735
3736 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003737 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3738 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003739 }
3740}
3741
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003742void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3743 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003744 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003745 auto& funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003746 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3747 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003748 uint32_t regionCount = pResolveImageInfo->regionCount;
3749
3750 for (uint32_t i = 0; i < regionCount; i++) {
3751 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3752 pResolveImageInfo->pRegions[i].srcSubresource);
3753 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3754 pResolveImageInfo->pRegions[i].dstSubresource);
3755 }
3756}
3757
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003758void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3759 const VkClearColorValue* pColor, uint32_t rangeCount,
3760 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003761 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003762 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003763 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003764
3765 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003766 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003767 }
3768}
3769
3770void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3771 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3772 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003773 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003774 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003775 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003776
3777 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003778 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003779 }
3780}
3781
3782void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3783 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3784 const VkImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003785 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003786 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003787 auto src = Get<bp_state::Image>(srcImage);
3788 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003789
3790 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003791 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3792 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003793 }
3794}
3795
3796void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3797 VkImageLayout dstImageLayout, uint32_t regionCount,
3798 const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003799 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003800 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003801 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003802
3803 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003804 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003805 }
3806}
3807
3808void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3809 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003810 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003811 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003812 auto src = Get<bp_state::Image>(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003813
3814 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003815 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003816 }
3817}
3818
3819void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3820 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3821 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003822 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003823 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003824 auto src = Get<bp_state::Image>(srcImage);
3825 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003826
3827 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003828 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3829 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003830 }
3831}
3832
Attilio Provenzano02859b22020-02-27 14:17:28 +00003833bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3834 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3835 bool skip = false;
3836
3837 if (VendorCheckEnabled(kBPVendorArm)) {
3838 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3839 skip |= LogPerformanceWarning(
3840 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3841 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3842 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3843 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003844 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003845 }
3846
3847 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3848 skip |= LogPerformanceWarning(
3849 device, kVUID_BestPractices_CreateSampler_LodClamping,
3850 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3851 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3852 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3853 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3854 }
3855
3856 if (pCreateInfo->mipLodBias != 0.0f) {
3857 skip |=
3858 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3859 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3860 "descriptors being created and may cause reduced performance.",
3861 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3862 }
3863
3864 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3865 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3866 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3867 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3868 skip |= LogPerformanceWarning(
3869 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3870 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3871 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3872 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3873 VendorSpecificTag(kBPVendorArm));
3874 }
3875
3876 if (pCreateInfo->unnormalizedCoordinates) {
3877 skip |= LogPerformanceWarning(
3878 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3879 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3880 "descriptors being created and may cause reduced performance.",
3881 VendorSpecificTag(kBPVendorArm));
3882 }
3883
3884 if (pCreateInfo->anisotropyEnable) {
3885 skip |= LogPerformanceWarning(
3886 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3887 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3888 "and may cause reduced performance.",
3889 VendorSpecificTag(kBPVendorArm));
3890 }
3891 }
3892
3893 return skip;
3894}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003895
Nadav Gevaf0808442021-05-21 13:51:25 -04003896void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3897 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3898 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3899 void* cgpl_state) {
3900 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3901 pPipelines);
3902 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003903 num_pso_ += createInfoCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003904}
3905
3906bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3907 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3908 const VkCopyDescriptorSet* pDescriptorCopies) const {
3909 bool skip = false;
3910 if (VendorCheckEnabled(kBPVendorAMD)) {
3911 if (descriptorCopyCount > 0) {
3912 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3913 "%s Performance warning: copying descriptor sets is not recommended",
3914 VendorSpecificTag(kBPVendorAMD));
3915 }
3916 }
3917
3918 return skip;
3919}
3920
3921bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3922 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3923 const VkAllocationCallbacks* pAllocator,
3924 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3925 bool skip = false;
3926 if (VendorCheckEnabled(kBPVendorAMD)) {
3927 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3928 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3929 "vkUpdateDescriptorSet instead",
3930 VendorSpecificTag(kBPVendorAMD));
3931 }
3932
3933 return skip;
3934}
3935
3936bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3937 const VkClearColorValue* pColor, uint32_t rangeCount,
3938 const VkImageSubresourceRange* pRanges) const {
3939 bool skip = false;
3940 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003941 skip |= LogPerformanceWarning(
3942 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003943 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3944 "vkCmdClearAttachments instead",
3945 VendorSpecificTag(kBPVendorAMD));
3946 }
3947
3948 return skip;
3949}
3950
3951bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3952 VkImageLayout imageLayout,
3953 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3954 const VkImageSubresourceRange* pRanges) const {
3955 bool skip = false;
3956 if (VendorCheckEnabled(kBPVendorAMD)) {
3957 skip |= LogPerformanceWarning(
3958 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3959 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3960 "vkCmdClearAttachments instead",
3961 VendorSpecificTag(kBPVendorAMD));
3962 }
3963
3964 return skip;
3965}
3966
3967bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3968 const VkAllocationCallbacks* pAllocator,
3969 VkPipelineLayout* pPipelineLayout) const {
3970 bool skip = false;
3971 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003972 uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2;
Nadav Gevaf0808442021-05-21 13:51:25 -04003973 // Descriptor sets cost 1 DWORD each.
3974 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3975 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3976 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3977 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3978 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003979 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003980 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size;
Nadav Gevaf0808442021-05-21 13:51:25 -04003981 }
3982
3983 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3984 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3985 }
3986
3987 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3988 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3989 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3990 "Descriptor sets cost 1 DWORD each. "
3991 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3992 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3993 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3994 VendorSpecificTag(kBPVendorAMD));
3995 }
3996 }
3997
Rodrigo Locatti65b33832022-03-15 17:57:30 -03003998 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3999 bool has_separate_sampler = false;
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004000 size_t fast_space_usage = 0;
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004001
4002 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; ++i) {
4003 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
4004 for (const auto& binding : descriptor_set_layout_state->GetBindings()) {
4005 if (binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) {
4006 has_separate_sampler = true;
4007 }
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004008
4009 if ((descriptor_set_layout_state->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) == 0U) {
4010 size_t descriptor_type_size = 0;
4011
4012 switch (binding.descriptorType) {
4013 case VK_DESCRIPTOR_TYPE_SAMPLER:
4014 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
4015 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
4016 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
4017 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
4018 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
4019 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
4020 descriptor_type_size = 4;
4021 break;
4022 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
4023 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
4024 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
4025 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
4026 descriptor_type_size = 8;
4027 break;
4028 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
4029 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
4030 case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE:
4031 descriptor_type_size = 16;
4032 break;
4033 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
4034 descriptor_type_size = 1;
4035 default:
4036 // Unknown type.
4037 break;
4038 }
4039
4040 size_t descriptor_size = descriptor_type_size * binding.descriptorCount;
4041 fast_space_usage += descriptor_size;
4042 }
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004043 }
4044 }
4045
4046 if (has_separate_sampler) {
4047 skip |= LogPerformanceWarning(
4048 device, kVUID_BestPractices_CreatePipelineLayout_SeparateSampler,
4049 "%s Consider using combined image samplers instead of separate samplers for marginally better performance.",
4050 VendorSpecificTag(kBPVendorNVIDIA));
4051 }
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004052
4053 if (fast_space_usage > kPipelineLayoutFastDescriptorSpaceNVIDIA) {
4054 skip |= LogPerformanceWarning(
4055 device, kVUID_BestPractices_CreatePipelinesLayout_LargePipelineLayout,
4056 "%s Pipeline layout size is too large, prefer using pipeline-specific descriptor set layouts. "
4057 "Aim for consuming less than %" PRIu32 " bytes to allow fast reads for all non-bindless descriptors. "
4058 "Samplers, textures, texel buffers, and combined image samplers consume 4 bytes each. "
4059 "Uniform buffers and acceleration structures consume 8 bytes. "
4060 "Storage buffers consume 16 bytes. "
4061 "Push constants do not consume space.",
4062 VendorSpecificTag(kBPVendorNVIDIA), kPipelineLayoutFastDescriptorSpaceNVIDIA);
4063 }
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004064 }
4065
Nadav Gevaf0808442021-05-21 13:51:25 -04004066 return skip;
4067}
4068
4069bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4070 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4071 const VkImageCopy* pRegions) const {
4072 bool skip = false;
4073 std::stringstream src_image_hex;
4074 std::stringstream dst_image_hex;
4075 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
4076 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
4077
4078 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004079 auto src_state = Get<IMAGE_STATE>(srcImage);
4080 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04004081
4082 if (src_state && dst_state) {
4083 VkImageTiling src_Tiling = src_state->createInfo.tiling;
4084 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
4085 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
4086 skip |=
4087 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
4088 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
4089 "image (vkCmdCopyImageToBuffer) "
4090 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
4091 "copies when converting between linear and optimal images",
4092 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
4093 }
4094 }
4095 }
4096
4097 return skip;
4098}
4099
4100bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
4101 VkPipeline pipeline) const {
4102 bool skip = false;
4103
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03004104 auto cb = Get<bp_state::CommandBuffer>(commandBuffer);
4105
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004106 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004107 if (IsPipelineUsedInFrame(pipeline)) {
Nadav Gevaf0808442021-05-21 13:51:25 -04004108 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004109 "%s %s Performance warning: Pipeline %s was bound twice in the frame. "
4110 "Keep pipeline state changes to a minimum, for example, by sorting draw calls by pipeline.",
4111 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA),
4112 report_data->FormatHandle(pipeline).c_str());
Nadav Gevaf0808442021-05-21 13:51:25 -04004113 }
4114 }
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03004115 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4116 const auto& tgm = cb->nv.tess_geometry_mesh;
4117 if (tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA && !tgm.threshold_signaled) {
4118 LogPerformanceWarning(commandBuffer, kVUID_BestPractices_BindPipeline_SwitchTessGeometryMesh,
4119 "%s Avoid switching between pipelines with and without tessellation, geometry, task, "
4120 "and/or mesh shaders. Group draw calls using these shader stages together.",
4121 VendorSpecificTag(kBPVendorNVIDIA));
4122 // Do not set 'skip' so the number of switches gets properly counted after the message.
4123 }
4124 }
4125
Nadav Gevaf0808442021-05-21 13:51:25 -04004126 return skip;
4127}
4128
4129void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
4130 VkFence fence, VkResult result) {
4131 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004132 num_queue_submissions_ += submitCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04004133}
4134
4135bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
4136 bool skip = false;
4137
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004138 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004139 auto num = num_queue_submissions_.load();
4140 if (num > kNumberOfSubmissionWarningLimitAMD) {
4141 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004142 "%s %s Performance warning: command buffers submitted %" PRId32
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004143 " times this frame. Submitting command buffers has a CPU "
4144 "and GPU overhead. Submit fewer times to incur less overhead.",
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004145 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04004146 }
4147 }
4148
4149 return skip;
4150}
4151
4152void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
4153 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
4154 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
4155 uint32_t bufferMemoryBarrierCount,
4156 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
4157 uint32_t imageMemoryBarrierCount,
4158 const VkImageMemoryBarrier* pImageMemoryBarriers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004159 num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount);
Nadav Gevaf0808442021-05-21 13:51:25 -04004160}
4161
4162bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
4163 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
4164 bool skip = false;
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004165 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004166 if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04004167 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004168 "%s %s Performance warning: High number of vkSemaphore objects created. "
Nadav Gevaf0808442021-05-21 13:51:25 -04004169 "Minimize the amount of queue synchronization that is used. "
4170 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004171 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04004172 }
4173 }
4174
4175 return skip;
4176}
4177
4178bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
4179 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
4180 bool skip = false;
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004181 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004182 if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04004183 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004184 "%s %s Performance warning: High number of VkFence objects created."
Nadav Gevaf0808442021-05-21 13:51:25 -04004185 "Minimize the amount of CPU-GPU synchronization that is used. "
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004186 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
4187 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04004188 }
4189 }
4190
4191 return skip;
4192}
4193
Sam Walls8e77e4f2020-03-16 20:47:40 +00004194void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
4195
4196bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
4197 // look for a cache hit
4198 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
4199 if (hit != _entries.end()) {
4200 // mark the cache hit as being most recently used
4201 hit->age = iteration++;
4202 return true;
4203 }
4204
4205 // if there's no cache hit, we need to model the entry being inserted into the cache
4206 CacheEntry new_entry = {value, iteration};
4207 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
4208 // if there is still space left in the cache, use the next available slot
4209 *(_entries.begin() + iteration) = new_entry;
4210 } else {
4211 // otherwise replace the least recently used cache entry
4212 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
4213 *lru = new_entry;
4214 }
4215 iteration++;
4216 return false;
4217}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004218
4219bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
4220 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004221 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004222 bool skip = false;
4223 if (swapchain_data && swapchain_data->images.size() == 0) {
4224 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
4225 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
4226 "vkGetSwapchainImagesKHR after swapchain creation.");
4227 }
4228 return skip;
4229}
4230
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004231void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
4232 if (no_pointer) {
4233 if (UNCALLED == call_state) {
4234 call_state = QUERY_COUNT;
4235 }
4236 } else { // Save queue family properties
4237 call_state = QUERY_DETAILS;
4238 }
4239}
4240
Nathaniel Cesariof121d122020-10-08 13:09:46 -06004241void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
4242 uint32_t* pQueueFamilyPropertyCount,
4243 VkQueueFamilyProperties* pQueueFamilyProperties) {
4244 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
4245 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004246 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004247 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004248 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
4249 nullptr == pQueueFamilyProperties);
4250 }
4251}
4252
4253void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
4254 uint32_t* pQueueFamilyPropertyCount,
4255 VkQueueFamilyProperties2* pQueueFamilyProperties) {
4256 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
4257 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004258 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004259 if (bp_pd_state) {
4260 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
4261 nullptr == pQueueFamilyProperties);
4262 }
4263}
4264
4265void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
4266 uint32_t* pQueueFamilyPropertyCount,
4267 VkQueueFamilyProperties2* pQueueFamilyProperties) {
4268 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
4269 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004270 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004271 if (bp_pd_state) {
4272 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
4273 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004274 }
4275}
4276
Nathaniel Cesariof121d122020-10-08 13:09:46 -06004277void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
4278 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004279 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004280 if (bp_pd_state) {
4281 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
4282 }
4283}
4284
Nathaniel Cesariof121d122020-10-08 13:09:46 -06004285void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
4286 VkPhysicalDeviceFeatures2* pFeatures) {
4287 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004288 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004289 if (bp_pd_state) {
4290 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
4291 }
4292}
4293
Nathaniel Cesariof121d122020-10-08 13:09:46 -06004294void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
4295 VkPhysicalDeviceFeatures2* pFeatures) {
4296 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004297 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004298 if (bp_pd_state) {
4299 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
4300 }
4301}
4302
4303void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
4304 VkSurfaceKHR surface,
4305 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
4306 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004307 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004308 if (bp_pd_state) {
4309 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
4310 }
4311}
4312
4313void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
4314 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
4315 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004316 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004317 if (bp_pd_state) {
4318 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
4319 }
4320}
4321
4322void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
4323 VkSurfaceKHR surface,
4324 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
4325 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004326 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004327 if (bp_pd_state) {
4328 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
4329 }
4330}
4331
4332void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
4333 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
4334 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004335 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004336 if (bp_pd_data) {
4337 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
4338
4339 if (*pPresentModeCount) {
4340 if (call_state < QUERY_COUNT) {
4341 call_state = QUERY_COUNT;
4342 }
4343 }
4344 if (pPresentModes) {
4345 if (call_state < QUERY_DETAILS) {
4346 call_state = QUERY_DETAILS;
4347 }
4348 }
4349 }
4350}
4351
4352void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
4353 uint32_t* pSurfaceFormatCount,
4354 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004355 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004356 if (bp_pd_data) {
4357 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
4358
4359 if (*pSurfaceFormatCount) {
4360 if (call_state < QUERY_COUNT) {
4361 call_state = QUERY_COUNT;
4362 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06004363 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004364 }
4365 if (pSurfaceFormats) {
4366 if (call_state < QUERY_DETAILS) {
4367 call_state = QUERY_DETAILS;
4368 }
4369 }
4370 }
4371}
4372
4373void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
4374 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
4375 uint32_t* pSurfaceFormatCount,
4376 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004377 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004378 if (bp_pd_data) {
4379 if (*pSurfaceFormatCount) {
4380 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
4381 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
4382 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06004383 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004384 }
4385 if (pSurfaceFormats) {
4386 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
4387 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
4388 }
4389 }
4390 }
4391}
4392
4393void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
4394 uint32_t* pPropertyCount,
4395 VkDisplayPlanePropertiesKHR* pProperties,
4396 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004397 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004398 if (bp_pd_data) {
4399 if (*pPropertyCount) {
4400 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
4401 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
4402 }
4403 }
4404 if (pProperties) {
4405 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
4406 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
4407 }
4408 }
4409 }
4410}
4411
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004412void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
4413 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
4414 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004415 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06004416 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
4417 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
4418 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004419 }
4420 }
4421}
4422
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01004423void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
4424 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
4425
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06004426 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01004427 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02004428 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01004429 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004430 auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01004431 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07004432 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01004433 }
4434 }
4435 }
4436}