blob: 1a057aa833cbcfe61edd694c0cc3320c1718ea24 [file] [log] [blame]
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
Nadav Geva41c12a22021-05-21 13:14:05 -04004 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Camdeneaa86ea2019-07-26 11:00:09 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Camden Stocker <camden@lunarg.com>
Nadav Geva41c12a22021-05-21 13:14:05 -040019 * Author: Nadav Geva <nadav.geva@amd.com>
Camdeneaa86ea2019-07-26 11:00:09 -060020 */
21
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070022#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060023#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060024#include "best_practices_error_enums.h"
Sam Wallsd7ab6db2020-06-19 20:41:54 +010025#include "shader_validation.h"
Jeremy Gebbena3705f42021-01-19 16:47:43 -070026#include "sync_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060027#include "cmd_buffer_state.h"
28#include "device_state.h"
29#include "render_pass_state.h"
Camden5b184be2019-08-13 07:50:19 -060030
31#include <string>
Sam Walls8e77e4f2020-03-16 20:47:40 +000032#include <bitset>
Sam Wallsd7ab6db2020-06-19 20:41:54 +010033#include <memory>
Camden5b184be2019-08-13 07:50:19 -060034
Attilio Provenzano19d6a982020-02-27 12:41:41 +000035struct VendorSpecificInfo {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060036 EnableFlags vendor_id;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000037 std::string name;
38};
39
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070040const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060041 {kBPVendorArm, {vendor_specific_arm, "Arm"}},
Nadav Geva41c12a22021-05-21 13:14:05 -040042 {kBPVendorAMD, {vendor_specific_amd, "AMD"}},
Attilio Provenzano19d6a982020-02-27 12:41:41 +000043};
44
Hannes Harnisch607d1d92021-07-10 18:44:56 +020045const SpecialUseVUIDs kSpecialUseInstanceVUIDs {
46 kVUID_BestPractices_CreateInstance_SpecialUseExtension_CADSupport,
47 kVUID_BestPractices_CreateInstance_SpecialUseExtension_D3DEmulation,
48 kVUID_BestPractices_CreateInstance_SpecialUseExtension_DevTools,
49 kVUID_BestPractices_CreateInstance_SpecialUseExtension_Debugging,
50 kVUID_BestPractices_CreateInstance_SpecialUseExtension_GLEmulation,
51};
52
53const SpecialUseVUIDs kSpecialUseDeviceVUIDs {
54 kVUID_BestPractices_CreateDevice_SpecialUseExtension_CADSupport,
55 kVUID_BestPractices_CreateDevice_SpecialUseExtension_D3DEmulation,
56 kVUID_BestPractices_CreateDevice_SpecialUseExtension_DevTools,
57 kVUID_BestPractices_CreateDevice_SpecialUseExtension_Debugging,
58 kVUID_BestPractices_CreateDevice_SpecialUseExtension_GLEmulation,
59};
60
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060061std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb,
62 const VkCommandBufferAllocateInfo* pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060063 const COMMAND_POOL_STATE* pool) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060064 return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<CMD_BUFFER_STATE_BP>(this, cb, pCreateInfo, pool));
65}
66
67CMD_BUFFER_STATE_BP::CMD_BUFFER_STATE_BP(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060068 const COMMAND_POOL_STATE* pool)
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060069 : CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {}
70
Attilio Provenzano19d6a982020-02-27 12:41:41 +000071bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070072 for (const auto& vendor : kVendorInfo) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060073 if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000074 return true;
75 }
76 }
77 return false;
78}
79
80const char* VendorSpecificTag(BPVendorFlags vendors) {
81 // Cache built vendor tags in a map
Jeremy Gebbencbf22862021-03-03 12:01:22 -070082 static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000083
84 auto res = tag_map.find(vendors);
85 if (res == tag_map.end()) {
86 // Build the vendor tag string
87 std::stringstream vendor_tag;
88
89 vendor_tag << "[";
90 bool first_vendor = true;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070091 for (const auto& vendor : kVendorInfo) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000092 if (vendors & vendor.first) {
93 if (!first_vendor) {
94 vendor_tag << ", ";
95 }
96 vendor_tag << vendor.second.name;
97 first_vendor = false;
98 }
99 }
100 vendor_tag << "]";
101
102 tag_map[vendors] = vendor_tag.str();
103 res = tag_map.find(vendors);
104 }
105
106 return res->second.c_str();
107}
108
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700109const char* DepReasonToString(ExtDeprecationReason reason) {
110 switch (reason) {
111 case kExtPromoted:
112 return "promoted to";
113 break;
114 case kExtObsoleted:
115 return "obsoleted by";
116 break;
117 case kExtDeprecated:
118 return "deprecated by";
119 break;
120 default:
121 return "";
122 break;
123 }
124}
125
126bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
127 const char* vuid) const {
128 bool skip = false;
129 auto dep_info_it = deprecated_extensions.find(extension_name);
130 if (dep_info_it != deprecated_extensions.end()) {
131 auto dep_info = dep_info_it->second;
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600132 if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
Tony-LunarGc30b59f2022-02-15 11:02:36 -0700133 ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2)) ||
134 ((dep_info.target.compare("VK_VERSION_1_3") == 0) && (version >= VK_API_VERSION_1_3))) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700135 skip |=
136 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
137 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600138 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700139 if (dep_info.target.length() == 0) {
140 skip |= LogWarning(instance, vuid,
141 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
142 "without replacement.",
143 api_name, extension_name);
144 } else {
145 skip |= LogWarning(instance, vuid,
146 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
147 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
148 }
149 }
150 }
151 return skip;
152}
153
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200154bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
155{
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700156 bool skip = false;
157 auto dep_info_it = special_use_extensions.find(extension_name);
158
159 if (dep_info_it != special_use_extensions.end()) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200160 const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s "
161 "and it is strongly recommended that it be otherwise avoided.";
162 auto& special_uses = dep_info_it->second;
sfricke-samsungef15e482022-01-26 11:32:49 -0800163
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700164 if (special_uses.find("cadsupport") != std::string::npos) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800165 skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
166 "specialized functionality used by CAD/CAM applications");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700167 }
168 if (special_uses.find("d3demulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200169 skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name,
170 "D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700171 }
172 if (special_uses.find("devtools") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200173 skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
174 "developer tools such as capture-replay libraries");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700175 }
176 if (special_uses.find("debugging") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200177 skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
178 "use by applications when debugging");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700179 }
180 if (special_uses.find("glemulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200181 skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700182 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200183 "specific to those APIs");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700184 }
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700185 }
186 return skip;
187}
188
Nadav Gevaf0808442021-05-21 13:51:25 -0400189void BestPractices::InitDeviceValidationObject(bool add_obj, ValidationObject* inst_obj, ValidationObject* dev_obj) {
190 if (add_obj) {
191 ValidationStateTracker::InitDeviceValidationObject(add_obj, inst_obj, dev_obj);
192 }
193}
194
195
Camden5b184be2019-08-13 07:50:19 -0600196bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500197 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600198 bool skip = false;
199
200 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
201 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800202 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700203 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
204 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600205 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600206 uint32_t specified_version =
207 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
208 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700209 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200210 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600211 }
212
213 return skip;
214}
215
Camden5b184be2019-08-13 07:50:19 -0600216bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500217 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600218 bool skip = false;
219
220 // get API version of physical device passed when creating device.
221 VkPhysicalDeviceProperties physical_device_properties{};
222 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500223 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600224
225 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600226 if (api_version > device_api_version) {
227 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600228 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600229
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700230 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
231 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
232 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600233 }
234
235 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
236 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800237 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700238 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
239 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600240 }
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600241 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700242 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200243 skip |= ValidateSpecialUseExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600244 }
245
Jeremy Gebben78684b12022-02-23 17:31:56 -0700246 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600247 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700248 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
249 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600250 }
251
Nadav Gevaf0808442021-05-21 13:51:25 -0400252 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
Szilard Papp7d2c7952020-06-22 14:38:13 +0100253 (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
254 skip |= LogPerformanceWarning(
255 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
Nadav Gevaf0808442021-05-21 13:51:25 -0400256 "%s %s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100257 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
258 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
259 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
Nadav Gevaf0808442021-05-21 13:51:25 -0400260 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100261 }
262
Camden5b184be2019-08-13 07:50:19 -0600263 return skip;
264}
265
266bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500267 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600268 bool skip = false;
269
270 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700271 std::stringstream buffer_hex;
272 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600273
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700274 skip |= LogWarning(
275 device, kVUID_BestPractices_SharingModeExclusive,
276 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
277 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700278 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600279 }
280
281 return skip;
282}
283
284bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500285 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600286 bool skip = false;
287
288 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700289 std::stringstream image_hex;
290 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600291
292 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700293 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
294 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
295 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700296 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600297 }
298
ziga-lunarg6df3d102022-03-18 17:02:14 +0100299 if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
300 skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags,
301 "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not "
302 "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the "
303 "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect.");
304 }
305
Attilio Provenzano02859b22020-02-27 14:17:28 +0000306 if (VendorCheckEnabled(kBPVendorArm)) {
307 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
308 skip |= LogPerformanceWarning(
309 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
310 "%s vkCreateImage(): Trying to create an image with %u samples. "
311 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
312 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
313 }
314
315 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
316 skip |= LogPerformanceWarning(
317 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
318 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
319 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
320 "and do not need to be backed by physical storage. "
321 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
322 VendorSpecificTag(kBPVendorArm));
323 }
324 }
325
Nadav Gevaf0808442021-05-21 13:51:25 -0400326 if (VendorCheckEnabled(kBPVendorAMD)) {
327 std::stringstream image_hex;
328 image_hex << "0x" << std::hex << HandleToUint64(pImage);
329
330 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
331 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
332 skip |= LogPerformanceWarning(device,
333 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
334 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
335 "Using a SHARING_MODE_CONCURRENT "
336 "is not recommended with color and depth targets",
337 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
338 }
339
340 if ((pCreateInfo->usage &
341 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
342 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
343 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
344 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
345 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
346 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
347 }
348
349 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
350 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
351 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
352 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
353 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
354 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
355 }
356 }
357
Camden5b184be2019-08-13 07:50:19 -0600358 return skip;
359}
360
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100361void BestPractices::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
362 ValidationStateTracker::PreCallRecordDestroyImage(device, image, pAllocator);
363 ReleaseImageUsageState(image);
364}
365
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200366void BestPractices::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
Tony-LunarG299187b2021-05-28 09:29:12 -0600367 if (VK_NULL_HANDLE != swapchain) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600368 auto chain = Get<SWAPCHAIN_NODE>(swapchain);
Tony-LunarG299187b2021-05-28 09:29:12 -0600369 for (auto& image : chain->images) {
370 if (image.image_state) {
371 ReleaseImageUsageState(image.image_state->image());
372 }
373 }
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200374 }
375 ValidationStateTracker::PreCallRecordDestroySwapchainKHR(device, swapchain, pAllocator);
376}
377
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100378IMAGE_STATE_BP* BestPractices::GetImageUsageState(VkImage vk_image) {
379 auto itr = imageUsageMap.find(vk_image);
380 if (itr != imageUsageMap.end()) {
381 return &itr->second;
382 } else {
383 auto& state = imageUsageMap[vk_image];
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600384 auto image = Get<IMAGE_STATE>(vk_image);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600385 state.image = image.get();
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100386 state.usages.resize(image->createInfo.arrayLayers);
387 for (auto& mips : state.usages) {
388 mips.resize(image->createInfo.mipLevels, IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED);
389 }
390 return &state;
391 }
392}
393
394void BestPractices::ReleaseImageUsageState(VkImage image) {
395 auto itr = imageUsageMap.find(image);
396 if (itr != imageUsageMap.end()) {
397 imageUsageMap.erase(itr);
398 }
399}
400
Camden5b184be2019-08-13 07:50:19 -0600401bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500402 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600403 bool skip = false;
404
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600405 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600406 if (bp_pd_state) {
407 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
408 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
409 "vkCreateSwapchainKHR() called before getting surface capabilities from "
410 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
411 }
Camden83a9c372019-08-14 11:41:38 -0600412
Shannon McPherson73e58c82021-03-05 17:14:26 -0700413 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
414 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600415 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
416 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
417 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
418 }
Camden83a9c372019-08-14 11:41:38 -0600419
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600420 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
421 skip |= LogWarning(
422 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
423 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
424 }
Camden83a9c372019-08-14 11:41:38 -0600425 }
426
Camden5b184be2019-08-13 07:50:19 -0600427 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700428 skip |=
429 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600430 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700431 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
432 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600433 }
434
Szilard Papp48a6da32020-06-10 14:41:59 +0100435 if (pCreateInfo->minImageCount == 2) {
436 skip |= LogPerformanceWarning(
437 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
438 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
439 ", which means double buffering is going "
440 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
441 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
442 "3 to use triple buffering to maximize performance in such cases.",
443 pCreateInfo->minImageCount);
444 }
445
Szilard Pappd5f0f812020-06-22 09:01:29 +0100446 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
447 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
448 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
449 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
450 "Presentation modes which are not FIFO will present the latest available frame and discard other "
451 "frame(s) if any.",
452 VendorSpecificTag(kBPVendorArm));
453 }
454
Camden5b184be2019-08-13 07:50:19 -0600455 return skip;
456}
457
458bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
459 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500460 const VkAllocationCallbacks* pAllocator,
461 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600462 bool skip = false;
463
464 for (uint32_t i = 0; i < swapchainCount; i++) {
465 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700466 skip |= LogWarning(
467 device, kVUID_BestPractices_SharingModeExclusive,
468 "Warning: A shared swapchain (index %" PRIu32
469 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
470 "queues (queueFamilyIndexCount of %" PRIu32 ").",
471 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600472 }
473 }
474
475 return skip;
476}
477
478bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500479 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600480 bool skip = false;
481
482 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
483 VkFormat format = pCreateInfo->pAttachments[i].format;
484 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
485 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
486 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700487 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
488 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
489 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
490 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
491 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600492 }
493 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700494 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
495 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
496 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
497 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
498 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600499 }
500 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000501
502 const auto& attachment = pCreateInfo->pAttachments[i];
503 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
504 bool access_requires_memory =
505 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
506
507 if (FormatHasStencil(format)) {
508 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
509 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
510 }
511
512 if (access_requires_memory) {
513 skip |= LogPerformanceWarning(
514 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
515 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
516 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
517 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
518 i, static_cast<uint32_t>(attachment.samples));
519 }
520 }
Camden5b184be2019-08-13 07:50:19 -0600521 }
522
523 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
524 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
525 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
526 }
527
528 return skip;
529}
530
Tony-LunarG767180f2020-04-23 14:03:59 -0600531bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
532 const VkImageView* image_views) const {
533 bool skip = false;
534
535 // Check for non-transient attachments that should be transient and vice versa
536 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200537 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600538 bool attachment_should_be_transient =
539 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
540
541 if (FormatHasStencil(attachment.format)) {
542 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
543 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
544 }
545
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600546 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600547 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600548 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600549
550 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
551
552 // The check for an image that should not be transient applies to all GPUs
553 if (!attachment_should_be_transient && image_is_transient) {
554 skip |= LogPerformanceWarning(
555 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
556 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
557 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
558 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
559 i);
560 }
561
562 bool supports_lazy = false;
563 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
564 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
565 supports_lazy = true;
566 }
567 }
568
569 // The check for an image that should be transient only applies to GPUs supporting
570 // lazily allocated memory
571 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
572 skip |= LogPerformanceWarning(
573 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
574 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
575 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
576 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
577 i);
578 }
579 }
580 }
581 return skip;
582}
583
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000584bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
585 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
586 bool skip = false;
587
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600588 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800589 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600590 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000591 }
592
593 return skip;
594}
595
Sam Wallse746d522020-03-16 21:20:23 +0000596bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
597 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
598 bool skip = false;
599 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
600
601 if (!skip) {
602 const auto& pool_handle = pAllocateInfo->descriptorPool;
603 auto iter = descriptor_pool_freed_count.find(pool_handle);
604 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
605 // this warning is specific to Arm
606 if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) {
607 skip |= LogPerformanceWarning(
608 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
609 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
610 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
611 VendorSpecificTag(kBPVendorArm));
612 }
613 }
614
615 return skip;
616}
617
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600618void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
619 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000620 if (result == VK_SUCCESS) {
621 // find the free count for the pool we allocated into
622 auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool);
623 if (iter != descriptor_pool_freed_count.end()) {
624 // we record successful allocations by subtracting the allocation count from the last recorded free count
625 const auto alloc_count = pAllocateInfo->descriptorSetCount;
626 // clamp the unsigned subtraction to the range [0, last_free_count]
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700627 if (iter->second > alloc_count) {
Sam Wallse746d522020-03-16 21:20:23 +0000628 iter->second -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700629 } else {
Sam Wallse746d522020-03-16 21:20:23 +0000630 iter->second = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700631 }
Sam Wallse746d522020-03-16 21:20:23 +0000632 }
633 }
634}
635
636void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
637 const VkDescriptorSet* pDescriptorSets, VkResult result) {
638 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
639 if (result == VK_SUCCESS) {
640 // we want to track frees because we're interested in suggesting re-use
641 auto iter = descriptor_pool_freed_count.find(descriptorPool);
642 if (iter == descriptor_pool_freed_count.end()) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700643 descriptor_pool_freed_count.emplace(descriptorPool, descriptorSetCount);
Sam Wallse746d522020-03-16 21:20:23 +0000644 } else {
645 iter->second += descriptorSetCount;
646 }
647 }
648}
649
Camden5b184be2019-08-13 07:50:19 -0600650bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500651 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600652 bool skip = false;
653
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500654 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700655 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
656 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600657 }
658
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000659 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
660 skip |= LogPerformanceWarning(
661 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600662 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
663 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000664 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
665 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
666 }
667
Camden83a9c372019-08-14 11:41:38 -0600668 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
669
670 return skip;
671}
672
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600673void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
674 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
675 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700676 if (result != VK_SUCCESS) {
677 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
678 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800679 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700680 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600681 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700682 return;
683 }
684 num_mem_objects++;
685}
Camden Stocker9738af92019-10-16 13:54:03 -0700686
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600687void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
688 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700689 auto error = std::find(error_codes.begin(), error_codes.end(), result);
690 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000691 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
692 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
693
694 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
695 if (common_failure != common_failure_codes.end()) {
696 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
697 } else {
698 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
699 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700700 return;
701 }
702 auto success = std::find(success_codes.begin(), success_codes.end(), result);
703 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600704 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
705 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500706 }
707}
708
Jeff Bolz5c801d12019-10-09 10:38:45 -0500709bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
710 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700711 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600712 bool skip = false;
713
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700714 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600715
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700716 for (const auto& item : mem_info->ObjectBindings()) {
717 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600718 LogObjectList objlist(device);
719 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600720 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600721 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 -0600722 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600723 }
724
Camden5b184be2019-08-13 07:50:19 -0600725 return skip;
726}
727
728void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700729 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
Camden5b184be2019-08-13 07:50:19 -0600730 if (memory != VK_NULL_HANDLE) {
731 num_mem_objects--;
732 }
733}
734
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000735bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600736 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700737 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600738
sfricke-samsunge2441192019-11-06 14:07:57 -0800739 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700740 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
741 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
742 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600743 }
744
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700745 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000746
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300747 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000748 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
749 skip |= LogPerformanceWarning(
750 device, kVUID_BestPractices_SmallDedicatedAllocation,
751 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600752 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
753 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000754 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
755 }
756
Camden Stockerb603cc82019-09-03 10:09:02 -0600757 return skip;
758}
759
760bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500761 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600762 bool skip = false;
763 const char* api_name = "BindBufferMemory()";
764
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000765 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600766
767 return skip;
768}
769
770bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500771 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600772 char api_name[64];
773 bool skip = false;
774
775 for (uint32_t i = 0; i < bindInfoCount; i++) {
776 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000777 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600778 }
779
780 return skip;
781}
Camden Stockerb603cc82019-09-03 10:09:02 -0600782
783bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500784 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600785 char api_name[64];
786 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600787
Camden Stocker8b798ab2019-09-03 10:33:28 -0600788 for (uint32_t i = 0; i < bindInfoCount; i++) {
789 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000790 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600791 }
792
793 return skip;
794}
795
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000796bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600797 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700798 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600799
sfricke-samsung71bc6572020-04-29 15:49:43 -0700800 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600801 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700802 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
803 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
804 api_name, report_data->FormatHandle(image).c_str());
805 }
806 } else {
807 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
808 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600809 }
810
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700811 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000812
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600813 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000814 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
815 skip |= LogPerformanceWarning(
816 device, kVUID_BestPractices_SmallDedicatedAllocation,
817 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600818 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
819 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000820 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
821 }
822
823 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
824 // make sure this type is actually used.
825 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
826 // (i.e.most tile - based renderers)
827 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
828 bool supports_lazy = false;
829 uint32_t suggested_type = 0;
830
831 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600832 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000833 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
834 supports_lazy = true;
835 suggested_type = i;
836 break;
837 }
838 }
839 }
840
841 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
842
843 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
844 skip |= LogPerformanceWarning(
845 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600846 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000847 "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 -0600848 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600849 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000850 }
851 }
852
Camden Stocker8b798ab2019-09-03 10:33:28 -0600853 return skip;
854}
855
856bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500857 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600858 bool skip = false;
859 const char* api_name = "vkBindImageMemory()";
860
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000861 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600862
863 return skip;
864}
865
866bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500867 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600868 char api_name[64];
869 bool skip = false;
870
871 for (uint32_t i = 0; i < bindInfoCount; i++) {
872 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700873 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600874 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
875 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600876 }
877
878 return skip;
879}
880
881bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500882 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600883 char api_name[64];
884 bool skip = false;
885
886 for (uint32_t i = 0; i < bindInfoCount; i++) {
887 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000888 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600889 }
890
891 return skip;
892}
Camden83a9c372019-08-14 11:41:38 -0600893
Attilio Provenzano02859b22020-02-27 14:17:28 +0000894static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
895 switch (format) {
896 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
897 case VK_FORMAT_R16_SFLOAT:
898 case VK_FORMAT_R16G16_SFLOAT:
899 case VK_FORMAT_R16G16B16_SFLOAT:
900 case VK_FORMAT_R16G16B16A16_SFLOAT:
901 case VK_FORMAT_R32_SFLOAT:
902 case VK_FORMAT_R32G32_SFLOAT:
903 case VK_FORMAT_R32G32B32_SFLOAT:
904 case VK_FORMAT_R32G32B32A32_SFLOAT:
905 return false;
906
907 default:
908 return true;
909 }
910}
911
912bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
913 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
914 bool skip = false;
915
916 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700917 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000918
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700919 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
920 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
921 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000922 return skip;
923 }
924
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600925 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200926 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000927
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +0200928 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
929 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
930
931 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200932 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000933 uint32_t att = subpass.pColorAttachments[j].attachment;
934
935 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
936 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
937 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
938 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
939 "color attachment #%u makes use "
940 "of a format which cannot be blended at full throughput when using MSAA.",
941 VendorSpecificTag(kBPVendorArm), i, j);
942 }
943 }
944 }
945 }
946
947 return skip;
948}
949
Nadav Gevaf0808442021-05-21 13:51:25 -0400950void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
951 const VkComputePipelineCreateInfo* pCreateInfos,
952 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
953 VkResult result, void* pipe_state) {
954 // AMD best practice
955 pipeline_cache = pipelineCache;
956}
957
Camden5b184be2019-08-13 07:50:19 -0600958bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
959 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600960 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500961 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600962 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
963 pAllocator, pPipelines, cgpl_state_data);
ziga-lunarg08c81582022-03-08 17:33:45 +0100964 if (skip) {
965 return skip;
966 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600967 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600968
969 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700970 skip |= LogPerformanceWarning(
971 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
972 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
973 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600974 }
975
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000976 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200977 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000978
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600979 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200980 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600981 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700982 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
983 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600984 count++;
985 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000986 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600987 if (count > kMaxInstancedVertexBuffers) {
988 skip |= LogPerformanceWarning(
989 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
990 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
991 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
992 count, kMaxInstancedVertexBuffers);
993 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000994 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000995
Szilard Pappaaf2da32020-06-22 10:37:35 +0100996 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
997 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200998 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
999 VendorCheckEnabled(kBPVendorArm)) {
1000 skip |= LogPerformanceWarning(
1001 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
1002 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
1003 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
1004 "efficiency during rasterization. Consider disabling depthBias or increasing either "
1005 "depthBiasConstantFactor or depthBiasSlopeFactor.",
1006 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +01001007 }
1008
Attilio Provenzano02859b22020-02-27 14:17:28 +00001009 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001010 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001011 if (VendorCheckEnabled(kBPVendorAMD)) {
1012 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1013 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
1014 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1015 "improve cache hit rate", VendorSpecificTag(kBPVendorAMD));
1016 }
1017
1018 if (num_pso > kMaxRecommendedNumberOfPSOAMD) {
1019 skip |=
1020 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
1021 "%s Performance warning: Too many pipelines created, consider consolidation",
1022 VendorSpecificTag(kBPVendorAMD));
1023 }
1024
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -06001025 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001026 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
1027 "%s Performance warning: Use of primitive restart is not recommended",
1028 VendorSpecificTag(kBPVendorAMD));
1029 }
1030
1031 // TODO: this might be too aggressive of a check
1032 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
1033 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
1034 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1035 VendorSpecificTag(kBPVendorAMD));
1036 }
1037 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001038
Camden5b184be2019-08-13 07:50:19 -06001039 return skip;
1040}
1041
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001042void BestPractices::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator)
1043{
1044 auto itr = graphicsPipelineCIs.find(pipeline);
1045 if (itr != graphicsPipelineCIs.end()) {
1046 graphicsPipelineCIs.erase(itr);
1047 }
1048 ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator);
1049}
1050
Sam Walls0961ec02020-03-31 16:39:15 +01001051void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1052 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1053 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1054 VkResult result, void* cgpl_state_data) {
1055 for (size_t i = 0; i < count; i++) {
1056 const auto* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
1057 const VkPipeline pipeline_handle = pPipelines[i];
1058
1059 // record depth stencil state and color blend states for depth pre-pass tracking purposes
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001060 GraphicsPipelineCIs& cis = graphicsPipelineCIs[pipeline_handle];
Sam Walls0961ec02020-03-31 16:39:15 +01001061
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001062 auto& create_info = cgpl_state->pCreateInfos[i];
Andrej Redeky0d513072022-02-13 14:38:20 +01001063
1064 if (create_info.renderPass == VK_NULL_HANDLE) {
1065 // TODO: this is necessary to avoid crashing
1066 LogWarning(device, kVUID_BestPractices_DynamicRendering_NotSupported,
1067 "vkCreateGraphicsPipelines: pCreateInfos[%" PRIu32
1068 "].renderPass is VK_NULL_HANDLE, VK_KHR_dynamic_rendering is not supported.\n",
1069 static_cast<uint32_t>(i));
1070 continue;
1071 }
1072
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001073 auto rp = Get<RENDER_PASS_STATE>(create_info.renderPass);
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001074
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001075 if (create_info.pColorBlendState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001076 // pColorBlendState is ignored if the pipeline has rasterization disabled or if no color attachments are used
1077 bool uses_color_attachments = false;
1078 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1079 uses_color_attachments |= rp->UsesColorAttachment(j);
1080 }
1081 if (uses_color_attachments && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1082 cis.colorBlendStateCI.emplace(create_info.pColorBlendState);
1083 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001084 }
1085
1086 if (create_info.pDepthStencilState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001087 // pDepthStencilState is ignored if the pipeline has rasterization disabled or if no depth/stencil attachment is used
1088 bool uses_depth_stencil = false;
1089 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1090 uses_depth_stencil |= rp->UsesDepthStencilAttachment(j);
1091 }
1092 if (uses_depth_stencil && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1093 cis.depthStencilStateCI.emplace(create_info.pDepthStencilState);
1094 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001095 }
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001096 // Record which frame buffer attachments we should consider to be accessed when a draw call is performed.
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001097 auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1098 cis.accessFramebufferAttachments.clear();
1099
1100 if (cis.colorBlendStateCI) {
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001101 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1102 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, cis.colorBlendStateCI->attachmentCount);
1103 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001104 if (cis.colorBlendStateCI->pAttachments[j].colorWriteMask != 0) {
1105 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1106 if (attachment != VK_ATTACHMENT_UNUSED) {
1107 cis.accessFramebufferAttachments.push_back({ attachment, VK_IMAGE_ASPECT_COLOR_BIT });
1108 }
1109 }
1110 }
1111 }
1112
1113 if (cis.depthStencilStateCI && (cis.depthStencilStateCI->depthTestEnable ||
1114 cis.depthStencilStateCI->depthBoundsTestEnable ||
1115 cis.depthStencilStateCI->stencilTestEnable)) {
1116 uint32_t attachment = subpass.pDepthStencilAttachment ?
1117 subpass.pDepthStencilAttachment->attachment :
1118 VK_ATTACHMENT_UNUSED;
1119 if (attachment != VK_ATTACHMENT_UNUSED) {
1120 VkImageAspectFlags aspects = 0;
1121 if (cis.depthStencilStateCI->depthTestEnable || cis.depthStencilStateCI->depthBoundsTestEnable) {
1122 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1123 }
1124 if (cis.depthStencilStateCI->stencilTestEnable) {
1125 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1126 }
1127 cis.accessFramebufferAttachments.push_back({ attachment, aspects });
1128 }
1129 }
Sam Walls0961ec02020-03-31 16:39:15 +01001130 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001131
1132 // AMD best practice
1133 pipeline_cache = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001134}
1135
Camden5b184be2019-08-13 07:50:19 -06001136bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1137 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001138 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001139 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001140 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1141 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001142
1143 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001144 skip |= LogPerformanceWarning(
1145 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1146 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1147 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001148 }
1149
Nadav Gevaf0808442021-05-21 13:51:25 -04001150 if (VendorCheckEnabled(kBPVendorAMD)) {
1151 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1152 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1153 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1154 "improve cache hit rate",
1155 VendorSpecificTag(kBPVendorAMD));
1156 }
1157 }
1158
sfricke-samsung86d055a2022-02-11 14:43:50 -08001159 for (uint32_t i = 0; i < createInfoCount; i++) {
1160 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1161 if (VendorCheckEnabled(kBPVendorArm)) {
1162 skip |= ValidateCreateComputePipelineArm(createInfo);
1163 }
1164
1165 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1166 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1167 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1168 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1169 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1170 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1171 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1172 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1173 i);
1174 }
1175 }
1176 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001177 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001178
1179 return skip;
1180}
1181
1182bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1183 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001184 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001185 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001186 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1187 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001188
1189 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001190 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001191
1192 uint32_t thread_count = x * y * z;
1193
1194 // Generate a priori warnings about work group sizes.
1195 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1196 skip |= LogPerformanceWarning(
1197 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1198 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1199 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1200 "groups with less than %u threads, especially when using barrier() or shared memory.",
1201 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1202 }
1203
1204 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1205 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1206 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1207 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1208 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1209 "%u, %u) is not aligned to %u "
1210 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1211 "leave threads idle on the shader "
1212 "core.",
1213 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1214 kThreadGroupDispatchCountAlignmentArm);
1215 }
1216
sfricke-samsungef15e482022-01-26 11:32:49 -08001217 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1218 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001219
1220 unsigned dimensions = 0;
1221 if (x > 1) dimensions++;
1222 if (y > 1) dimensions++;
1223 if (z > 1) dimensions++;
1224 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1225 dimensions = std::max(dimensions, 1u);
1226
1227 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1228 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1229 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1230 bool accesses_2d = false;
1231 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001232 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001233 if (dim < 0) continue;
1234 auto spvdim = spv::Dim(dim);
1235 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1236 }
1237
1238 if (accesses_2d && dimensions < 2) {
1239 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1240 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1241 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1242 "exhibiting poor spatial locality with respect to one or more shader resources.",
1243 VendorSpecificTag(kBPVendorArm), x, y, z);
1244 }
1245
Camden5b184be2019-08-13 07:50:19 -06001246 return skip;
1247}
1248
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001249bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001250 bool skip = false;
1251
1252 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001253 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1254 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001255 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001256 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1257 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001258 }
1259
1260 return skip;
1261}
1262
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001263bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1264 bool skip = false;
1265
1266 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1267 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1268 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1269 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1270 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1271 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1272 }
1273
1274 return skip;
1275}
1276
1277bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1278 bool skip = false;
1279 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1280
1281 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1282 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1283
1284 return skip;
1285}
1286
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001287void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001288 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1289 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1290 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1291 LogPerformanceWarning(
1292 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1293 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1294 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1295 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1296 "convenient opportunity.",
1297 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1298 }
1299 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001300
1301 // AMD best practice
1302 // end-of-frame cleanup
1303 num_queue_submissions = 0;
1304 num_barriers_objects = 0;
1305 pipelines_used_in_frame.clear();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001306}
1307
Jeff Bolz5c801d12019-10-09 10:38:45 -05001308bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1309 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001310 bool skip = false;
1311
1312 for (uint32_t submit = 0; submit < submitCount; submit++) {
1313 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1314 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1315 }
1316 }
1317
1318 return skip;
1319}
1320
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001321bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1322 VkFence fence) const {
1323 bool skip = false;
1324
1325 for (uint32_t submit = 0; submit < submitCount; submit++) {
1326 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1327 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1328 }
1329 }
1330
1331 return skip;
1332}
1333
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001334bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1335 VkFence fence) const {
1336 bool skip = false;
1337
1338 for (uint32_t submit = 0; submit < submitCount; submit++) {
1339 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1340 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1341 }
1342 }
1343
1344 return skip;
1345}
1346
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001347bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1348 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1349 bool skip = false;
1350
1351 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1352 skip |= LogPerformanceWarning(
1353 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1354 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1355 "pool instead.");
1356 }
1357
1358 return skip;
1359}
1360
1361bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1362 const VkCommandBufferBeginInfo* pBeginInfo) const {
1363 bool skip = false;
1364
1365 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1366 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1367 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1368 }
1369
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001370 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1371 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001372 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1373 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1374 VendorSpecificTag(kBPVendorArm));
1375 }
1376
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001377 return skip;
1378}
1379
Jeff Bolz5c801d12019-10-09 10:38:45 -05001380bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001381 bool skip = false;
1382
1383 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1384
1385 return skip;
1386}
1387
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001388bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1389 const VkDependencyInfoKHR* pDependencyInfo) const {
1390 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1391}
1392
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001393bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1394 const VkDependencyInfo* pDependencyInfo) const {
1395 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1396}
1397
Jeff Bolz5c801d12019-10-09 10:38:45 -05001398bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1399 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001400 bool skip = false;
1401
1402 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1403
1404 return skip;
1405}
1406
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001407bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1408 VkPipelineStageFlags2KHR stageMask) const {
1409 bool skip = false;
1410
1411 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1412
1413 return skip;
1414}
1415
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001416bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1417 VkPipelineStageFlags2 stageMask) const {
1418 bool skip = false;
1419
1420 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1421
1422 return skip;
1423}
1424
Camden5b184be2019-08-13 07:50:19 -06001425bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1426 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1427 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1428 uint32_t bufferMemoryBarrierCount,
1429 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1430 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001431 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001432 bool skip = false;
1433
1434 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1435 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1436
1437 return skip;
1438}
1439
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001440bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1441 const VkDependencyInfoKHR* pDependencyInfos) const {
1442 bool skip = false;
1443 for (uint32_t i = 0; i < eventCount; i++) {
1444 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1445 }
1446
1447 return skip;
1448}
1449
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001450bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1451 const VkDependencyInfo* pDependencyInfos) const {
1452 bool skip = false;
1453 for (uint32_t i = 0; i < eventCount; i++) {
1454 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1455 }
1456
1457 return skip;
1458}
1459
Camden5b184be2019-08-13 07:50:19 -06001460bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1461 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1462 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1463 uint32_t bufferMemoryBarrierCount,
1464 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1465 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001466 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001467 bool skip = false;
1468
1469 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1470 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1471
Nadav Gevaf0808442021-05-21 13:51:25 -04001472 if (VendorCheckEnabled(kBPVendorAMD)) {
1473 if (num_barriers_objects + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
1474 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
1475 "%s Performance warning: In this frame, %" PRIu32 " barriers were already submitted. Barriers have a high cost and can "
1476 "stall the GPU. "
1477 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1478 VendorSpecificTag(kBPVendorAMD), num_barriers_objects);
1479 }
1480
1481 std::array<VkImageLayout, 3> read_layouts = {
1482 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1483 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1484 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1485 };
1486
1487 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1488 // read to read barriers
1489 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1490 bool old_is_read_layout = found != read_layouts.end();
1491 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1492 bool new_is_read_layout = found != read_layouts.end();
1493 if (old_is_read_layout && new_is_read_layout) {
1494 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1495 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1496 "time you use it.",
1497 VendorSpecificTag(kBPVendorAMD));
1498 }
1499
1500 // general with no storage
1501 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1502 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1503 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1504 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1505 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1506 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1507 VendorSpecificTag(kBPVendorAMD));
1508 }
1509 }
1510 }
1511 }
1512
Camden5b184be2019-08-13 07:50:19 -06001513 return skip;
1514}
1515
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001516bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1517 const VkDependencyInfoKHR* pDependencyInfo) const {
1518 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1519}
1520
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001521bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1522 const VkDependencyInfo* pDependencyInfo) const {
1523 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1524}
1525
Camden5b184be2019-08-13 07:50:19 -06001526bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001527 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001528 bool skip = false;
1529
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001530 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1531
1532 return skip;
1533}
1534
1535bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1536 VkQueryPool queryPool, uint32_t query) const {
1537 bool skip = false;
1538
1539 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001540
1541 return skip;
1542}
1543
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001544bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1545 VkQueryPool queryPool, uint32_t query) const {
1546 bool skip = false;
1547
1548 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1549
1550 return skip;
1551}
1552
Sam Walls0961ec02020-03-31 16:39:15 +01001553void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1554 VkPipeline pipeline) {
1555 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1556
Nadav Gevaf0808442021-05-21 13:51:25 -04001557 // AMD best practice
1558 pipelines_used_in_frame.emplace(pipeline);
1559
Sam Walls0961ec02020-03-31 16:39:15 +01001560 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
1561 // check for depth/blend state tracking
1562 auto gp_cis = graphicsPipelineCIs.find(pipeline);
1563 if (gp_cis != graphicsPipelineCIs.end()) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07001564 auto cb_node = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001565 assert(cb_node);
1566 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001567
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001568 render_pass_state.nextDrawTouchesAttachments = gp_cis->second.accessFramebufferAttachments;
1569 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001570
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001571 const auto& blend_state = gp_cis->second.colorBlendStateCI;
1572 const auto& stencil_state = gp_cis->second.depthStencilStateCI;
Sam Walls0961ec02020-03-31 16:39:15 +01001573
1574 if (blend_state) {
1575 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001576 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001577 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1578 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001579 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001580 }
1581 }
1582 }
1583
1584 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001585 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001586
1587 if (stencil_state && stencil_state->depthTestEnable) {
1588 switch (stencil_state->depthCompareOp) {
1589 case VK_COMPARE_OP_EQUAL:
1590 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1591 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001592 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001593 break;
1594 default:
1595 break;
1596 }
1597 }
Sam Walls0961ec02020-03-31 16:39:15 +01001598 }
1599 }
1600}
1601
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001602static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1603 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1604 const auto& subpass_info = createInfo.pSubpasses[subpass];
1605 if (subpass_info.pResolveAttachments) {
1606 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1607 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1608 }
1609 }
1610 }
1611
1612 return false;
1613}
1614
Attilio Provenzano02859b22020-02-27 14:17:28 +00001615static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1616 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001617 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001618
1619 // If an attachment is ever used as a color attachment,
1620 // resolve attachment or depth stencil attachment,
1621 // it needs to exist on tile at some point.
1622
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001623 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1624 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001625 }
1626
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001627 if (subpass_info.pResolveAttachments) {
1628 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1629 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1630 }
1631 }
1632
1633 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001634 }
1635
1636 return false;
1637}
1638
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001639static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1640 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1641 return false;
1642 }
1643
1644 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001645 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001646
1647 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1648 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1649 return true;
1650 }
1651 }
1652 }
1653
1654 return false;
1655}
1656
Attilio Provenzano02859b22020-02-27 14:17:28 +00001657bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1658 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1659 bool skip = false;
1660
1661 if (!pRenderPassBegin) {
1662 return skip;
1663 }
1664
Gareth Webbdc6549a2021-06-16 03:52:24 +01001665 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1666 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1667 "This render pass has a zero-size render area. It cannot write to any attachments, "
1668 "and can only be used for side effects such as layout transitions.");
1669 }
1670
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001671 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001672 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001673 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001674 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001675 if (rpabi) {
1676 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1677 }
1678 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001679 // Check if any attachments have LOAD operation on them
1680 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001681 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001682
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001683 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001684 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001685 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001686 }
1687
1688 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001689 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001690 }
1691
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001692 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001693
1694 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001695 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1696 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001697 }
1698
1699 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001700 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1701 skip |= LogPerformanceWarning(
1702 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1703 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1704 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001705 "which will copy in total %u pixels (renderArea = "
1706 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001707 VendorSpecificTag(kBPVendorArm), att,
1708 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1709 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1710 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001711 }
1712 }
1713 }
1714
1715 return skip;
1716}
1717
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001718void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1719 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001720 if (view) {
Jeremy Gebbenb4d17012021-07-08 13:18:15 -06001721 QueueValidateImage(funcs, function_name, GetImageUsageState(view->create_info.image), usage,
1722 view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001723 }
1724}
1725
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001726void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1727 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001728 const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001729 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001730
1731 // If we're viewing a 3D slice, ignore base array layer.
1732 // The entire 3D subresource is accessed as one atomic unit.
1733 const uint32_t base_array_layer = image->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
1734
1735 const uint32_t max_layers = image->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001736 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
1737 const uint32_t max_levels = image->createInfo.mipLevels - subresource_range.baseMipLevel;
1738 const uint32_t mip_levels = std::min(image->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001739
1740 for (uint32_t layer = 0; layer < array_layers; layer++) {
1741 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001742 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1743 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001744 }
1745 }
1746}
1747
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001748void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1749 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001750 const VkImageSubresourceLayers& subresource_layers) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001751 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001752 const uint32_t max_layers = image->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
1753 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001754
1755 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001756 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001757 }
1758}
1759
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001760void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1761 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001762 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001763 if (VendorCheckEnabled(kBPVendorArm)) {
1764 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](
1765 const ValidationStateTracker&, const QUEUE_STATE&, const CMD_BUFFER_STATE&) -> bool {
1766 ValidateImageInQueue(function_name, state, usage, array_layer, mip_level);
1767 return false;
1768 });
1769 }
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001770}
1771
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001772void BestPractices::ValidateImageInQueueArm(const char* function_name, IMAGE_STATE* image,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001773 IMAGE_SUBRESOURCE_USAGE_BP last_usage,
1774 IMAGE_SUBRESOURCE_USAGE_BP usage,
1775 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001776 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001777 if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED &&
Jeremy Gebben82e11d52021-07-26 09:19:37 -06001778 !image->IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001779 LogPerformanceWarning(
1780 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001781 "%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 +02001782 "image was used, it was written to with STORE_OP_STORE. "
1783 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1784 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001785 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001786 } 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 +02001787 LogPerformanceWarning(
1788 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001789 "%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 +02001790 "image was used, it was written to with vkCmdClear*Image(). "
1791 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1792 "tile-based architectures."
1793 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001794 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001795 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
1796 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE ||
1797 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1798 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE ||
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001799 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001800 const char *last_cmd = nullptr;
1801 const char *vuid = nullptr;
1802 const char *suggestion = nullptr;
1803
1804 switch (last_usage) {
1805 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1806 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1807 last_cmd = "vkCmdBlitImage";
1808 suggestion =
1809 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1810 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1811 "which avoids the memory roundtrip.";
1812 break;
1813 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1814 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1815 last_cmd = "vkCmdClear*Image";
1816 suggestion =
1817 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1818 "tile-based architectures. "
1819 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1820 break;
1821 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1822 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1823 last_cmd = "vkCmdCopy*Image";
1824 suggestion =
1825 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1826 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1827 "which avoids the memory roundtrip.";
1828 break;
1829 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1830 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1831 last_cmd = "vkCmdResolveImage";
1832 suggestion =
1833 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1834 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1835 "which avoids the memory roundtrip.";
1836 break;
1837 default:
1838 break;
1839 }
1840
1841 LogPerformanceWarning(
1842 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001843 "%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 +01001844 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001845 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001846 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001847}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001848
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001849void BestPractices::ValidateImageInQueue(const char* function_name, IMAGE_STATE_BP* state,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001850 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer,
1851 uint32_t mip_level) {
1852 IMAGE_STATE* image = state->image;
1853 IMAGE_SUBRESOURCE_USAGE_BP last_usage = state->usages[array_layer][mip_level];
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001854 state->usages[array_layer][mip_level] = usage;
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001855 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001856 ValidateImageInQueueArm(function_name, image, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001857 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001858}
1859
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001860void BestPractices::AddDeferredQueueOperations(CMD_BUFFER_STATE_BP* cb) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001861 cb->queue_submit_functions.insert(cb->queue_submit_functions.end(),
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001862 cb->queue_submit_functions_after_render_pass.begin(),
1863 cb->queue_submit_functions_after_render_pass.end());
1864 cb->queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001865}
1866
1867void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1868 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben78684b12022-02-23 17:31:56 -07001869 auto cb_node = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001870 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001871}
1872
1873void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1874 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben78684b12022-02-23 17:31:56 -07001875 auto cb_node = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001876 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001877}
1878
1879void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1880 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben78684b12022-02-23 17:31:56 -07001881 auto cb_node = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001882 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001883}
1884
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001885void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1886 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001887 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001888 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001889 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1890}
1891
1892void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1893 const VkRenderPassBeginInfo* pRenderPassBegin,
1894 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1895 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1896 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1897}
1898
1899void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1900 const VkRenderPassBeginInfo* pRenderPassBegin,
1901 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1902 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1903 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1904}
1905
1906void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001907
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001908 if (!pRenderPassBegin) {
1909 return;
1910 }
1911
Jeremy Gebben78684b12022-02-23 17:31:56 -07001912 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001913
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001914 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001915 if (rp_state) {
1916 // Check load ops
1917 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001918 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001919
1920 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1921 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1922 continue;
1923 }
1924
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001925 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001926
1927 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1928 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001929 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001930 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1931 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001932 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001933 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001934 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001935 }
1936
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001937 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001938 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001939
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001940 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001941 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1942 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001943 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001944 }
1945 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001946 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001947 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001948
Jeremy Gebben9f537102021-10-05 16:37:12 -06001949 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001950 }
1951
1952 // Check store ops
1953 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001954 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001955
1956 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1957 continue;
1958 }
1959
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001960 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001961
1962 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1963 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001964 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001965 }
1966
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001967 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001968
Jeremy Gebben9f537102021-10-05 16:37:12 -06001969 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001970 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001971 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1972 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001973 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001974 }
1975 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001976 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001977 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001978
Jeremy Gebben9f537102021-10-05 16:37:12 -06001979 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001980 }
1981 }
1982}
1983
Attilio Provenzano02859b22020-02-27 14:17:28 +00001984bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1985 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001986 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1987 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001988 return skip;
1989}
1990
1991bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1992 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001993 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001994 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1995 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001996 return skip;
1997}
1998
1999bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08002000 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002001 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2002 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002003 return skip;
2004}
2005
Sam Walls0961ec02020-03-31 16:39:15 +01002006void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
2007 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002008 // Reset the renderpass state
Jeremy Gebben78684b12022-02-23 17:31:56 -07002009 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002010 cb->hasDrawCmd = false;
2011 assert(cb);
2012 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002013 render_pass_state.touchesAttachments.clear();
2014 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002015 render_pass_state.numDrawCallsDepthOnly = 0;
2016 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2017 render_pass_state.colorAttachment = false;
2018 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002019 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002020 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002021
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002022 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002023
2024 // track depth / color attachment usage within the renderpass
2025 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2026 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002027 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002028
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002029 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002030 }
2031}
2032
2033void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2034 VkSubpassContents contents) {
2035 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2036 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2037}
2038
2039void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2040 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2041 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2042 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2043}
2044
2045void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2046 const VkRenderPassBeginInfo* pRenderPassBegin,
2047 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2048 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2049 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2050}
2051
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002052// Generic function to handle validation for all CmdDraw* type functions
2053bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2054 bool skip = false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07002055 const auto cb_state = Get<CMD_BUFFER_STATE_BP>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002056 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002057 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2058 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002059 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002060
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002061 // Verify vertex binding
2062 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
2063 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002064 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002065 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002066 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2067 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002068 }
2069 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002070
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002071 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002072 if (pipe) {
2073 const auto* rp_state = pipe->rp_state.get();
2074 if (rp_state) {
2075 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2076 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002077 const auto& create_info = pipe->create_info.graphics;
2078 const uint32_t depth_stencil_attachment =
2079 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2080 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2081 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002082 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2083 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2084 }
2085 }
2086 }
2087 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002088 }
2089 return skip;
2090}
2091
Sam Walls0961ec02020-03-31 16:39:15 +01002092void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002093 auto cb_node = Get<CMD_BUFFER_STATE_BP>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002094 assert(cb_node);
2095 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002096 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002097 RecordCmdDrawTypeArm(render_pass_state, draw_count, caller);
2098 }
2099
2100 if (render_pass_state.drawTouchAttachments) {
2101 for (auto& touch : render_pass_state.nextDrawTouchesAttachments) {
2102 RecordAttachmentAccess(render_pass_state, touch.framebufferAttachment, touch.aspects);
2103 }
2104 // No need to touch the same attachments over and over.
2105 render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002106 }
2107}
2108
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002109void BestPractices::RecordCmdDrawTypeArm(RenderPassState& render_pass_state, uint32_t draw_count, const char* caller) {
2110 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2111 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2112 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002113 }
2114}
2115
Camden5b184be2019-08-13 07:50:19 -06002116bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002117 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002118 bool skip = false;
2119
2120 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002121 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2122 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002123 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002124 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002125
2126 return skip;
2127}
2128
Sam Walls0961ec02020-03-31 16:39:15 +01002129void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2130 uint32_t firstVertex, uint32_t firstInstance) {
2131 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2132 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2133}
2134
Camden5b184be2019-08-13 07:50:19 -06002135bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002136 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002137 bool skip = false;
2138
2139 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002140 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2141 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002142 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002143 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2144
Attilio Provenzano02859b22020-02-27 14:17:28 +00002145 // Check if we reached the limit for small indexed draw calls.
2146 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben78684b12022-02-23 17:31:56 -07002147 const auto cmd_state = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002148 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002149 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2150 VendorCheckEnabled(kBPVendorArm)) {
2151 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002152 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002153 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2154 "You can try batching drawcalls or instancing when applicable.",
2155 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2156 }
2157
Sam Walls8e77e4f2020-03-16 20:47:40 +00002158 if (VendorCheckEnabled(kBPVendorArm)) {
2159 ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2160 }
2161
2162 return skip;
2163}
2164
2165bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2166 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2167 bool skip = false;
2168
2169 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Jeremy Gebben78684b12022-02-23 17:31:56 -07002170 const auto cmd_state = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002171 if (cmd_state == nullptr) return skip;
2172
locke-lunarg1ae57d62020-11-18 10:49:19 -07002173 const auto* ib_state = cmd_state->index_buffer_binding.buffer_state.get();
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002174 if (ib_state == nullptr || cmd_state->index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002175
2176 const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002177 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002178 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2179 const void* ib_mem = ib_mem_state.p_driver_data;
2180 bool primitive_restart_enable = false;
2181
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002182 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2183 const auto& pipeline_binding_iter = cmd_state->lastBound[lv_bind_point];
2184 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002185
Jeremy Gebben11af9792021-08-20 10:20:09 -06002186 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2187 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002188 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002189
2190 // 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 -06002191 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002192 uint32_t scan_stride;
2193 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2194 scan_stride = sizeof(uint8_t);
2195 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2196 scan_stride = sizeof(uint16_t);
2197 } else {
2198 scan_stride = sizeof(uint32_t);
2199 }
2200
2201 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2202 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2203
2204 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2205 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2206 // irrespective of whether or not they're part of the draw call.
2207
2208 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2209 uint32_t min_index = ~0u;
2210 // start with maximum as 0 and adjust to indices in the buffer
2211 uint32_t max_index = 0u;
2212
2213 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2214 // for the given index buffer
2215 uint32_t vertex_shade_count = 0;
2216
2217 PostTransformLRUCacheModel post_transform_cache;
2218
2219 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2220 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2221 // target architecture.
2222 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2223 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2224 post_transform_cache.resize(32);
2225
2226 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2227 uint32_t scan_index;
2228 uint32_t primitive_restart_value;
2229 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2230 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2231 primitive_restart_value = 0xFF;
2232 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2233 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2234 primitive_restart_value = 0xFFFF;
2235 } else {
2236 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2237 primitive_restart_value = 0xFFFFFFFF;
2238 }
2239
2240 max_index = std::max(max_index, scan_index);
2241 min_index = std::min(min_index, scan_index);
2242
2243 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2244 bool in_cache = post_transform_cache.query_cache(scan_index);
2245 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2246 if (!in_cache) vertex_shade_count++;
2247 }
2248 }
2249
2250 // 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 +01002251 // 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
2252 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002253
2254 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002255 skip |=
2256 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2257 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2258 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2259 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2260 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2261 VendorSpecificTag(kBPVendorArm),
2262 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002263 return skip;
2264 }
2265
2266 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2267 // 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 +01002268 const size_t refs_per_bucket = 64;
2269 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2270
2271 const uint32_t n_indices = max_index - min_index + 1;
2272 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2273 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2274
2275 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2276 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002277
2278 // To avoid using too much memory, we run over the indices again.
2279 // Knowing the size from the last scan allows us to record index usage with bitsets
2280 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2281 uint32_t scan_index;
2282 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2283 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2284 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2285 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2286 } else {
2287 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2288 }
2289 // keep track of the set of all indices used to reference vertices in the draw call
2290 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002291 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2292 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002293 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2294 }
2295
2296 uint32_t vertex_reference_count = 0;
2297 for (const auto& bitset : vertex_reference_buckets) {
2298 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2299 }
2300
2301 // 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 -07002302 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002303 // 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 -07002304 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002305
2306 if (utilization < 0.5f) {
2307 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2308 "%s The indices which were specified for the draw call only utilise approximately "
2309 "%.02f%% of the bound vertex buffer.",
2310 VendorSpecificTag(kBPVendorArm), utilization);
2311 }
2312
2313 if (cache_hit_rate <= 0.5f) {
2314 skip |=
2315 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2316 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2317 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2318 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2319 "recently shaded vertices.",
2320 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2321 }
2322 }
2323
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002324 return skip;
2325}
2326
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002327bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2328 const VkCommandBuffer* pCommandBuffers) const {
2329 bool skip = false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07002330 const auto primary = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002331 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002332 const auto secondary_cb = Get<CMD_BUFFER_STATE_BP>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002333 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002334 continue;
2335 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002336 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002337 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002338 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(clear.rects.size()), clear.rects.data())) {
2339 skip |= ValidateClearAttachment(commandBuffer, primary.get(), clear.framebufferAttachment, clear.colorAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002340 clear.aspects, true);
2341 }
2342 }
2343 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002344
2345 if (VendorCheckEnabled(kBPVendorAMD)) {
2346 if (commandBufferCount > 0) {
2347 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2348 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2349 VendorSpecificTag(kBPVendorAMD));
2350 }
2351 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002352 return skip;
2353}
2354
2355void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2356 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002357 auto primary = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002358 auto& primary_state = primary->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002359
2360 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002361 auto secondary_cb = Get<CMD_BUFFER_STATE_BP>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002362 if (secondary_cb == nullptr) {
2363 continue;
2364 }
2365 auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002366
2367 for (auto& early_clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002368 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2369 RecordAttachmentClearAttachments(primary.get(), primary_state, early_clear.framebufferAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002370 early_clear.colorAttachment, early_clear.aspects,
Hans-Kristian Arntzenfa8ef9f2021-06-29 12:07:59 +02002371 uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002372 } else {
2373 RecordAttachmentAccess(primary_state, early_clear.framebufferAttachment,
2374 early_clear.aspects);
2375 }
2376 }
2377
2378 for (auto& touch : secondary.touchesAttachments) {
2379 RecordAttachmentAccess(primary_state, touch.framebufferAttachment,
2380 touch.aspects);
2381 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002382
2383 primary_state.numDrawCallsDepthEqualCompare += secondary.numDrawCallsDepthEqualCompare;
2384 primary_state.numDrawCallsDepthOnly += secondary.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002385
Jeremy Gebben78684b12022-02-23 17:31:56 -07002386 auto second_state = Get<CMD_BUFFER_STATE_BP>(pCommandBuffers[i]);
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002387 if (second_state->hasDrawCmd) {
2388 primary->hasDrawCmd = true;
2389 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002390 }
2391
2392 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2393}
2394
2395void BestPractices::RecordAttachmentAccess(RenderPassState& state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2396 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
2397 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002398 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002399 return info.framebufferAttachment == fb_attachment;
2400 });
2401
2402 if (itr != state.touchesAttachments.end()) {
2403 itr->aspects |= aspects;
2404 } else {
2405 state.touchesAttachments.push_back({ fb_attachment, aspects });
2406 }
2407}
2408
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002409void BestPractices::RecordAttachmentClearAttachments(CMD_BUFFER_STATE_BP* cmd_state, RenderPassState& state, uint32_t fb_attachment,
2410 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2411 const VkClearRect* pRects) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002412 // If we observe a full clear before any other access to a frame buffer attachment,
2413 // we have candidate for redundant clear attachments.
2414 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002415 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002416 return info.framebufferAttachment == fb_attachment;
2417 });
2418
2419 uint32_t new_aspects = aspects;
2420 if (itr != state.touchesAttachments.end()) {
2421 new_aspects = aspects & ~itr->aspects;
2422 itr->aspects |= aspects;
2423 } else {
2424 state.touchesAttachments.push_back({ fb_attachment, aspects });
2425 }
2426
2427 if (new_aspects == 0) {
2428 return;
2429 }
2430
2431 if (cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2432 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2433 // CmdExecuteCommands.
2434 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2435 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2436 }
2437}
2438
2439void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2440 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2441 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002442 auto cmd_state = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002443 RENDER_PASS_STATE* rp_state = cmd_state->activeRenderPass.get();
2444 FRAMEBUFFER_STATE* fb_state = cmd_state->activeFramebuffer.get();
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002445 RenderPassState& tracking_state = cmd_state->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002446 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2447
2448 if (rectCount == 0 || !rp_state) {
2449 return;
2450 }
2451
2452 if (!is_secondary && !fb_state) {
2453 return;
2454 }
2455
2456 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
Jeremy Gebben9f537102021-10-05 16:37:12 -06002457 bool full_clear = ClearAttachmentsIsFullClear(cmd_state.get(), rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002458
ziga-lunarg885c6542022-03-07 01:08:25 +01002459 if (!rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) {
2460 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2461 for (uint32_t i = 0; i < attachmentCount; i++) {
2462 auto& attachment = pClearAttachments[i];
2463 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2464 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002465
ziga-lunarg885c6542022-03-07 01:08:25 +01002466 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2467 if (subpass.pDepthStencilAttachment) {
2468 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2469 }
2470 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2471 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002472 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002473
ziga-lunarg885c6542022-03-07 01:08:25 +01002474 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2475 if (full_clear) {
2476 RecordAttachmentClearAttachments(cmd_state.get(), tracking_state, fb_attachment, attachment.colorAttachment,
2477 aspects, rectCount, pRects);
2478 } else {
2479 RecordAttachmentAccess(tracking_state, fb_attachment, aspects);
2480 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002481 }
2482 }
2483 }
2484
2485 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments,
2486 rectCount, pRects);
2487}
2488
Attilio Provenzano02859b22020-02-27 14:17:28 +00002489void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2490 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2491 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2492 firstInstance);
2493
Jeremy Gebben78684b12022-02-23 17:31:56 -07002494 auto cmd_state = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002495 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2496 cmd_state->small_indexed_draw_call_count++;
2497 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002498
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002499 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002500}
2501
Sam Walls0961ec02020-03-31 16:39:15 +01002502void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2503 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2504 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2505 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2506}
2507
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002508bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2509 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2510 uint32_t maxDrawCount, uint32_t stride) const {
2511 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2512
2513 return skip;
2514}
2515
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002516bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2517 VkDeviceSize offset, VkBuffer countBuffer,
2518 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2519 uint32_t stride) const {
2520 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002521
2522 return skip;
2523}
2524
2525bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002526 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002527 bool skip = false;
2528
2529 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002530 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2531 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002532 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002533 }
2534
2535 return skip;
2536}
2537
Sam Walls0961ec02020-03-31 16:39:15 +01002538void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2539 uint32_t count, uint32_t stride) {
2540 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2541 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2542}
2543
Camden5b184be2019-08-13 07:50:19 -06002544bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002545 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002546 bool skip = false;
2547
2548 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002549 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2550 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002551 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002552 }
2553
2554 return skip;
2555}
2556
Sam Walls0961ec02020-03-31 16:39:15 +01002557void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2558 uint32_t count, uint32_t stride) {
2559 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2560 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2561}
2562
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002563void BestPractices::ValidateBoundDescriptorSets(VkCommandBuffer commandBuffer, const char* function_name) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002564 auto cb_state = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002565
2566 if (cb_state) {
2567 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002568 const auto& layout = *descriptor_set->GetLayout();
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002569
2570 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2571 // For bindless scenarios, we should not attempt to track descriptor set state.
2572 // It is highly uncertain which resources are actually bound.
2573 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2574 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2575 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
2576 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2577 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2578 continue;
2579 }
2580
2581 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002582 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002583 VkImageView image_view{VK_NULL_HANDLE};
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002584
2585 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2586 switch (descriptor->GetClass()) {
2587 case cvdescriptorset::DescriptorClass::Image: {
2588 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2589 image_view = image_descriptor->GetImageView();
2590 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002591 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002592 }
2593 case cvdescriptorset::DescriptorClass::ImageSampler: {
2594 if (const auto image_sampler_descriptor =
2595 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2596 image_view = image_sampler_descriptor->GetImageView();
2597 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002598 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002599 }
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002600 default:
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002601 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002602 }
2603
2604 if (image_view) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002605 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002606 QueueValidateImageView(cb_state->queue_submit_functions, function_name, image_view_state.get(),
2607 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002608 }
2609 }
2610 }
2611 }
2612 }
2613}
2614
2615void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2616 uint32_t firstVertex, uint32_t firstInstance) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002617 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002618}
2619
2620void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2621 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002622 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002623}
2624
2625void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2626 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002627 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002628}
2629
Camden5b184be2019-08-13 07:50:19 -06002630bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002631 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002632 bool skip = false;
2633
2634 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002635 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2636 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2637 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2638 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002639 }
2640
2641 return skip;
2642}
Camden83a9c372019-08-14 11:41:38 -06002643
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002644bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2645 bool skip = false;
2646 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2647 skip |= ValidateCmdEndRenderPass(commandBuffer);
2648 return skip;
2649}
2650
2651bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2652 bool skip = false;
2653 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2654 skip |= ValidateCmdEndRenderPass(commandBuffer);
2655 return skip;
2656}
2657
Sam Walls0961ec02020-03-31 16:39:15 +01002658bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2659 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002660 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002661 skip |= ValidateCmdEndRenderPass(commandBuffer);
2662 return skip;
2663}
2664
2665bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2666 bool skip = false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07002667 const auto cmd = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002668
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002669 if (cmd == nullptr) return skip;
2670 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002671
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002672 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2673 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2674 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002675 if (uses_depth) {
2676 skip |= LogPerformanceWarning(
2677 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2678 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2679 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2680 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2681 VendorSpecificTag(kBPVendorArm));
2682 }
2683
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002684 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2685
2686 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2687
2688 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2689 // it is redundant to have it be part of the render pass.
2690 // Only consider it redundant if it will actually consume bandwidth, i.e.
2691 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2692 // as is using pure input attachments.
2693 // CLEAR -> STORE might be considered a "useful" thing to do, but
2694 // the optimal thing to do is to defer the clear until you're actually
2695 // going to render to the image.
2696
2697 uint32_t num_attachments = rp->createInfo.attachmentCount;
2698 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002699 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2700 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002701 continue;
2702 }
2703
2704 auto& attachment = rp->createInfo.pAttachments[i];
2705
2706 VkImageAspectFlags bandwidth_aspects = 0;
2707
2708 if (!FormatIsStencilOnly(attachment.format) &&
2709 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2710 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2711 if (FormatHasDepth(attachment.format)) {
2712 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2713 } else {
2714 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2715 }
2716 }
2717
2718 if (FormatHasStencil(attachment.format) &&
2719 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2720 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2721 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2722 }
2723
2724 if (!bandwidth_aspects) {
2725 continue;
2726 }
2727
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002728 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
2729 [&](const AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002730 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002731 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002732 untouched_aspects &= ~itr->aspects;
2733 }
2734
2735 if (untouched_aspects) {
2736 skip |= LogPerformanceWarning(
2737 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2738 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2739 "was never accessed by a pipeline or clear command. "
2740 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2741 "if the attachments are not intended to be accessed.",
2742 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2743 }
2744 }
2745 }
2746
Sam Walls0961ec02020-03-31 16:39:15 +01002747 return skip;
2748}
2749
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002750void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002751 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002752}
2753
2754void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002755 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002756}
2757
Camden Stocker9c051442019-11-06 14:28:43 -08002758bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2759 const char* api_name) const {
2760 bool skip = false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07002761 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002762
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002763 if (bp_pd_state) {
2764 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2765 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2766 "Potential problem with calling %s() without first retrieving properties from "
2767 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2768 api_name);
2769 }
Camden Stocker9c051442019-11-06 14:28:43 -08002770 }
2771
2772 return skip;
2773}
2774
Camden83a9c372019-08-14 11:41:38 -06002775bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002776 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002777 bool skip = false;
2778
Camden Stocker9c051442019-11-06 14:28:43 -08002779 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002780
Camden Stocker9c051442019-11-06 14:28:43 -08002781 return skip;
2782}
2783
2784bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2785 uint32_t planeIndex,
2786 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2787 bool skip = false;
2788
2789 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2790
2791 return skip;
2792}
2793
2794bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2795 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2796 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2797 bool skip = false;
2798
2799 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002800
2801 return skip;
2802}
Camden05de2d42019-08-19 10:23:56 -06002803
2804bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002805 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002806 bool skip = false;
2807
Jeremy Gebben78684b12022-02-23 17:31:56 -07002808 auto swapchain_state = Get<SWAPCHAIN_STATE_BP>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06002809
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002810 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002811 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002812 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002813 skip |=
2814 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2815 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2816 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002817 }
Camden05de2d42019-08-19 10:23:56 -06002818
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002819 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2820 skip |= LogWarning(
2821 device, kVUID_BestPractices_Swapchain_InvalidCount,
2822 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002823 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002824 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2825 }
2826 }
2827
Camden05de2d42019-08-19 10:23:56 -06002828 return skip;
2829}
2830
2831// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002832bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002833 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002834 const CALL_STATE call_state,
2835 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002836 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002837 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2838 if (UNCALLED == call_state) {
2839 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002840 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002841 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2842 "recommended "
2843 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2844 caller_name, caller_name);
2845 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002846 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2847 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002848 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2849 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2850 ". It is recommended to instead receive all the properties by calling %s with "
2851 "pQueueFamilyPropertyCount that was "
2852 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002853 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002854 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002855 }
2856
2857 return skip;
2858}
2859
Jeff Bolz5c801d12019-10-09 10:38:45 -05002860bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2861 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002862 bool skip = false;
2863
2864 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002865 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002866 if (!as_state->memory_requirements_checked) {
2867 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2868 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2869 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002870 skip |= LogWarning(
2871 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002872 "vkBindAccelerationStructureMemoryNV(): "
2873 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2874 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2875 }
2876 }
2877
2878 return skip;
2879}
2880
Camden05de2d42019-08-19 10:23:56 -06002881bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2882 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002883 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002884 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002885 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002886 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002887 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2888 "vkGetPhysicalDeviceQueueFamilyProperties()");
2889 }
2890 return false;
Camden05de2d42019-08-19 10:23:56 -06002891}
2892
Mike Schuchardt2df08912020-12-15 16:28:09 -08002893bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2894 uint32_t* pQueueFamilyPropertyCount,
2895 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002896 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002897 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002898 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002899 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2900 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2901 }
2902 return false;
Camden05de2d42019-08-19 10:23:56 -06002903}
2904
Jeff Bolz5c801d12019-10-09 10:38:45 -05002905bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002906 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben78684b12022-02-23 17:31:56 -07002907 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002908 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002909 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002910 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2911 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2912 }
2913 return false;
Camden05de2d42019-08-19 10:23:56 -06002914}
2915
2916bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2917 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002918 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002919 if (!pSurfaceFormats) return false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07002920 const auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002921 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002922 bool skip = false;
2923 if (call_state == UNCALLED) {
2924 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2925 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002926 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2927 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2928 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002929 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002930 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002931 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2932 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2933 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2934 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002935 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002936 }
2937 }
2938 return skip;
2939}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002940
2941bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002942 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002943 bool skip = false;
2944
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002945 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2946 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002947 // 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 -07002948 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002949 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2950 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002951 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002952 // 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 -07002953 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2954 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002955 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002956 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002957 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002958 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002959 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002960 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2961 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2962 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002963 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002964 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2965 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002966 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002967 }
2968 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002969 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002970 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002971 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002972 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2973 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002974 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002975 }
2976 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002977 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2978 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002979 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002980 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002981 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002982 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002983 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002984 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2985 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2986 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002987 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002988 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2989 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002990 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002991 }
2992 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002993 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002994 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002995 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002996 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2997 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002998 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002999 }
3000 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3001 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003002 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003003 }
3004 }
3005 }
3006 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003007 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
3008 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06003009 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003010 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003011 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
3012 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003013 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003014 }
3015 }
3016 }
3017
3018 return skip;
3019}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003020
Mark Lobodzinski84101d72020-04-24 09:43:48 -06003021void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
3022 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003023 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003024 return;
3025 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003026
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003027 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3028 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
3029 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3030 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003031 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003032 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003033 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003034 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003035 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3036 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
3037 image_state->sparse_metadata_bound = true;
3038 }
3039 }
3040 }
3041 }
3042}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003043
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003044bool BestPractices::ClearAttachmentsIsFullClear(const CMD_BUFFER_STATE_BP* cmd, uint32_t rectCount,
3045 const VkClearRect* pRects) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003046 if (cmd->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3047 // We don't know the accurate render area in a secondary,
3048 // so assume we clear the entire frame buffer.
3049 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
3050 return true;
3051 }
3052
3053 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
3054 for (uint32_t i = 0; i < rectCount; i++) {
3055 auto& rect = pRects[i];
3056 auto& render_area = cmd->activeRenderPassBeginInfo.renderArea;
3057 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
3058 return true;
3059 }
3060 }
3061
3062 return false;
3063}
3064
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003065bool BestPractices::ValidateClearAttachment(VkCommandBuffer commandBuffer, const CMD_BUFFER_STATE_BP* cmd, uint32_t fb_attachment,
3066 uint32_t color_attachment, VkImageAspectFlags aspects, bool secondary) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003067 const RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3068 bool skip = false;
3069
3070 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3071 return skip;
3072 }
3073
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003074 const auto& rp_state = cmd->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003075
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003076 auto attachment_itr = std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02003077 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003078 return info.framebufferAttachment == fb_attachment;
3079 });
3080
3081 // Only report aspects which haven't been touched yet.
3082 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003083 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003084 new_aspects &= ~attachment_itr->aspects;
3085 }
3086
3087 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
3088 if (!cmd->hasDrawCmd) {
3089 skip |= LogPerformanceWarning(
3090 commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003091 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3092 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003093 report_data->FormatHandle(commandBuffer).c_str());
3094 }
3095
3096 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3097 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3098 skip |= LogPerformanceWarning(
3099 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3100 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3101 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3102 "it is more efficient.",
3103 secondary ? "vkCmdExecuteCommands(): " : "",
3104 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
3105 }
3106
3107 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3108 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3109 skip |= LogPerformanceWarning(
3110 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3111 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3112 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3113 "it is more efficient.",
3114 secondary ? "vkCmdExecuteCommands(): " : "",
3115 report_data->FormatHandle(commandBuffer).c_str());
3116 }
3117
3118 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3119 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3120 skip |= LogPerformanceWarning(
3121 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3122 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3123 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3124 "it is more efficient.",
3125 secondary ? "vkCmdExecuteCommands(): " : "",
3126 report_data->FormatHandle(commandBuffer).c_str());
3127 }
3128
3129 return skip;
3130}
3131
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003132bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003133 const VkClearAttachment* pAttachments, uint32_t rectCount,
3134 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003135 bool skip = false;
Jeremy Gebben78684b12022-02-23 17:31:56 -07003136 const auto cb_node = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003137 if (!cb_node) return skip;
3138
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003139 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3140 // Defer checks to ExecuteCommands.
3141 return skip;
3142 }
3143
3144 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben9f537102021-10-05 16:37:12 -06003145 if (!ClearAttachmentsIsFullClear(cb_node.get(), rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003146 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003147 }
3148
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003149 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3150 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003151 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003152 if (rp) {
3153 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3154
3155 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003156 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003157
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003158 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3159 uint32_t color_attachment = attachment.colorAttachment;
3160 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003161 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, color_attachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003162 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003163 }
3164
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003165 if (subpass.pDepthStencilAttachment &&
3166 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003167 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003168 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, VK_ATTACHMENT_UNUSED,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003169 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003170 }
3171 }
3172 }
3173
Nadav Gevaf0808442021-05-21 13:51:25 -04003174 if (VendorCheckEnabled(kBPVendorAMD)) {
3175 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3176 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3177 bool black_check = false;
3178 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3179 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3180 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3181 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3182 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3183
3184 bool white_check = false;
3185 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3186 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3187 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3188 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3189 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3190
3191 if (black_check && white_check) {
3192 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3193 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3194 "Consider changing to one of the following:"
3195 "RGBA(0, 0, 0, 0) "
3196 "RGBA(0, 0, 0, 1) "
3197 "RGBA(1, 1, 1, 0) "
3198 "RGBA(1, 1, 1, 1)",
3199 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3200 }
3201 } else {
3202 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3203 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3204 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3205 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3206 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3207 "attachment %" PRId32 " is not a fast clear value."
3208 "Consider changing to one of the following:"
3209 "D=0.0f, S=0"
3210 "D=1.0f, S=0",
3211 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3212 }
3213 }
3214 }
3215 }
3216
Camden Stockerf55721f2019-09-09 11:04:49 -06003217 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003218}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003219
3220bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3221 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3222 const VkImageResolve* pRegions) const {
3223 bool skip = false;
3224
3225 skip |= VendorCheckEnabled(kBPVendorArm) &&
3226 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3227 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3228 "This is a very slow and extremely bandwidth intensive path. "
3229 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3230 VendorSpecificTag(kBPVendorArm));
3231
3232 return skip;
3233}
3234
Jeff Leger178b1e52020-10-05 12:22:23 -04003235bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3236 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3237 bool skip = false;
3238
3239 skip |= VendorCheckEnabled(kBPVendorArm) &&
3240 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3241 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3242 "This is a very slow and extremely bandwidth intensive path. "
3243 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3244 VendorSpecificTag(kBPVendorArm));
3245
3246 return skip;
3247}
3248
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003249bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3250 const VkResolveImageInfo2* pResolveImageInfo) const {
3251 bool skip = false;
3252
3253 skip |= VendorCheckEnabled(kBPVendorArm) &&
3254 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3255 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3256 "This is a very slow and extremely bandwidth intensive path. "
3257 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3258 VendorSpecificTag(kBPVendorArm));
3259
3260 return skip;
3261}
3262
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003263void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3264 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3265 const VkImageResolve* pRegions) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003266 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003267 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003268 auto* src = GetImageUsageState(srcImage);
3269 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003270
3271 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003272 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3273 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003274 }
3275}
3276
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003277void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3278 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003279 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003280 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003281 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3282 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003283 uint32_t regionCount = pResolveImageInfo->regionCount;
3284
3285 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003286 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3287 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003288 }
3289}
3290
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003291void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3292 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003293 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003294 auto& funcs = cb->queue_submit_functions;
3295 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3296 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
3297 uint32_t regionCount = pResolveImageInfo->regionCount;
3298
3299 for (uint32_t i = 0; i < regionCount; i++) {
3300 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3301 pResolveImageInfo->pRegions[i].srcSubresource);
3302 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3303 pResolveImageInfo->pRegions[i].dstSubresource);
3304 }
3305}
3306
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003307void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3308 const VkClearColorValue* pColor, uint32_t rangeCount,
3309 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003310 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003311 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003312 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003313
3314 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003315 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003316 }
3317}
3318
3319void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3320 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3321 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003322 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003323 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003324 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003325
3326 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003327 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003328 }
3329}
3330
3331void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3332 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3333 const VkImageCopy* pRegions) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003334 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003335 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003336 auto* src = GetImageUsageState(srcImage);
3337 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003338
3339 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003340 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3341 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003342 }
3343}
3344
3345void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3346 VkImageLayout dstImageLayout, uint32_t regionCount,
3347 const VkBufferImageCopy* pRegions) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003348 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003349 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003350 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003351
3352 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003353 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003354 }
3355}
3356
3357void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3358 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003359 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003360 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003361 auto* src = GetImageUsageState(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003362
3363 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003364 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003365 }
3366}
3367
3368void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3369 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3370 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003371 auto cb = Get<CMD_BUFFER_STATE_BP>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003372 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003373 auto* src = GetImageUsageState(srcImage);
3374 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003375
3376 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003377 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3378 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003379 }
3380}
3381
Attilio Provenzano02859b22020-02-27 14:17:28 +00003382bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3383 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3384 bool skip = false;
3385
3386 if (VendorCheckEnabled(kBPVendorArm)) {
3387 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3388 skip |= LogPerformanceWarning(
3389 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3390 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3391 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3392 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003393 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003394 }
3395
3396 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3397 skip |= LogPerformanceWarning(
3398 device, kVUID_BestPractices_CreateSampler_LodClamping,
3399 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3400 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3401 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3402 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3403 }
3404
3405 if (pCreateInfo->mipLodBias != 0.0f) {
3406 skip |=
3407 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3408 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3409 "descriptors being created and may cause reduced performance.",
3410 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3411 }
3412
3413 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3414 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3415 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3416 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3417 skip |= LogPerformanceWarning(
3418 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3419 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3420 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3421 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3422 VendorSpecificTag(kBPVendorArm));
3423 }
3424
3425 if (pCreateInfo->unnormalizedCoordinates) {
3426 skip |= LogPerformanceWarning(
3427 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3428 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3429 "descriptors being created and may cause reduced performance.",
3430 VendorSpecificTag(kBPVendorArm));
3431 }
3432
3433 if (pCreateInfo->anisotropyEnable) {
3434 skip |= LogPerformanceWarning(
3435 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3436 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3437 "and may cause reduced performance.",
3438 VendorSpecificTag(kBPVendorArm));
3439 }
3440 }
3441
3442 return skip;
3443}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003444
Nadav Gevaf0808442021-05-21 13:51:25 -04003445void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3446 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3447 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3448 void* cgpl_state) {
3449 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3450 pPipelines);
3451 // AMD best practice
3452 num_pso += createInfoCount;
3453}
3454
3455bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3456 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3457 const VkCopyDescriptorSet* pDescriptorCopies) const {
3458 bool skip = false;
3459 if (VendorCheckEnabled(kBPVendorAMD)) {
3460 if (descriptorCopyCount > 0) {
3461 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3462 "%s Performance warning: copying descriptor sets is not recommended",
3463 VendorSpecificTag(kBPVendorAMD));
3464 }
3465 }
3466
3467 return skip;
3468}
3469
3470bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3471 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3472 const VkAllocationCallbacks* pAllocator,
3473 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3474 bool skip = false;
3475 if (VendorCheckEnabled(kBPVendorAMD)) {
3476 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3477 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3478 "vkUpdateDescriptorSet instead",
3479 VendorSpecificTag(kBPVendorAMD));
3480 }
3481
3482 return skip;
3483}
3484
3485bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3486 const VkClearColorValue* pColor, uint32_t rangeCount,
3487 const VkImageSubresourceRange* pRanges) const {
3488 bool skip = false;
3489 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003490 skip |= LogPerformanceWarning(
3491 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003492 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3493 "vkCmdClearAttachments instead",
3494 VendorSpecificTag(kBPVendorAMD));
3495 }
3496
3497 return skip;
3498}
3499
3500bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3501 VkImageLayout imageLayout,
3502 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3503 const VkImageSubresourceRange* pRanges) const {
3504 bool skip = false;
3505 if (VendorCheckEnabled(kBPVendorAMD)) {
3506 skip |= LogPerformanceWarning(
3507 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3508 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3509 "vkCmdClearAttachments instead",
3510 VendorSpecificTag(kBPVendorAMD));
3511 }
3512
3513 return skip;
3514}
3515
3516bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3517 const VkAllocationCallbacks* pAllocator,
3518 VkPipelineLayout* pPipelineLayout) const {
3519 bool skip = false;
3520 if (VendorCheckEnabled(kBPVendorAMD)) {
3521 // Descriptor sets cost 1 DWORD each.
3522 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3523 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3524 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3525 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3526 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003527 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Nadav Gevaf0808442021-05-21 13:51:25 -04003528 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * (robust_buffer_access ? 4 : 2);
3529 }
3530
3531 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3532 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3533 }
3534
3535 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3536 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3537 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3538 "Descriptor sets cost 1 DWORD each. "
3539 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3540 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3541 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3542 VendorSpecificTag(kBPVendorAMD));
3543 }
3544 }
3545
3546 return skip;
3547}
3548
3549bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3550 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3551 const VkImageCopy* pRegions) const {
3552 bool skip = false;
3553 std::stringstream src_image_hex;
3554 std::stringstream dst_image_hex;
3555 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3556 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3557
3558 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003559 auto src_state = Get<IMAGE_STATE>(srcImage);
3560 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003561
3562 if (src_state && dst_state) {
3563 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3564 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3565 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3566 skip |=
3567 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3568 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3569 "image (vkCmdCopyImageToBuffer) "
3570 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3571 "copies when converting between linear and optimal images",
3572 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3573 }
3574 }
3575 }
3576
3577 return skip;
3578}
3579
3580bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3581 VkPipeline pipeline) const {
3582 bool skip = false;
3583
3584 if (VendorCheckEnabled(kBPVendorAMD)) {
3585 if (pipelines_used_in_frame.find(pipeline) != pipelines_used_in_frame.end()) {
3586 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3587 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3588 "for example, by sorting draw calls by pipeline.",
3589 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3590 }
3591 }
3592
3593 return skip;
3594}
3595
3596void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3597 VkFence fence, VkResult result) {
3598 // AMD best practice
3599 num_queue_submissions += submitCount;
3600}
3601
3602bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3603 bool skip = false;
3604
3605 if (VendorCheckEnabled(kBPVendorAMD)) {
3606 if (num_queue_submissions > kNumberOfSubmissionWarningLimitAMD) {
3607 skip |= LogPerformanceWarning(
3608 device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3609 "%s Performance warning: command buffers submitted %" PRId32 " times this frame. Submitting command buffers has a CPU "
3610 "and GPU overhead. Submit fewer times to incur less overhead.",
3611 VendorSpecificTag(kBPVendorAMD), num_queue_submissions);
3612 }
3613 }
3614
3615 return skip;
3616}
3617
3618void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3619 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3620 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3621 uint32_t bufferMemoryBarrierCount,
3622 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3623 uint32_t imageMemoryBarrierCount,
3624 const VkImageMemoryBarrier* pImageMemoryBarriers) {
3625 num_barriers_objects += memoryBarrierCount;
3626 num_barriers_objects += imageMemoryBarrierCount;
3627 num_barriers_objects += bufferMemoryBarrierCount;
3628}
3629
3630void BestPractices::ManualPostCallRecordCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3631 const VkAllocationCallbacks* pAllocator, VkFence* pFence, VkResult result) {
3632 // AMD best practice
3633 if (result == VK_SUCCESS) {
3634 num_fence_objects++;
3635 }
3636}
3637
3638void BestPractices::ManualPostCallRecordCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3639 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore,
3640 VkResult result) {
3641 // AMD best practice
3642 if (result == VK_SUCCESS) {
3643 num_semaphore_objects++;
3644 }
3645}
3646
3647bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3648 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3649 bool skip = false;
3650 if (VendorCheckEnabled(kBPVendorAMD)) {
3651 if (num_semaphore_objects > kMaxRecommendedSemaphoreObjectsSizeAMD) {
3652 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3653 "%s Performance warning: High number of vkSemaphore objects created."
3654 "Minimize the amount of queue synchronization that is used. "
3655 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3656 VendorSpecificTag(kBPVendorAMD));
3657 }
3658 }
3659
3660 return skip;
3661}
3662
3663bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3664 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3665 bool skip = false;
3666 if (VendorCheckEnabled(kBPVendorAMD)) {
3667 if (num_fence_objects > kMaxRecommendedFenceObjectsSizeAMD) {
3668 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3669 "%s Performance warning: High number of VkFence objects created."
3670 "Minimize the amount of CPU-GPU synchronization that is used. "
3671 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3672 VendorSpecificTag(kBPVendorAMD));
3673 }
3674 }
3675
3676 return skip;
3677}
3678
Sam Walls8e77e4f2020-03-16 20:47:40 +00003679void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3680
3681bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3682 // look for a cache hit
3683 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3684 if (hit != _entries.end()) {
3685 // mark the cache hit as being most recently used
3686 hit->age = iteration++;
3687 return true;
3688 }
3689
3690 // if there's no cache hit, we need to model the entry being inserted into the cache
3691 CacheEntry new_entry = {value, iteration};
3692 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3693 // if there is still space left in the cache, use the next available slot
3694 *(_entries.begin() + iteration) = new_entry;
3695 } else {
3696 // otherwise replace the least recently used cache entry
3697 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3698 *lru = new_entry;
3699 }
3700 iteration++;
3701 return false;
3702}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003703
3704bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3705 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003706 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003707 bool skip = false;
3708 if (swapchain_data && swapchain_data->images.size() == 0) {
3709 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3710 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3711 "vkGetSwapchainImagesKHR after swapchain creation.");
3712 }
3713 return skip;
3714}
3715
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003716void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3717 if (no_pointer) {
3718 if (UNCALLED == call_state) {
3719 call_state = QUERY_COUNT;
3720 }
3721 } else { // Save queue family properties
3722 call_state = QUERY_DETAILS;
3723 }
3724}
3725
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003726void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3727 uint32_t* pQueueFamilyPropertyCount,
3728 VkQueueFamilyProperties* pQueueFamilyProperties) {
3729 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3730 pQueueFamilyProperties);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003731 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003732 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003733 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3734 nullptr == pQueueFamilyProperties);
3735 }
3736}
3737
3738void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3739 uint32_t* pQueueFamilyPropertyCount,
3740 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3741 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3742 pQueueFamilyProperties);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003743 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003744 if (bp_pd_state) {
3745 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3746 nullptr == pQueueFamilyProperties);
3747 }
3748}
3749
3750void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3751 uint32_t* pQueueFamilyPropertyCount,
3752 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3753 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3754 pQueueFamilyProperties);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003755 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003756 if (bp_pd_state) {
3757 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3758 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003759 }
3760}
3761
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003762void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3763 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003764 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003765 if (bp_pd_state) {
3766 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3767 }
3768}
3769
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003770void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3771 VkPhysicalDeviceFeatures2* pFeatures) {
3772 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003773 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003774 if (bp_pd_state) {
3775 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3776 }
3777}
3778
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003779void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3780 VkPhysicalDeviceFeatures2* pFeatures) {
3781 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben78684b12022-02-23 17:31:56 -07003782 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003783 if (bp_pd_state) {
3784 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3785 }
3786}
3787
3788void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3789 VkSurfaceKHR surface,
3790 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3791 VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003792 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003793 if (bp_pd_state) {
3794 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3795 }
3796}
3797
3798void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3799 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3800 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003801 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003802 if (bp_pd_state) {
3803 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3804 }
3805}
3806
3807void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3808 VkSurfaceKHR surface,
3809 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3810 VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003811 auto bp_pd_state = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003812 if (bp_pd_state) {
3813 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3814 }
3815}
3816
3817void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3818 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3819 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003820 auto bp_pd_data = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003821 if (bp_pd_data) {
3822 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3823
3824 if (*pPresentModeCount) {
3825 if (call_state < QUERY_COUNT) {
3826 call_state = QUERY_COUNT;
3827 }
3828 }
3829 if (pPresentModes) {
3830 if (call_state < QUERY_DETAILS) {
3831 call_state = QUERY_DETAILS;
3832 }
3833 }
3834 }
3835}
3836
3837void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3838 uint32_t* pSurfaceFormatCount,
3839 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003840 auto bp_pd_data = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003841 if (bp_pd_data) {
3842 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3843
3844 if (*pSurfaceFormatCount) {
3845 if (call_state < QUERY_COUNT) {
3846 call_state = QUERY_COUNT;
3847 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003848 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003849 }
3850 if (pSurfaceFormats) {
3851 if (call_state < QUERY_DETAILS) {
3852 call_state = QUERY_DETAILS;
3853 }
3854 }
3855 }
3856}
3857
3858void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3859 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3860 uint32_t* pSurfaceFormatCount,
3861 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003862 auto bp_pd_data = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003863 if (bp_pd_data) {
3864 if (*pSurfaceFormatCount) {
3865 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3866 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3867 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003868 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003869 }
3870 if (pSurfaceFormats) {
3871 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3872 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3873 }
3874 }
3875 }
3876}
3877
3878void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3879 uint32_t* pPropertyCount,
3880 VkDisplayPlanePropertiesKHR* pProperties,
3881 VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003882 auto bp_pd_data = Get<PHYSICAL_DEVICE_STATE_BP>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003883 if (bp_pd_data) {
3884 if (*pPropertyCount) {
3885 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3886 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3887 }
3888 }
3889 if (pProperties) {
3890 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3891 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3892 }
3893 }
3894 }
3895}
3896
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003897void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3898 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3899 VkResult result) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003900 auto swapchain_state = Get<SWAPCHAIN_STATE_BP>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003901 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3902 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3903 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003904 }
3905 }
3906}
3907
Nadav Gevaf0808442021-05-21 13:51:25 -04003908void BestPractices::ManualPostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
3909 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003910 if (VK_SUCCESS == result) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003911 if ((pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
3912 robust_buffer_access = true;
3913 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003914 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003915}
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003916
3917void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3918 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3919
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003920 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003921 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003922 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003923 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben78684b12022-02-23 17:31:56 -07003924 auto cb = Get<CMD_BUFFER_STATE_BP>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003925 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003926 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003927 }
3928 }
3929 }
3930}