blob: eeb2cedf3863c5ec4714ce6022934f80e0dd4376 [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 Gebben383b9a32021-09-08 16:31:33 -0600246 const auto bp_pd_state = GetPhysicalDeviceState(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
Attilio Provenzano02859b22020-02-27 14:17:28 +0000299 if (VendorCheckEnabled(kBPVendorArm)) {
300 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
301 skip |= LogPerformanceWarning(
302 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
303 "%s vkCreateImage(): Trying to create an image with %u samples. "
304 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
305 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
306 }
307
308 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
309 skip |= LogPerformanceWarning(
310 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
311 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
312 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
313 "and do not need to be backed by physical storage. "
314 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
315 VendorSpecificTag(kBPVendorArm));
316 }
317 }
318
Nadav Gevaf0808442021-05-21 13:51:25 -0400319 if (VendorCheckEnabled(kBPVendorAMD)) {
320 std::stringstream image_hex;
321 image_hex << "0x" << std::hex << HandleToUint64(pImage);
322
323 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
324 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
325 skip |= LogPerformanceWarning(device,
326 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
327 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
328 "Using a SHARING_MODE_CONCURRENT "
329 "is not recommended with color and depth targets",
330 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
331 }
332
333 if ((pCreateInfo->usage &
334 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
335 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
336 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
337 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
338 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
339 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
340 }
341
342 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
343 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
344 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
345 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
346 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
347 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
348 }
349 }
350
Camden5b184be2019-08-13 07:50:19 -0600351 return skip;
352}
353
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100354void BestPractices::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
355 ValidationStateTracker::PreCallRecordDestroyImage(device, image, pAllocator);
356 ReleaseImageUsageState(image);
357}
358
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200359void BestPractices::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
Tony-LunarG299187b2021-05-28 09:29:12 -0600360 if (VK_NULL_HANDLE != swapchain) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600361 auto chain = Get<SWAPCHAIN_NODE>(swapchain);
Tony-LunarG299187b2021-05-28 09:29:12 -0600362 for (auto& image : chain->images) {
363 if (image.image_state) {
364 ReleaseImageUsageState(image.image_state->image());
365 }
366 }
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200367 }
368 ValidationStateTracker::PreCallRecordDestroySwapchainKHR(device, swapchain, pAllocator);
369}
370
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100371IMAGE_STATE_BP* BestPractices::GetImageUsageState(VkImage vk_image) {
372 auto itr = imageUsageMap.find(vk_image);
373 if (itr != imageUsageMap.end()) {
374 return &itr->second;
375 } else {
376 auto& state = imageUsageMap[vk_image];
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600377 auto image = Get<IMAGE_STATE>(vk_image);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600378 state.image = image.get();
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100379 state.usages.resize(image->createInfo.arrayLayers);
380 for (auto& mips : state.usages) {
381 mips.resize(image->createInfo.mipLevels, IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED);
382 }
383 return &state;
384 }
385}
386
387void BestPractices::ReleaseImageUsageState(VkImage image) {
388 auto itr = imageUsageMap.find(image);
389 if (itr != imageUsageMap.end()) {
390 imageUsageMap.erase(itr);
391 }
392}
393
Camden5b184be2019-08-13 07:50:19 -0600394bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500395 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600396 bool skip = false;
397
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600398 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600399 if (bp_pd_state) {
400 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
401 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
402 "vkCreateSwapchainKHR() called before getting surface capabilities from "
403 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
404 }
Camden83a9c372019-08-14 11:41:38 -0600405
Shannon McPherson73e58c82021-03-05 17:14:26 -0700406 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
407 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600408 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
409 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
410 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
411 }
Camden83a9c372019-08-14 11:41:38 -0600412
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600413 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
414 skip |= LogWarning(
415 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
416 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
417 }
Camden83a9c372019-08-14 11:41:38 -0600418 }
419
Camden5b184be2019-08-13 07:50:19 -0600420 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700421 skip |=
422 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600423 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700424 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
425 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600426 }
427
Szilard Papp48a6da32020-06-10 14:41:59 +0100428 if (pCreateInfo->minImageCount == 2) {
429 skip |= LogPerformanceWarning(
430 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
431 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
432 ", which means double buffering is going "
433 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
434 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
435 "3 to use triple buffering to maximize performance in such cases.",
436 pCreateInfo->minImageCount);
437 }
438
Szilard Pappd5f0f812020-06-22 09:01:29 +0100439 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
440 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
441 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
442 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
443 "Presentation modes which are not FIFO will present the latest available frame and discard other "
444 "frame(s) if any.",
445 VendorSpecificTag(kBPVendorArm));
446 }
447
Camden5b184be2019-08-13 07:50:19 -0600448 return skip;
449}
450
451bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
452 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500453 const VkAllocationCallbacks* pAllocator,
454 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600455 bool skip = false;
456
457 for (uint32_t i = 0; i < swapchainCount; i++) {
458 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700459 skip |= LogWarning(
460 device, kVUID_BestPractices_SharingModeExclusive,
461 "Warning: A shared swapchain (index %" PRIu32
462 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
463 "queues (queueFamilyIndexCount of %" PRIu32 ").",
464 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600465 }
466 }
467
468 return skip;
469}
470
471bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500472 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600473 bool skip = false;
474
475 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
476 VkFormat format = pCreateInfo->pAttachments[i].format;
477 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
478 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
479 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700480 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
481 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
482 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
483 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
484 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600485 }
486 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == 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 stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
489 "and 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 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000494
495 const auto& attachment = pCreateInfo->pAttachments[i];
496 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
497 bool access_requires_memory =
498 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
499
500 if (FormatHasStencil(format)) {
501 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
502 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
503 }
504
505 if (access_requires_memory) {
506 skip |= LogPerformanceWarning(
507 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
508 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
509 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
510 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
511 i, static_cast<uint32_t>(attachment.samples));
512 }
513 }
Camden5b184be2019-08-13 07:50:19 -0600514 }
515
516 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
517 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
518 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
519 }
520
521 return skip;
522}
523
Tony-LunarG767180f2020-04-23 14:03:59 -0600524bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
525 const VkImageView* image_views) const {
526 bool skip = false;
527
528 // Check for non-transient attachments that should be transient and vice versa
529 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200530 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600531 bool attachment_should_be_transient =
532 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
533
534 if (FormatHasStencil(attachment.format)) {
535 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
536 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
537 }
538
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600539 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600540 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600541 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600542
543 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
544
545 // The check for an image that should not be transient applies to all GPUs
546 if (!attachment_should_be_transient && image_is_transient) {
547 skip |= LogPerformanceWarning(
548 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
549 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
550 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
551 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
552 i);
553 }
554
555 bool supports_lazy = false;
556 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
557 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
558 supports_lazy = true;
559 }
560 }
561
562 // The check for an image that should be transient only applies to GPUs supporting
563 // lazily allocated memory
564 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
565 skip |= LogPerformanceWarning(
566 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
567 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
568 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
569 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
570 i);
571 }
572 }
573 }
574 return skip;
575}
576
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000577bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
578 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
579 bool skip = false;
580
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600581 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800582 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600583 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000584 }
585
586 return skip;
587}
588
Sam Wallse746d522020-03-16 21:20:23 +0000589bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
590 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
591 bool skip = false;
592 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
593
594 if (!skip) {
595 const auto& pool_handle = pAllocateInfo->descriptorPool;
596 auto iter = descriptor_pool_freed_count.find(pool_handle);
597 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
598 // this warning is specific to Arm
599 if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) {
600 skip |= LogPerformanceWarning(
601 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
602 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
603 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
604 VendorSpecificTag(kBPVendorArm));
605 }
606 }
607
608 return skip;
609}
610
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600611void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
612 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000613 if (result == VK_SUCCESS) {
614 // find the free count for the pool we allocated into
615 auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool);
616 if (iter != descriptor_pool_freed_count.end()) {
617 // we record successful allocations by subtracting the allocation count from the last recorded free count
618 const auto alloc_count = pAllocateInfo->descriptorSetCount;
619 // clamp the unsigned subtraction to the range [0, last_free_count]
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700620 if (iter->second > alloc_count) {
Sam Wallse746d522020-03-16 21:20:23 +0000621 iter->second -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700622 } else {
Sam Wallse746d522020-03-16 21:20:23 +0000623 iter->second = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700624 }
Sam Wallse746d522020-03-16 21:20:23 +0000625 }
626 }
627}
628
629void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
630 const VkDescriptorSet* pDescriptorSets, VkResult result) {
631 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
632 if (result == VK_SUCCESS) {
633 // we want to track frees because we're interested in suggesting re-use
634 auto iter = descriptor_pool_freed_count.find(descriptorPool);
635 if (iter == descriptor_pool_freed_count.end()) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700636 descriptor_pool_freed_count.emplace(descriptorPool, descriptorSetCount);
Sam Wallse746d522020-03-16 21:20:23 +0000637 } else {
638 iter->second += descriptorSetCount;
639 }
640 }
641}
642
Camden5b184be2019-08-13 07:50:19 -0600643bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500644 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600645 bool skip = false;
646
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500647 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700648 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
649 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600650 }
651
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000652 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
653 skip |= LogPerformanceWarning(
654 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600655 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
656 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000657 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
658 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
659 }
660
Camden83a9c372019-08-14 11:41:38 -0600661 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
662
663 return skip;
664}
665
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600666void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
667 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
668 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700669 if (result != VK_SUCCESS) {
670 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
671 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800672 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700673 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600674 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700675 return;
676 }
677 num_mem_objects++;
678}
Camden Stocker9738af92019-10-16 13:54:03 -0700679
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600680void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
681 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700682 auto error = std::find(error_codes.begin(), error_codes.end(), result);
683 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000684 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
685 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
686
687 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
688 if (common_failure != common_failure_codes.end()) {
689 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
690 } else {
691 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
692 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700693 return;
694 }
695 auto success = std::find(success_codes.begin(), success_codes.end(), result);
696 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600697 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
698 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500699 }
700}
701
Jeff Bolz5c801d12019-10-09 10:38:45 -0500702bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
703 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700704 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600705 bool skip = false;
706
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700707 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600708
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700709 for (const auto& item : mem_info->ObjectBindings()) {
710 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600711 LogObjectList objlist(device);
712 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600713 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600714 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 -0600715 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600716 }
717
Camden5b184be2019-08-13 07:50:19 -0600718 return skip;
719}
720
721void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700722 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
Camden5b184be2019-08-13 07:50:19 -0600723 if (memory != VK_NULL_HANDLE) {
724 num_mem_objects--;
725 }
726}
727
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000728bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600729 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700730 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600731
sfricke-samsunge2441192019-11-06 14:07:57 -0800732 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700733 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
734 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
735 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600736 }
737
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700738 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000739
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300740 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000741 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
742 skip |= LogPerformanceWarning(
743 device, kVUID_BestPractices_SmallDedicatedAllocation,
744 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600745 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
746 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000747 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
748 }
749
Camden Stockerb603cc82019-09-03 10:09:02 -0600750 return skip;
751}
752
753bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500754 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600755 bool skip = false;
756 const char* api_name = "BindBufferMemory()";
757
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000758 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600759
760 return skip;
761}
762
763bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500764 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600765 char api_name[64];
766 bool skip = false;
767
768 for (uint32_t i = 0; i < bindInfoCount; i++) {
769 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000770 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600771 }
772
773 return skip;
774}
Camden Stockerb603cc82019-09-03 10:09:02 -0600775
776bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500777 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600778 char api_name[64];
779 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600780
Camden Stocker8b798ab2019-09-03 10:33:28 -0600781 for (uint32_t i = 0; i < bindInfoCount; i++) {
782 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000783 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600784 }
785
786 return skip;
787}
788
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000789bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600790 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700791 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600792
sfricke-samsung71bc6572020-04-29 15:49:43 -0700793 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600794 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700795 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
796 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
797 api_name, report_data->FormatHandle(image).c_str());
798 }
799 } else {
800 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
801 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600802 }
803
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700804 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000805
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600806 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000807 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
808 skip |= LogPerformanceWarning(
809 device, kVUID_BestPractices_SmallDedicatedAllocation,
810 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600811 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
812 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000813 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
814 }
815
816 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
817 // make sure this type is actually used.
818 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
819 // (i.e.most tile - based renderers)
820 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
821 bool supports_lazy = false;
822 uint32_t suggested_type = 0;
823
824 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600825 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000826 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
827 supports_lazy = true;
828 suggested_type = i;
829 break;
830 }
831 }
832 }
833
834 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
835
836 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
837 skip |= LogPerformanceWarning(
838 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600839 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000840 "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 -0600841 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600842 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000843 }
844 }
845
Camden Stocker8b798ab2019-09-03 10:33:28 -0600846 return skip;
847}
848
849bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500850 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600851 bool skip = false;
852 const char* api_name = "vkBindImageMemory()";
853
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000854 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600855
856 return skip;
857}
858
859bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500860 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600861 char api_name[64];
862 bool skip = false;
863
864 for (uint32_t i = 0; i < bindInfoCount; i++) {
865 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700866 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600867 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
868 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600869 }
870
871 return skip;
872}
873
874bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500875 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600876 char api_name[64];
877 bool skip = false;
878
879 for (uint32_t i = 0; i < bindInfoCount; i++) {
880 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000881 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600882 }
883
884 return skip;
885}
Camden83a9c372019-08-14 11:41:38 -0600886
Attilio Provenzano02859b22020-02-27 14:17:28 +0000887static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
888 switch (format) {
889 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
890 case VK_FORMAT_R16_SFLOAT:
891 case VK_FORMAT_R16G16_SFLOAT:
892 case VK_FORMAT_R16G16B16_SFLOAT:
893 case VK_FORMAT_R16G16B16A16_SFLOAT:
894 case VK_FORMAT_R32_SFLOAT:
895 case VK_FORMAT_R32G32_SFLOAT:
896 case VK_FORMAT_R32G32B32_SFLOAT:
897 case VK_FORMAT_R32G32B32A32_SFLOAT:
898 return false;
899
900 default:
901 return true;
902 }
903}
904
905bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
906 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
907 bool skip = false;
908
909 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700910 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000911
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700912 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
913 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
914 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000915 return skip;
916 }
917
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600918 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200919 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000920
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +0200921 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
922 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
923
924 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200925 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000926 uint32_t att = subpass.pColorAttachments[j].attachment;
927
928 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
929 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
930 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
931 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
932 "color attachment #%u makes use "
933 "of a format which cannot be blended at full throughput when using MSAA.",
934 VendorSpecificTag(kBPVendorArm), i, j);
935 }
936 }
937 }
938 }
939
940 return skip;
941}
942
Nadav Gevaf0808442021-05-21 13:51:25 -0400943void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
944 const VkComputePipelineCreateInfo* pCreateInfos,
945 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
946 VkResult result, void* pipe_state) {
947 // AMD best practice
948 pipeline_cache = pipelineCache;
949}
950
Camden5b184be2019-08-13 07:50:19 -0600951bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
952 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600953 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500954 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600955 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
956 pAllocator, pPipelines, cgpl_state_data);
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600957 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600958
959 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700960 skip |= LogPerformanceWarning(
961 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
962 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
963 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600964 }
965
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000966 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200967 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000968
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600969 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200970 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600971 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700972 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
973 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600974 count++;
975 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000976 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600977 if (count > kMaxInstancedVertexBuffers) {
978 skip |= LogPerformanceWarning(
979 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
980 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
981 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
982 count, kMaxInstancedVertexBuffers);
983 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000984 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000985
Szilard Pappaaf2da32020-06-22 10:37:35 +0100986 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
987 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200988 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
989 VendorCheckEnabled(kBPVendorArm)) {
990 skip |= LogPerformanceWarning(
991 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
992 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
993 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
994 "efficiency during rasterization. Consider disabling depthBias or increasing either "
995 "depthBiasConstantFactor or depthBiasSlopeFactor.",
996 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +0100997 }
998
Attilio Provenzano02859b22020-02-27 14:17:28 +0000999 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001000 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001001 if (VendorCheckEnabled(kBPVendorAMD)) {
1002 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1003 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
1004 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1005 "improve cache hit rate", VendorSpecificTag(kBPVendorAMD));
1006 }
1007
1008 if (num_pso > kMaxRecommendedNumberOfPSOAMD) {
1009 skip |=
1010 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
1011 "%s Performance warning: Too many pipelines created, consider consolidation",
1012 VendorSpecificTag(kBPVendorAMD));
1013 }
1014
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -06001015 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001016 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
1017 "%s Performance warning: Use of primitive restart is not recommended",
1018 VendorSpecificTag(kBPVendorAMD));
1019 }
1020
1021 // TODO: this might be too aggressive of a check
1022 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
1023 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
1024 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1025 VendorSpecificTag(kBPVendorAMD));
1026 }
1027 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001028
Camden5b184be2019-08-13 07:50:19 -06001029 return skip;
1030}
1031
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001032void BestPractices::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator)
1033{
1034 auto itr = graphicsPipelineCIs.find(pipeline);
1035 if (itr != graphicsPipelineCIs.end()) {
1036 graphicsPipelineCIs.erase(itr);
1037 }
1038 ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator);
1039}
1040
Sam Walls0961ec02020-03-31 16:39:15 +01001041void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1042 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1043 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1044 VkResult result, void* cgpl_state_data) {
1045 for (size_t i = 0; i < count; i++) {
1046 const auto* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
1047 const VkPipeline pipeline_handle = pPipelines[i];
1048
1049 // record depth stencil state and color blend states for depth pre-pass tracking purposes
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001050 GraphicsPipelineCIs& cis = graphicsPipelineCIs[pipeline_handle];
Sam Walls0961ec02020-03-31 16:39:15 +01001051
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001052 auto& create_info = cgpl_state->pCreateInfos[i];
Andrej Redeky0d513072022-02-13 14:38:20 +01001053
1054 if (create_info.renderPass == VK_NULL_HANDLE) {
1055 // TODO: this is necessary to avoid crashing
1056 LogWarning(device, kVUID_BestPractices_DynamicRendering_NotSupported,
1057 "vkCreateGraphicsPipelines: pCreateInfos[%" PRIu32
1058 "].renderPass is VK_NULL_HANDLE, VK_KHR_dynamic_rendering is not supported.\n",
1059 static_cast<uint32_t>(i));
1060 continue;
1061 }
1062
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001063 auto rp = Get<RENDER_PASS_STATE>(create_info.renderPass);
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001064
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001065 if (create_info.pColorBlendState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001066 // pColorBlendState is ignored if the pipeline has rasterization disabled or if no color attachments are used
1067 bool uses_color_attachments = false;
1068 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1069 uses_color_attachments |= rp->UsesColorAttachment(j);
1070 }
1071 if (uses_color_attachments && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1072 cis.colorBlendStateCI.emplace(create_info.pColorBlendState);
1073 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001074 }
1075
1076 if (create_info.pDepthStencilState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001077 // pDepthStencilState is ignored if the pipeline has rasterization disabled or if no depth/stencil attachment is used
1078 bool uses_depth_stencil = false;
1079 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1080 uses_depth_stencil |= rp->UsesDepthStencilAttachment(j);
1081 }
1082 if (uses_depth_stencil && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1083 cis.depthStencilStateCI.emplace(create_info.pDepthStencilState);
1084 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001085 }
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001086 // 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 +02001087 auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1088 cis.accessFramebufferAttachments.clear();
1089
1090 if (cis.colorBlendStateCI) {
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001091 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1092 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, cis.colorBlendStateCI->attachmentCount);
1093 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001094 if (cis.colorBlendStateCI->pAttachments[j].colorWriteMask != 0) {
1095 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1096 if (attachment != VK_ATTACHMENT_UNUSED) {
1097 cis.accessFramebufferAttachments.push_back({ attachment, VK_IMAGE_ASPECT_COLOR_BIT });
1098 }
1099 }
1100 }
1101 }
1102
1103 if (cis.depthStencilStateCI && (cis.depthStencilStateCI->depthTestEnable ||
1104 cis.depthStencilStateCI->depthBoundsTestEnable ||
1105 cis.depthStencilStateCI->stencilTestEnable)) {
1106 uint32_t attachment = subpass.pDepthStencilAttachment ?
1107 subpass.pDepthStencilAttachment->attachment :
1108 VK_ATTACHMENT_UNUSED;
1109 if (attachment != VK_ATTACHMENT_UNUSED) {
1110 VkImageAspectFlags aspects = 0;
1111 if (cis.depthStencilStateCI->depthTestEnable || cis.depthStencilStateCI->depthBoundsTestEnable) {
1112 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1113 }
1114 if (cis.depthStencilStateCI->stencilTestEnable) {
1115 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1116 }
1117 cis.accessFramebufferAttachments.push_back({ attachment, aspects });
1118 }
1119 }
Sam Walls0961ec02020-03-31 16:39:15 +01001120 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001121
1122 // AMD best practice
1123 pipeline_cache = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001124}
1125
Camden5b184be2019-08-13 07:50:19 -06001126bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1127 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001128 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001129 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001130 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1131 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001132
1133 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001134 skip |= LogPerformanceWarning(
1135 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1136 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1137 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001138 }
1139
Nadav Gevaf0808442021-05-21 13:51:25 -04001140 if (VendorCheckEnabled(kBPVendorAMD)) {
1141 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1142 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1143 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1144 "improve cache hit rate",
1145 VendorSpecificTag(kBPVendorAMD));
1146 }
1147 }
1148
sfricke-samsung86d055a2022-02-11 14:43:50 -08001149 for (uint32_t i = 0; i < createInfoCount; i++) {
1150 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1151 if (VendorCheckEnabled(kBPVendorArm)) {
1152 skip |= ValidateCreateComputePipelineArm(createInfo);
1153 }
1154
1155 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1156 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1157 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1158 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1159 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1160 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1161 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1162 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1163 i);
1164 }
1165 }
1166 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001167 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001168
1169 return skip;
1170}
1171
1172bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1173 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001174 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001175 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001176 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1177 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001178
1179 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001180 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001181
1182 uint32_t thread_count = x * y * z;
1183
1184 // Generate a priori warnings about work group sizes.
1185 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1186 skip |= LogPerformanceWarning(
1187 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1188 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1189 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1190 "groups with less than %u threads, especially when using barrier() or shared memory.",
1191 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1192 }
1193
1194 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1195 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1196 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1197 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1198 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1199 "%u, %u) is not aligned to %u "
1200 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1201 "leave threads idle on the shader "
1202 "core.",
1203 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1204 kThreadGroupDispatchCountAlignmentArm);
1205 }
1206
sfricke-samsungef15e482022-01-26 11:32:49 -08001207 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1208 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001209
1210 unsigned dimensions = 0;
1211 if (x > 1) dimensions++;
1212 if (y > 1) dimensions++;
1213 if (z > 1) dimensions++;
1214 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1215 dimensions = std::max(dimensions, 1u);
1216
1217 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1218 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1219 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1220 bool accesses_2d = false;
1221 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001222 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001223 if (dim < 0) continue;
1224 auto spvdim = spv::Dim(dim);
1225 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1226 }
1227
1228 if (accesses_2d && dimensions < 2) {
1229 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1230 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1231 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1232 "exhibiting poor spatial locality with respect to one or more shader resources.",
1233 VendorSpecificTag(kBPVendorArm), x, y, z);
1234 }
1235
Camden5b184be2019-08-13 07:50:19 -06001236 return skip;
1237}
1238
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001239bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001240 bool skip = false;
1241
1242 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001243 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1244 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001245 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001246 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1247 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001248 }
1249
1250 return skip;
1251}
1252
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001253bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1254 bool skip = false;
1255
1256 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1257 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1258 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1259 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1260 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1261 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1262 }
1263
1264 return skip;
1265}
1266
1267bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1268 bool skip = false;
1269 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1270
1271 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1272 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1273
1274 return skip;
1275}
1276
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001277void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001278 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1279 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1280 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1281 LogPerformanceWarning(
1282 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1283 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1284 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1285 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1286 "convenient opportunity.",
1287 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1288 }
1289 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001290
1291 // AMD best practice
1292 // end-of-frame cleanup
1293 num_queue_submissions = 0;
1294 num_barriers_objects = 0;
1295 pipelines_used_in_frame.clear();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001296}
1297
Jeff Bolz5c801d12019-10-09 10:38:45 -05001298bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1299 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001300 bool skip = false;
1301
1302 for (uint32_t submit = 0; submit < submitCount; submit++) {
1303 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1304 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1305 }
1306 }
1307
1308 return skip;
1309}
1310
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001311bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1312 VkFence fence) const {
1313 bool skip = false;
1314
1315 for (uint32_t submit = 0; submit < submitCount; submit++) {
1316 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1317 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1318 }
1319 }
1320
1321 return skip;
1322}
1323
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001324bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1325 VkFence fence) const {
1326 bool skip = false;
1327
1328 for (uint32_t submit = 0; submit < submitCount; submit++) {
1329 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1330 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1331 }
1332 }
1333
1334 return skip;
1335}
1336
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001337bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1338 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1339 bool skip = false;
1340
1341 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1342 skip |= LogPerformanceWarning(
1343 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1344 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1345 "pool instead.");
1346 }
1347
1348 return skip;
1349}
1350
1351bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1352 const VkCommandBufferBeginInfo* pBeginInfo) const {
1353 bool skip = false;
1354
1355 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1356 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1357 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1358 }
1359
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001360 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1361 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001362 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1363 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1364 VendorSpecificTag(kBPVendorArm));
1365 }
1366
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001367 return skip;
1368}
1369
Jeff Bolz5c801d12019-10-09 10:38:45 -05001370bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001371 bool skip = false;
1372
1373 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1374
1375 return skip;
1376}
1377
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001378bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1379 const VkDependencyInfoKHR* pDependencyInfo) const {
1380 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1381}
1382
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001383bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1384 const VkDependencyInfo* pDependencyInfo) const {
1385 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1386}
1387
Jeff Bolz5c801d12019-10-09 10:38:45 -05001388bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1389 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001390 bool skip = false;
1391
1392 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1393
1394 return skip;
1395}
1396
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001397bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1398 VkPipelineStageFlags2KHR stageMask) const {
1399 bool skip = false;
1400
1401 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1402
1403 return skip;
1404}
1405
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001406bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1407 VkPipelineStageFlags2 stageMask) const {
1408 bool skip = false;
1409
1410 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1411
1412 return skip;
1413}
1414
Camden5b184be2019-08-13 07:50:19 -06001415bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1416 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1417 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1418 uint32_t bufferMemoryBarrierCount,
1419 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1420 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001421 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001422 bool skip = false;
1423
1424 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1425 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1426
1427 return skip;
1428}
1429
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001430bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1431 const VkDependencyInfoKHR* pDependencyInfos) const {
1432 bool skip = false;
1433 for (uint32_t i = 0; i < eventCount; i++) {
1434 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1435 }
1436
1437 return skip;
1438}
1439
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001440bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1441 const VkDependencyInfo* pDependencyInfos) const {
1442 bool skip = false;
1443 for (uint32_t i = 0; i < eventCount; i++) {
1444 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1445 }
1446
1447 return skip;
1448}
1449
Camden5b184be2019-08-13 07:50:19 -06001450bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1451 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1452 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1453 uint32_t bufferMemoryBarrierCount,
1454 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1455 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001456 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001457 bool skip = false;
1458
1459 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1460 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1461
Nadav Gevaf0808442021-05-21 13:51:25 -04001462 if (VendorCheckEnabled(kBPVendorAMD)) {
1463 if (num_barriers_objects + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
1464 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
1465 "%s Performance warning: In this frame, %" PRIu32 " barriers were already submitted. Barriers have a high cost and can "
1466 "stall the GPU. "
1467 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1468 VendorSpecificTag(kBPVendorAMD), num_barriers_objects);
1469 }
1470
1471 std::array<VkImageLayout, 3> read_layouts = {
1472 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1473 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1474 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1475 };
1476
1477 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1478 // read to read barriers
1479 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1480 bool old_is_read_layout = found != read_layouts.end();
1481 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1482 bool new_is_read_layout = found != read_layouts.end();
1483 if (old_is_read_layout && new_is_read_layout) {
1484 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1485 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1486 "time you use it.",
1487 VendorSpecificTag(kBPVendorAMD));
1488 }
1489
1490 // general with no storage
1491 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1492 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1493 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1494 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1495 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1496 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1497 VendorSpecificTag(kBPVendorAMD));
1498 }
1499 }
1500 }
1501 }
1502
Camden5b184be2019-08-13 07:50:19 -06001503 return skip;
1504}
1505
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001506bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1507 const VkDependencyInfoKHR* pDependencyInfo) const {
1508 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1509}
1510
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001511bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1512 const VkDependencyInfo* pDependencyInfo) const {
1513 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1514}
1515
Camden5b184be2019-08-13 07:50:19 -06001516bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001517 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001518 bool skip = false;
1519
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001520 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1521
1522 return skip;
1523}
1524
1525bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1526 VkQueryPool queryPool, uint32_t query) const {
1527 bool skip = false;
1528
1529 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001530
1531 return skip;
1532}
1533
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001534bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1535 VkQueryPool queryPool, uint32_t query) const {
1536 bool skip = false;
1537
1538 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1539
1540 return skip;
1541}
1542
Sam Walls0961ec02020-03-31 16:39:15 +01001543void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1544 VkPipeline pipeline) {
1545 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1546
Nadav Gevaf0808442021-05-21 13:51:25 -04001547 // AMD best practice
1548 pipelines_used_in_frame.emplace(pipeline);
1549
Sam Walls0961ec02020-03-31 16:39:15 +01001550 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
1551 // check for depth/blend state tracking
1552 auto gp_cis = graphicsPipelineCIs.find(pipeline);
1553 if (gp_cis != graphicsPipelineCIs.end()) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001554 auto cb_node = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001555 assert(cb_node);
1556 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001557
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001558 render_pass_state.nextDrawTouchesAttachments = gp_cis->second.accessFramebufferAttachments;
1559 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001560
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001561 const auto& blend_state = gp_cis->second.colorBlendStateCI;
1562 const auto& stencil_state = gp_cis->second.depthStencilStateCI;
Sam Walls0961ec02020-03-31 16:39:15 +01001563
1564 if (blend_state) {
1565 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001566 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001567 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1568 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001569 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001570 }
1571 }
1572 }
1573
1574 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001575 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001576
1577 if (stencil_state && stencil_state->depthTestEnable) {
1578 switch (stencil_state->depthCompareOp) {
1579 case VK_COMPARE_OP_EQUAL:
1580 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1581 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001582 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001583 break;
1584 default:
1585 break;
1586 }
1587 }
Sam Walls0961ec02020-03-31 16:39:15 +01001588 }
1589 }
1590}
1591
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001592static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1593 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1594 const auto& subpass_info = createInfo.pSubpasses[subpass];
1595 if (subpass_info.pResolveAttachments) {
1596 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1597 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1598 }
1599 }
1600 }
1601
1602 return false;
1603}
1604
Attilio Provenzano02859b22020-02-27 14:17:28 +00001605static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1606 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001607 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001608
1609 // If an attachment is ever used as a color attachment,
1610 // resolve attachment or depth stencil attachment,
1611 // it needs to exist on tile at some point.
1612
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001613 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1614 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001615 }
1616
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001617 if (subpass_info.pResolveAttachments) {
1618 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1619 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1620 }
1621 }
1622
1623 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001624 }
1625
1626 return false;
1627}
1628
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001629static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1630 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1631 return false;
1632 }
1633
1634 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001635 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001636
1637 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1638 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1639 return true;
1640 }
1641 }
1642 }
1643
1644 return false;
1645}
1646
Attilio Provenzano02859b22020-02-27 14:17:28 +00001647bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1648 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1649 bool skip = false;
1650
1651 if (!pRenderPassBegin) {
1652 return skip;
1653 }
1654
Gareth Webbdc6549a2021-06-16 03:52:24 +01001655 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1656 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1657 "This render pass has a zero-size render area. It cannot write to any attachments, "
1658 "and can only be used for side effects such as layout transitions.");
1659 }
1660
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001661 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001662 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001663 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001664 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001665 if (rpabi) {
1666 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1667 }
1668 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001669 // Check if any attachments have LOAD operation on them
1670 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001671 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001672
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001673 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001674 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001675 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001676 }
1677
1678 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001679 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001680 }
1681
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001682 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001683
1684 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001685 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1686 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001687 }
1688
1689 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001690 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1691 skip |= LogPerformanceWarning(
1692 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1693 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1694 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001695 "which will copy in total %u pixels (renderArea = "
1696 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001697 VendorSpecificTag(kBPVendorArm), att,
1698 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1699 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1700 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001701 }
1702 }
1703 }
1704
1705 return skip;
1706}
1707
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001708void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1709 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001710 if (view) {
Jeremy Gebbenb4d17012021-07-08 13:18:15 -06001711 QueueValidateImage(funcs, function_name, GetImageUsageState(view->create_info.image), usage,
1712 view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001713 }
1714}
1715
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001716void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1717 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001718 const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001719 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001720
1721 // If we're viewing a 3D slice, ignore base array layer.
1722 // The entire 3D subresource is accessed as one atomic unit.
1723 const uint32_t base_array_layer = image->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
1724
1725 const uint32_t max_layers = image->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001726 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
1727 const uint32_t max_levels = image->createInfo.mipLevels - subresource_range.baseMipLevel;
1728 const uint32_t mip_levels = std::min(image->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001729
1730 for (uint32_t layer = 0; layer < array_layers; layer++) {
1731 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001732 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1733 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001734 }
1735 }
1736}
1737
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001738void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1739 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001740 const VkImageSubresourceLayers& subresource_layers) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001741 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001742 const uint32_t max_layers = image->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
1743 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001744
1745 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001746 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001747 }
1748}
1749
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001750void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1751 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001752 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07001753 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker&, const QUEUE_STATE&,
1754 const CMD_BUFFER_STATE&) -> bool {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001755 ValidateImageInQueue(function_name, state, usage, array_layer, mip_level);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001756 return false;
1757 });
1758}
1759
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001760void BestPractices::ValidateImageInQueueArm(const char* function_name, IMAGE_STATE* image,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001761 IMAGE_SUBRESOURCE_USAGE_BP last_usage,
1762 IMAGE_SUBRESOURCE_USAGE_BP usage,
1763 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001764 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001765 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 -06001766 !image->IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001767 LogPerformanceWarning(
1768 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001769 "%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 +02001770 "image was used, it was written to with STORE_OP_STORE. "
1771 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1772 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001773 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001774 } 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 +02001775 LogPerformanceWarning(
1776 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001777 "%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 +02001778 "image was used, it was written to with vkCmdClear*Image(). "
1779 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1780 "tile-based architectures."
1781 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001782 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001783 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
1784 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE ||
1785 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1786 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE ||
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001787 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001788 const char *last_cmd = nullptr;
1789 const char *vuid = nullptr;
1790 const char *suggestion = nullptr;
1791
1792 switch (last_usage) {
1793 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1794 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1795 last_cmd = "vkCmdBlitImage";
1796 suggestion =
1797 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1798 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1799 "which avoids the memory roundtrip.";
1800 break;
1801 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1802 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1803 last_cmd = "vkCmdClear*Image";
1804 suggestion =
1805 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1806 "tile-based architectures. "
1807 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1808 break;
1809 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1810 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1811 last_cmd = "vkCmdCopy*Image";
1812 suggestion =
1813 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1814 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1815 "which avoids the memory roundtrip.";
1816 break;
1817 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1818 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1819 last_cmd = "vkCmdResolveImage";
1820 suggestion =
1821 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1822 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1823 "which avoids the memory roundtrip.";
1824 break;
1825 default:
1826 break;
1827 }
1828
1829 LogPerformanceWarning(
1830 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001831 "%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 +01001832 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001833 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001834 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001835}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001836
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001837void BestPractices::ValidateImageInQueue(const char* function_name, IMAGE_STATE_BP* state,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001838 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer,
1839 uint32_t mip_level) {
1840 IMAGE_STATE* image = state->image;
1841 IMAGE_SUBRESOURCE_USAGE_BP last_usage = state->usages[array_layer][mip_level];
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001842 state->usages[array_layer][mip_level] = usage;
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001843 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001844 ValidateImageInQueueArm(function_name, image, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001845 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001846}
1847
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001848void BestPractices::AddDeferredQueueOperations(CMD_BUFFER_STATE_BP* cb) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001849 cb->queue_submit_functions.insert(cb->queue_submit_functions.end(),
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001850 cb->queue_submit_functions_after_render_pass.begin(),
1851 cb->queue_submit_functions_after_render_pass.end());
1852 cb->queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001853}
1854
1855void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1856 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001857 auto cb_node = GetCBState(commandBuffer);
1858 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001859}
1860
1861void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1862 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001863 auto cb_node = GetCBState(commandBuffer);
1864 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001865}
1866
1867void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1868 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001869 auto cb_node = GetCBState(commandBuffer);
1870 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001871}
1872
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001873void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1874 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001875 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001876 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001877 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1878}
1879
1880void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1881 const VkRenderPassBeginInfo* pRenderPassBegin,
1882 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1883 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1884 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1885}
1886
1887void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1888 const VkRenderPassBeginInfo* pRenderPassBegin,
1889 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1890 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1891 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1892}
1893
1894void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001895
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001896 if (!pRenderPassBegin) {
1897 return;
1898 }
1899
Jeremy Gebben9f537102021-10-05 16:37:12 -06001900 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001901
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001902 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001903 if (rp_state) {
1904 // Check load ops
1905 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001906 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001907
1908 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1909 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1910 continue;
1911 }
1912
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001913 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001914
1915 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1916 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001917 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001918 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1919 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001920 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001921 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001922 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001923 }
1924
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001925 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001926 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001927
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001928 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001929 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1930 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001931 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001932 }
1933 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001934 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001935 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001936
Jeremy Gebben9f537102021-10-05 16:37:12 -06001937 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001938 }
1939
1940 // Check store ops
1941 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001942 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001943
1944 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1945 continue;
1946 }
1947
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001948 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001949
1950 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1951 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001952 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001953 }
1954
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001955 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001956
Jeremy Gebben9f537102021-10-05 16:37:12 -06001957 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001958 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001959 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1960 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001961 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001962 }
1963 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001964 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001965 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001966
Jeremy Gebben9f537102021-10-05 16:37:12 -06001967 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001968 }
1969 }
1970}
1971
Attilio Provenzano02859b22020-02-27 14:17:28 +00001972bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1973 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001974 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1975 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001976 return skip;
1977}
1978
1979bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1980 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001981 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001982 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1983 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001984 return skip;
1985}
1986
1987bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001988 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001989 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1990 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001991 return skip;
1992}
1993
Sam Walls0961ec02020-03-31 16:39:15 +01001994void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1995 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001996 // Reset the renderpass state
Jeremy Gebben9f537102021-10-05 16:37:12 -06001997 auto cb = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001998 cb->hasDrawCmd = false;
1999 assert(cb);
2000 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002001 render_pass_state.touchesAttachments.clear();
2002 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002003 render_pass_state.numDrawCallsDepthOnly = 0;
2004 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2005 render_pass_state.colorAttachment = false;
2006 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002007 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002008 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002009
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002010 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002011
2012 // track depth / color attachment usage within the renderpass
2013 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2014 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002015 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002016
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002017 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002018 }
2019}
2020
2021void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2022 VkSubpassContents contents) {
2023 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2024 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2025}
2026
2027void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2028 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2029 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2030 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2031}
2032
2033void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2034 const VkRenderPassBeginInfo* pRenderPassBegin,
2035 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2036 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2037 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2038}
2039
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002040// Generic function to handle validation for all CmdDraw* type functions
2041bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2042 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002043 const auto cb_state = GetCBState(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002044 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002045 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2046 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002047 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002048
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002049 // Verify vertex binding
2050 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
2051 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002052 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002053 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002054 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2055 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002056 }
2057 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002058
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002059 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002060 if (pipe) {
2061 const auto* rp_state = pipe->rp_state.get();
2062 if (rp_state) {
2063 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2064 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002065 const auto& create_info = pipe->create_info.graphics;
2066 const uint32_t depth_stencil_attachment =
2067 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2068 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2069 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002070 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2071 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2072 }
2073 }
2074 }
2075 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002076 }
2077 return skip;
2078}
2079
Sam Walls0961ec02020-03-31 16:39:15 +01002080void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002081 auto cb_node = GetCBState(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002082 assert(cb_node);
2083 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002084 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002085 RecordCmdDrawTypeArm(render_pass_state, draw_count, caller);
2086 }
2087
2088 if (render_pass_state.drawTouchAttachments) {
2089 for (auto& touch : render_pass_state.nextDrawTouchesAttachments) {
2090 RecordAttachmentAccess(render_pass_state, touch.framebufferAttachment, touch.aspects);
2091 }
2092 // No need to touch the same attachments over and over.
2093 render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002094 }
2095}
2096
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002097void BestPractices::RecordCmdDrawTypeArm(RenderPassState& render_pass_state, uint32_t draw_count, const char* caller) {
2098 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2099 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2100 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002101 }
2102}
2103
Camden5b184be2019-08-13 07:50:19 -06002104bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002105 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002106 bool skip = false;
2107
2108 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002109 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2110 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002111 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002112 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002113
2114 return skip;
2115}
2116
Sam Walls0961ec02020-03-31 16:39:15 +01002117void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2118 uint32_t firstVertex, uint32_t firstInstance) {
2119 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2120 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2121}
2122
Camden5b184be2019-08-13 07:50:19 -06002123bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002124 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002125 bool skip = false;
2126
2127 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002128 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2129 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002130 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002131 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2132
Attilio Provenzano02859b22020-02-27 14:17:28 +00002133 // Check if we reached the limit for small indexed draw calls.
2134 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben9f537102021-10-05 16:37:12 -06002135 const auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002136 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002137 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2138 VendorCheckEnabled(kBPVendorArm)) {
2139 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002140 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002141 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2142 "You can try batching drawcalls or instancing when applicable.",
2143 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2144 }
2145
Sam Walls8e77e4f2020-03-16 20:47:40 +00002146 if (VendorCheckEnabled(kBPVendorArm)) {
2147 ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2148 }
2149
2150 return skip;
2151}
2152
2153bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2154 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2155 bool skip = false;
2156
2157 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Jeremy Gebben9f537102021-10-05 16:37:12 -06002158 const auto cmd_state = GetCBState(commandBuffer);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002159 if (cmd_state == nullptr) return skip;
2160
locke-lunarg1ae57d62020-11-18 10:49:19 -07002161 const auto* ib_state = cmd_state->index_buffer_binding.buffer_state.get();
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002162 if (ib_state == nullptr || cmd_state->index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002163
2164 const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002165 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002166 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2167 const void* ib_mem = ib_mem_state.p_driver_data;
2168 bool primitive_restart_enable = false;
2169
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002170 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2171 const auto& pipeline_binding_iter = cmd_state->lastBound[lv_bind_point];
2172 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002173
Jeremy Gebben11af9792021-08-20 10:20:09 -06002174 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2175 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002176 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002177
2178 // 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 -06002179 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002180 uint32_t scan_stride;
2181 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2182 scan_stride = sizeof(uint8_t);
2183 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2184 scan_stride = sizeof(uint16_t);
2185 } else {
2186 scan_stride = sizeof(uint32_t);
2187 }
2188
2189 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2190 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2191
2192 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2193 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2194 // irrespective of whether or not they're part of the draw call.
2195
2196 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2197 uint32_t min_index = ~0u;
2198 // start with maximum as 0 and adjust to indices in the buffer
2199 uint32_t max_index = 0u;
2200
2201 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2202 // for the given index buffer
2203 uint32_t vertex_shade_count = 0;
2204
2205 PostTransformLRUCacheModel post_transform_cache;
2206
2207 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2208 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2209 // target architecture.
2210 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2211 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2212 post_transform_cache.resize(32);
2213
2214 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2215 uint32_t scan_index;
2216 uint32_t primitive_restart_value;
2217 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2218 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2219 primitive_restart_value = 0xFF;
2220 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2221 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2222 primitive_restart_value = 0xFFFF;
2223 } else {
2224 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2225 primitive_restart_value = 0xFFFFFFFF;
2226 }
2227
2228 max_index = std::max(max_index, scan_index);
2229 min_index = std::min(min_index, scan_index);
2230
2231 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2232 bool in_cache = post_transform_cache.query_cache(scan_index);
2233 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2234 if (!in_cache) vertex_shade_count++;
2235 }
2236 }
2237
2238 // 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 +01002239 // 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
2240 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002241
2242 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002243 skip |=
2244 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2245 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2246 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2247 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2248 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2249 VendorSpecificTag(kBPVendorArm),
2250 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002251 return skip;
2252 }
2253
2254 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2255 // 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 +01002256 const size_t refs_per_bucket = 64;
2257 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2258
2259 const uint32_t n_indices = max_index - min_index + 1;
2260 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2261 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2262
2263 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2264 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002265
2266 // To avoid using too much memory, we run over the indices again.
2267 // Knowing the size from the last scan allows us to record index usage with bitsets
2268 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2269 uint32_t scan_index;
2270 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2271 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2272 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2273 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2274 } else {
2275 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2276 }
2277 // keep track of the set of all indices used to reference vertices in the draw call
2278 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002279 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2280 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002281 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2282 }
2283
2284 uint32_t vertex_reference_count = 0;
2285 for (const auto& bitset : vertex_reference_buckets) {
2286 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2287 }
2288
2289 // 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 -07002290 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002291 // 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 -07002292 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002293
2294 if (utilization < 0.5f) {
2295 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2296 "%s The indices which were specified for the draw call only utilise approximately "
2297 "%.02f%% of the bound vertex buffer.",
2298 VendorSpecificTag(kBPVendorArm), utilization);
2299 }
2300
2301 if (cache_hit_rate <= 0.5f) {
2302 skip |=
2303 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2304 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2305 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2306 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2307 "recently shaded vertices.",
2308 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2309 }
2310 }
2311
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002312 return skip;
2313}
2314
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002315bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2316 const VkCommandBuffer* pCommandBuffers) const {
2317 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002318 const auto primary = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002319 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002320 const auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002321 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002322 continue;
2323 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002324 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002325 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002326 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(clear.rects.size()), clear.rects.data())) {
2327 skip |= ValidateClearAttachment(commandBuffer, primary.get(), clear.framebufferAttachment, clear.colorAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002328 clear.aspects, true);
2329 }
2330 }
2331 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002332
2333 if (VendorCheckEnabled(kBPVendorAMD)) {
2334 if (commandBufferCount > 0) {
2335 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2336 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2337 VendorSpecificTag(kBPVendorAMD));
2338 }
2339 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002340 return skip;
2341}
2342
2343void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2344 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002345 auto primary = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002346 auto& primary_state = primary->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002347
2348 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002349 auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002350 if (secondary_cb == nullptr) {
2351 continue;
2352 }
2353 auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002354
2355 for (auto& early_clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002356 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2357 RecordAttachmentClearAttachments(primary.get(), primary_state, early_clear.framebufferAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002358 early_clear.colorAttachment, early_clear.aspects,
Hans-Kristian Arntzenfa8ef9f2021-06-29 12:07:59 +02002359 uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002360 } else {
2361 RecordAttachmentAccess(primary_state, early_clear.framebufferAttachment,
2362 early_clear.aspects);
2363 }
2364 }
2365
2366 for (auto& touch : secondary.touchesAttachments) {
2367 RecordAttachmentAccess(primary_state, touch.framebufferAttachment,
2368 touch.aspects);
2369 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002370
2371 primary_state.numDrawCallsDepthEqualCompare += secondary.numDrawCallsDepthEqualCompare;
2372 primary_state.numDrawCallsDepthOnly += secondary.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002373
Jeremy Gebben9f537102021-10-05 16:37:12 -06002374 auto second_state = GetCBState(pCommandBuffers[i]);
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002375 if (second_state->hasDrawCmd) {
2376 primary->hasDrawCmd = true;
2377 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002378 }
2379
2380 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2381}
2382
2383void BestPractices::RecordAttachmentAccess(RenderPassState& state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2384 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
2385 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002386 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002387 return info.framebufferAttachment == fb_attachment;
2388 });
2389
2390 if (itr != state.touchesAttachments.end()) {
2391 itr->aspects |= aspects;
2392 } else {
2393 state.touchesAttachments.push_back({ fb_attachment, aspects });
2394 }
2395}
2396
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002397void BestPractices::RecordAttachmentClearAttachments(CMD_BUFFER_STATE_BP* cmd_state, RenderPassState& state, uint32_t fb_attachment,
2398 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2399 const VkClearRect* pRects) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002400 // If we observe a full clear before any other access to a frame buffer attachment,
2401 // we have candidate for redundant clear attachments.
2402 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002403 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002404 return info.framebufferAttachment == fb_attachment;
2405 });
2406
2407 uint32_t new_aspects = aspects;
2408 if (itr != state.touchesAttachments.end()) {
2409 new_aspects = aspects & ~itr->aspects;
2410 itr->aspects |= aspects;
2411 } else {
2412 state.touchesAttachments.push_back({ fb_attachment, aspects });
2413 }
2414
2415 if (new_aspects == 0) {
2416 return;
2417 }
2418
2419 if (cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2420 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2421 // CmdExecuteCommands.
2422 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2423 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2424 }
2425}
2426
2427void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2428 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2429 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002430 auto cmd_state = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002431 RENDER_PASS_STATE* rp_state = cmd_state->activeRenderPass.get();
2432 FRAMEBUFFER_STATE* fb_state = cmd_state->activeFramebuffer.get();
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002433 RenderPassState& tracking_state = cmd_state->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002434 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2435
2436 if (rectCount == 0 || !rp_state) {
2437 return;
2438 }
2439
2440 if (!is_secondary && !fb_state) {
2441 return;
2442 }
2443
2444 // 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 -06002445 bool full_clear = ClearAttachmentsIsFullClear(cmd_state.get(), rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002446
2447 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2448 for (uint32_t i = 0; i < attachmentCount; i++) {
2449 auto& attachment = pClearAttachments[i];
2450 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2451 VkImageAspectFlags aspects = attachment.aspectMask;
2452
2453 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2454 if (subpass.pDepthStencilAttachment) {
2455 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2456 }
2457 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2458 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
2459 }
2460
2461 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2462 if (full_clear) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002463 RecordAttachmentClearAttachments(cmd_state.get(), tracking_state, fb_attachment, attachment.colorAttachment,
2464 aspects, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002465 } else {
2466 RecordAttachmentAccess(tracking_state, fb_attachment, aspects);
2467 }
2468 }
2469 }
2470
2471 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments,
2472 rectCount, pRects);
2473}
2474
Attilio Provenzano02859b22020-02-27 14:17:28 +00002475void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2476 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2477 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2478 firstInstance);
2479
Jeremy Gebben9f537102021-10-05 16:37:12 -06002480 auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002481 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2482 cmd_state->small_indexed_draw_call_count++;
2483 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002484
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002485 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002486}
2487
Sam Walls0961ec02020-03-31 16:39:15 +01002488void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2489 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2490 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2491 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2492}
2493
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002494bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2495 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2496 uint32_t maxDrawCount, uint32_t stride) const {
2497 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2498
2499 return skip;
2500}
2501
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002502bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2503 VkDeviceSize offset, VkBuffer countBuffer,
2504 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2505 uint32_t stride) const {
2506 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002507
2508 return skip;
2509}
2510
2511bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002512 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002513 bool skip = false;
2514
2515 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002516 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2517 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002518 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002519 }
2520
2521 return skip;
2522}
2523
Sam Walls0961ec02020-03-31 16:39:15 +01002524void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2525 uint32_t count, uint32_t stride) {
2526 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2527 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2528}
2529
Camden5b184be2019-08-13 07:50:19 -06002530bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002531 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002532 bool skip = false;
2533
2534 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002535 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2536 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002537 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002538 }
2539
2540 return skip;
2541}
2542
Sam Walls0961ec02020-03-31 16:39:15 +01002543void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2544 uint32_t count, uint32_t stride) {
2545 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2546 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2547}
2548
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002549void BestPractices::ValidateBoundDescriptorSets(VkCommandBuffer commandBuffer, const char* function_name) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002550 auto cb_state = GetCBState(commandBuffer);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002551
2552 if (cb_state) {
2553 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002554 const auto& layout = *descriptor_set->GetLayout();
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002555
2556 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2557 // For bindless scenarios, we should not attempt to track descriptor set state.
2558 // It is highly uncertain which resources are actually bound.
2559 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2560 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2561 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
2562 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2563 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2564 continue;
2565 }
2566
2567 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002568 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002569 VkImageView image_view{VK_NULL_HANDLE};
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002570
2571 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2572 switch (descriptor->GetClass()) {
2573 case cvdescriptorset::DescriptorClass::Image: {
2574 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2575 image_view = image_descriptor->GetImageView();
2576 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002577 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002578 }
2579 case cvdescriptorset::DescriptorClass::ImageSampler: {
2580 if (const auto image_sampler_descriptor =
2581 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2582 image_view = image_sampler_descriptor->GetImageView();
2583 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002584 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002585 }
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002586 default:
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002587 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002588 }
2589
2590 if (image_view) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002591 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002592 QueueValidateImageView(cb_state->queue_submit_functions, function_name, image_view_state.get(),
2593 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002594 }
2595 }
2596 }
2597 }
2598 }
2599}
2600
2601void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2602 uint32_t firstVertex, uint32_t firstInstance) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002603 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002604}
2605
2606void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2607 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002608 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002609}
2610
2611void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2612 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002613 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002614}
2615
Camden5b184be2019-08-13 07:50:19 -06002616bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002617 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002618 bool skip = false;
2619
2620 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002621 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2622 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2623 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2624 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002625 }
2626
2627 return skip;
2628}
Camden83a9c372019-08-14 11:41:38 -06002629
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002630bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2631 bool skip = false;
2632 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2633 skip |= ValidateCmdEndRenderPass(commandBuffer);
2634 return skip;
2635}
2636
2637bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2638 bool skip = false;
2639 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2640 skip |= ValidateCmdEndRenderPass(commandBuffer);
2641 return skip;
2642}
2643
Sam Walls0961ec02020-03-31 16:39:15 +01002644bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2645 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002646 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002647 skip |= ValidateCmdEndRenderPass(commandBuffer);
2648 return skip;
2649}
2650
2651bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2652 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002653 const auto cmd = GetCBState(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002654
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002655 if (cmd == nullptr) return skip;
2656 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002657
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002658 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2659 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2660 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002661 if (uses_depth) {
2662 skip |= LogPerformanceWarning(
2663 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2664 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2665 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2666 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2667 VendorSpecificTag(kBPVendorArm));
2668 }
2669
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002670 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2671
2672 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2673
2674 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2675 // it is redundant to have it be part of the render pass.
2676 // Only consider it redundant if it will actually consume bandwidth, i.e.
2677 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2678 // as is using pure input attachments.
2679 // CLEAR -> STORE might be considered a "useful" thing to do, but
2680 // the optimal thing to do is to defer the clear until you're actually
2681 // going to render to the image.
2682
2683 uint32_t num_attachments = rp->createInfo.attachmentCount;
2684 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002685 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2686 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002687 continue;
2688 }
2689
2690 auto& attachment = rp->createInfo.pAttachments[i];
2691
2692 VkImageAspectFlags bandwidth_aspects = 0;
2693
2694 if (!FormatIsStencilOnly(attachment.format) &&
2695 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2696 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2697 if (FormatHasDepth(attachment.format)) {
2698 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2699 } else {
2700 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2701 }
2702 }
2703
2704 if (FormatHasStencil(attachment.format) &&
2705 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2706 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2707 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2708 }
2709
2710 if (!bandwidth_aspects) {
2711 continue;
2712 }
2713
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002714 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
2715 [&](const AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002716 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002717 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002718 untouched_aspects &= ~itr->aspects;
2719 }
2720
2721 if (untouched_aspects) {
2722 skip |= LogPerformanceWarning(
2723 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2724 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2725 "was never accessed by a pipeline or clear command. "
2726 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2727 "if the attachments are not intended to be accessed.",
2728 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2729 }
2730 }
2731 }
2732
Sam Walls0961ec02020-03-31 16:39:15 +01002733 return skip;
2734}
2735
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002736void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002737 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002738}
2739
2740void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002741 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002742}
2743
Camden Stocker9c051442019-11-06 14:28:43 -08002744bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2745 const char* api_name) const {
2746 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002747 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002748
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002749 if (bp_pd_state) {
2750 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2751 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2752 "Potential problem with calling %s() without first retrieving properties from "
2753 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2754 api_name);
2755 }
Camden Stocker9c051442019-11-06 14:28:43 -08002756 }
2757
2758 return skip;
2759}
2760
Camden83a9c372019-08-14 11:41:38 -06002761bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002762 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002763 bool skip = false;
2764
Camden Stocker9c051442019-11-06 14:28:43 -08002765 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002766
Camden Stocker9c051442019-11-06 14:28:43 -08002767 return skip;
2768}
2769
2770bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2771 uint32_t planeIndex,
2772 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2773 bool skip = false;
2774
2775 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2776
2777 return skip;
2778}
2779
2780bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2781 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2782 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2783 bool skip = false;
2784
2785 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002786
2787 return skip;
2788}
Camden05de2d42019-08-19 10:23:56 -06002789
2790bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002791 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002792 bool skip = false;
2793
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002794 auto swapchain_state = std::static_pointer_cast<const SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Camden05de2d42019-08-19 10:23:56 -06002795
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002796 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002797 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002798 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002799 skip |=
2800 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2801 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2802 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002803 }
Camden05de2d42019-08-19 10:23:56 -06002804
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002805 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2806 skip |= LogWarning(
2807 device, kVUID_BestPractices_Swapchain_InvalidCount,
2808 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002809 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002810 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2811 }
2812 }
2813
Camden05de2d42019-08-19 10:23:56 -06002814 return skip;
2815}
2816
2817// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002818bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002819 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002820 const CALL_STATE call_state,
2821 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002822 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002823 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2824 if (UNCALLED == call_state) {
2825 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002826 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002827 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2828 "recommended "
2829 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2830 caller_name, caller_name);
2831 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002832 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2833 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002834 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2835 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2836 ". It is recommended to instead receive all the properties by calling %s with "
2837 "pQueueFamilyPropertyCount that was "
2838 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002839 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002840 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002841 }
2842
2843 return skip;
2844}
2845
Jeff Bolz5c801d12019-10-09 10:38:45 -05002846bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2847 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002848 bool skip = false;
2849
2850 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002851 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002852 if (!as_state->memory_requirements_checked) {
2853 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2854 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2855 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002856 skip |= LogWarning(
2857 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002858 "vkBindAccelerationStructureMemoryNV(): "
2859 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2860 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2861 }
2862 }
2863
2864 return skip;
2865}
2866
Camden05de2d42019-08-19 10:23:56 -06002867bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2868 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002869 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002870 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002871 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002872 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002873 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2874 "vkGetPhysicalDeviceQueueFamilyProperties()");
2875 }
2876 return false;
Camden05de2d42019-08-19 10:23:56 -06002877}
2878
Mike Schuchardt2df08912020-12-15 16:28:09 -08002879bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2880 uint32_t* pQueueFamilyPropertyCount,
2881 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002882 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002883 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002884 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002885 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2886 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2887 }
2888 return false;
Camden05de2d42019-08-19 10:23:56 -06002889}
2890
Jeff Bolz5c801d12019-10-09 10:38:45 -05002891bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002892 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002893 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002894 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002895 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002896 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2897 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2898 }
2899 return false;
Camden05de2d42019-08-19 10:23:56 -06002900}
2901
2902bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2903 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002904 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002905 if (!pSurfaceFormats) return false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002906 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002907 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002908 bool skip = false;
2909 if (call_state == UNCALLED) {
2910 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2911 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002912 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2913 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2914 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002915 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002916 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002917 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2918 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2919 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2920 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002921 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002922 }
2923 }
2924 return skip;
2925}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002926
2927bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002928 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002929 bool skip = false;
2930
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002931 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2932 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002933 // 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 -07002934 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002935 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2936 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002937 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002938 // 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 -07002939 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2940 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002941 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002942 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002943 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002944 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002945 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002946 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2947 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2948 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002949 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002950 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2951 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002952 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002953 }
2954 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002955 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002956 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002957 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002958 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2959 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002960 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002961 }
2962 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002963 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2964 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002965 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002966 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002967 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002968 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002969 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002970 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2971 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2972 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002973 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002974 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2975 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002976 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002977 }
2978 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002979 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002980 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002981 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002982 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2983 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002984 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002985 }
2986 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2987 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002988 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002989 }
2990 }
2991 }
2992 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002993 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2994 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002995 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002996 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002997 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2998 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002999 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06003000 }
3001 }
3002 }
3003
3004 return skip;
3005}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003006
Mark Lobodzinski84101d72020-04-24 09:43:48 -06003007void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
3008 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003009 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003010 return;
3011 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003012
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003013 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3014 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
3015 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3016 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003017 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003018 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003019 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003020 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003021 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3022 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
3023 image_state->sparse_metadata_bound = true;
3024 }
3025 }
3026 }
3027 }
3028}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003029
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003030bool BestPractices::ClearAttachmentsIsFullClear(const CMD_BUFFER_STATE_BP* cmd, uint32_t rectCount,
3031 const VkClearRect* pRects) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003032 if (cmd->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3033 // We don't know the accurate render area in a secondary,
3034 // so assume we clear the entire frame buffer.
3035 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
3036 return true;
3037 }
3038
3039 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
3040 for (uint32_t i = 0; i < rectCount; i++) {
3041 auto& rect = pRects[i];
3042 auto& render_area = cmd->activeRenderPassBeginInfo.renderArea;
3043 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
3044 return true;
3045 }
3046 }
3047
3048 return false;
3049}
3050
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003051bool BestPractices::ValidateClearAttachment(VkCommandBuffer commandBuffer, const CMD_BUFFER_STATE_BP* cmd, uint32_t fb_attachment,
3052 uint32_t color_attachment, VkImageAspectFlags aspects, bool secondary) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003053 const RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3054 bool skip = false;
3055
3056 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3057 return skip;
3058 }
3059
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003060 const auto& rp_state = cmd->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003061
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003062 auto attachment_itr = std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02003063 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003064 return info.framebufferAttachment == fb_attachment;
3065 });
3066
3067 // Only report aspects which haven't been touched yet.
3068 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003069 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003070 new_aspects &= ~attachment_itr->aspects;
3071 }
3072
3073 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
3074 if (!cmd->hasDrawCmd) {
3075 skip |= LogPerformanceWarning(
3076 commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003077 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3078 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003079 report_data->FormatHandle(commandBuffer).c_str());
3080 }
3081
3082 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3083 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3084 skip |= LogPerformanceWarning(
3085 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3086 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3087 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3088 "it is more efficient.",
3089 secondary ? "vkCmdExecuteCommands(): " : "",
3090 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
3091 }
3092
3093 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3094 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3095 skip |= LogPerformanceWarning(
3096 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3097 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3098 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3099 "it is more efficient.",
3100 secondary ? "vkCmdExecuteCommands(): " : "",
3101 report_data->FormatHandle(commandBuffer).c_str());
3102 }
3103
3104 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3105 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3106 skip |= LogPerformanceWarning(
3107 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3108 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3109 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3110 "it is more efficient.",
3111 secondary ? "vkCmdExecuteCommands(): " : "",
3112 report_data->FormatHandle(commandBuffer).c_str());
3113 }
3114
3115 return skip;
3116}
3117
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003118bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003119 const VkClearAttachment* pAttachments, uint32_t rectCount,
3120 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003121 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003122 const auto cb_node = GetCBState(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003123 if (!cb_node) return skip;
3124
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003125 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3126 // Defer checks to ExecuteCommands.
3127 return skip;
3128 }
3129
3130 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben9f537102021-10-05 16:37:12 -06003131 if (!ClearAttachmentsIsFullClear(cb_node.get(), rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003132 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003133 }
3134
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003135 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3136 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003137 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003138 if (rp) {
3139 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3140
3141 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003142 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003143
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003144 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3145 uint32_t color_attachment = attachment.colorAttachment;
3146 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003147 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, color_attachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003148 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003149 }
3150
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003151 if (subpass.pDepthStencilAttachment &&
3152 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003153 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003154 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, VK_ATTACHMENT_UNUSED,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003155 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003156 }
3157 }
3158 }
3159
Nadav Gevaf0808442021-05-21 13:51:25 -04003160 if (VendorCheckEnabled(kBPVendorAMD)) {
3161 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3162 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3163 bool black_check = false;
3164 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3165 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3166 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3167 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3168 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3169
3170 bool white_check = false;
3171 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3172 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3173 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3174 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3175 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3176
3177 if (black_check && white_check) {
3178 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3179 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3180 "Consider changing to one of the following:"
3181 "RGBA(0, 0, 0, 0) "
3182 "RGBA(0, 0, 0, 1) "
3183 "RGBA(1, 1, 1, 0) "
3184 "RGBA(1, 1, 1, 1)",
3185 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3186 }
3187 } else {
3188 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3189 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3190 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3191 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3192 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3193 "attachment %" PRId32 " is not a fast clear value."
3194 "Consider changing to one of the following:"
3195 "D=0.0f, S=0"
3196 "D=1.0f, S=0",
3197 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3198 }
3199 }
3200 }
3201 }
3202
Camden Stockerf55721f2019-09-09 11:04:49 -06003203 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003204}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003205
3206bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3207 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3208 const VkImageResolve* pRegions) const {
3209 bool skip = false;
3210
3211 skip |= VendorCheckEnabled(kBPVendorArm) &&
3212 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3213 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3214 "This is a very slow and extremely bandwidth intensive path. "
3215 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3216 VendorSpecificTag(kBPVendorArm));
3217
3218 return skip;
3219}
3220
Jeff Leger178b1e52020-10-05 12:22:23 -04003221bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3222 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3223 bool skip = false;
3224
3225 skip |= VendorCheckEnabled(kBPVendorArm) &&
3226 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3227 "%s Attempting to use vkCmdResolveImage2KHR 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
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003235bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3236 const VkResolveImageInfo2* pResolveImageInfo) const {
3237 bool skip = false;
3238
3239 skip |= VendorCheckEnabled(kBPVendorArm) &&
3240 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3241 "%s Attempting to use vkCmdResolveImage2 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
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003249void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3250 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3251 const VkImageResolve* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003252 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003253 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003254 auto* src = GetImageUsageState(srcImage);
3255 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003256
3257 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003258 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3259 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003260 }
3261}
3262
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003263void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3264 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003265 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003266 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003267 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3268 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003269 uint32_t regionCount = pResolveImageInfo->regionCount;
3270
3271 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003272 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3273 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003274 }
3275}
3276
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003277void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3278 const VkResolveImageInfo2* pResolveImageInfo) {
3279 auto cb = GetCBState(commandBuffer);
3280 auto& funcs = cb->queue_submit_functions;
3281 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3282 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
3283 uint32_t regionCount = pResolveImageInfo->regionCount;
3284
3285 for (uint32_t i = 0; i < regionCount; i++) {
3286 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3287 pResolveImageInfo->pRegions[i].srcSubresource);
3288 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3289 pResolveImageInfo->pRegions[i].dstSubresource);
3290 }
3291}
3292
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003293void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3294 const VkClearColorValue* pColor, uint32_t rangeCount,
3295 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003296 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003297 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003298 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003299
3300 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003301 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003302 }
3303}
3304
3305void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3306 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3307 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003308 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003309 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003310 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003311
3312 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003313 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003314 }
3315}
3316
3317void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3318 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3319 const VkImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003320 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003321 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003322 auto* src = GetImageUsageState(srcImage);
3323 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003324
3325 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003326 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3327 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003328 }
3329}
3330
3331void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3332 VkImageLayout dstImageLayout, uint32_t regionCount,
3333 const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003334 auto cb = GetCBState(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* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003337
3338 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003339 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003340 }
3341}
3342
3343void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3344 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003345 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003346 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003347 auto* src = GetImageUsageState(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003348
3349 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003350 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003351 }
3352}
3353
3354void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3355 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3356 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003357 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003358 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003359 auto* src = GetImageUsageState(srcImage);
3360 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003361
3362 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003363 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3364 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003365 }
3366}
3367
Attilio Provenzano02859b22020-02-27 14:17:28 +00003368bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3369 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3370 bool skip = false;
3371
3372 if (VendorCheckEnabled(kBPVendorArm)) {
3373 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3374 skip |= LogPerformanceWarning(
3375 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3376 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3377 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3378 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003379 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003380 }
3381
3382 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3383 skip |= LogPerformanceWarning(
3384 device, kVUID_BestPractices_CreateSampler_LodClamping,
3385 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3386 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3387 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3388 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3389 }
3390
3391 if (pCreateInfo->mipLodBias != 0.0f) {
3392 skip |=
3393 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3394 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3395 "descriptors being created and may cause reduced performance.",
3396 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3397 }
3398
3399 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3400 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3401 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3402 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3403 skip |= LogPerformanceWarning(
3404 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3405 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3406 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3407 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3408 VendorSpecificTag(kBPVendorArm));
3409 }
3410
3411 if (pCreateInfo->unnormalizedCoordinates) {
3412 skip |= LogPerformanceWarning(
3413 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3414 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3415 "descriptors being created and may cause reduced performance.",
3416 VendorSpecificTag(kBPVendorArm));
3417 }
3418
3419 if (pCreateInfo->anisotropyEnable) {
3420 skip |= LogPerformanceWarning(
3421 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3422 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3423 "and may cause reduced performance.",
3424 VendorSpecificTag(kBPVendorArm));
3425 }
3426 }
3427
3428 return skip;
3429}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003430
Nadav Gevaf0808442021-05-21 13:51:25 -04003431void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3432 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3433 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3434 void* cgpl_state) {
3435 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3436 pPipelines);
3437 // AMD best practice
3438 num_pso += createInfoCount;
3439}
3440
3441bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3442 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3443 const VkCopyDescriptorSet* pDescriptorCopies) const {
3444 bool skip = false;
3445 if (VendorCheckEnabled(kBPVendorAMD)) {
3446 if (descriptorCopyCount > 0) {
3447 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3448 "%s Performance warning: copying descriptor sets is not recommended",
3449 VendorSpecificTag(kBPVendorAMD));
3450 }
3451 }
3452
3453 return skip;
3454}
3455
3456bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3457 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3458 const VkAllocationCallbacks* pAllocator,
3459 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3460 bool skip = false;
3461 if (VendorCheckEnabled(kBPVendorAMD)) {
3462 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3463 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3464 "vkUpdateDescriptorSet instead",
3465 VendorSpecificTag(kBPVendorAMD));
3466 }
3467
3468 return skip;
3469}
3470
3471bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3472 const VkClearColorValue* pColor, uint32_t rangeCount,
3473 const VkImageSubresourceRange* pRanges) const {
3474 bool skip = false;
3475 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003476 skip |= LogPerformanceWarning(
3477 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003478 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3479 "vkCmdClearAttachments instead",
3480 VendorSpecificTag(kBPVendorAMD));
3481 }
3482
3483 return skip;
3484}
3485
3486bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3487 VkImageLayout imageLayout,
3488 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3489 const VkImageSubresourceRange* pRanges) const {
3490 bool skip = false;
3491 if (VendorCheckEnabled(kBPVendorAMD)) {
3492 skip |= LogPerformanceWarning(
3493 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3494 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3495 "vkCmdClearAttachments instead",
3496 VendorSpecificTag(kBPVendorAMD));
3497 }
3498
3499 return skip;
3500}
3501
3502bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3503 const VkAllocationCallbacks* pAllocator,
3504 VkPipelineLayout* pPipelineLayout) const {
3505 bool skip = false;
3506 if (VendorCheckEnabled(kBPVendorAMD)) {
3507 // Descriptor sets cost 1 DWORD each.
3508 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3509 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3510 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3511 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3512 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003513 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Nadav Gevaf0808442021-05-21 13:51:25 -04003514 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * (robust_buffer_access ? 4 : 2);
3515 }
3516
3517 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3518 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3519 }
3520
3521 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3522 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3523 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3524 "Descriptor sets cost 1 DWORD each. "
3525 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3526 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3527 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3528 VendorSpecificTag(kBPVendorAMD));
3529 }
3530 }
3531
3532 return skip;
3533}
3534
3535bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3536 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3537 const VkImageCopy* pRegions) const {
3538 bool skip = false;
3539 std::stringstream src_image_hex;
3540 std::stringstream dst_image_hex;
3541 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3542 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3543
3544 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003545 auto src_state = Get<IMAGE_STATE>(srcImage);
3546 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003547
3548 if (src_state && dst_state) {
3549 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3550 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3551 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3552 skip |=
3553 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3554 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3555 "image (vkCmdCopyImageToBuffer) "
3556 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3557 "copies when converting between linear and optimal images",
3558 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3559 }
3560 }
3561 }
3562
3563 return skip;
3564}
3565
3566bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3567 VkPipeline pipeline) const {
3568 bool skip = false;
3569
3570 if (VendorCheckEnabled(kBPVendorAMD)) {
3571 if (pipelines_used_in_frame.find(pipeline) != pipelines_used_in_frame.end()) {
3572 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3573 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3574 "for example, by sorting draw calls by pipeline.",
3575 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3576 }
3577 }
3578
3579 return skip;
3580}
3581
3582void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3583 VkFence fence, VkResult result) {
3584 // AMD best practice
3585 num_queue_submissions += submitCount;
3586}
3587
3588bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3589 bool skip = false;
3590
3591 if (VendorCheckEnabled(kBPVendorAMD)) {
3592 if (num_queue_submissions > kNumberOfSubmissionWarningLimitAMD) {
3593 skip |= LogPerformanceWarning(
3594 device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3595 "%s Performance warning: command buffers submitted %" PRId32 " times this frame. Submitting command buffers has a CPU "
3596 "and GPU overhead. Submit fewer times to incur less overhead.",
3597 VendorSpecificTag(kBPVendorAMD), num_queue_submissions);
3598 }
3599 }
3600
3601 return skip;
3602}
3603
3604void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3605 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3606 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3607 uint32_t bufferMemoryBarrierCount,
3608 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3609 uint32_t imageMemoryBarrierCount,
3610 const VkImageMemoryBarrier* pImageMemoryBarriers) {
3611 num_barriers_objects += memoryBarrierCount;
3612 num_barriers_objects += imageMemoryBarrierCount;
3613 num_barriers_objects += bufferMemoryBarrierCount;
3614}
3615
3616void BestPractices::ManualPostCallRecordCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3617 const VkAllocationCallbacks* pAllocator, VkFence* pFence, VkResult result) {
3618 // AMD best practice
3619 if (result == VK_SUCCESS) {
3620 num_fence_objects++;
3621 }
3622}
3623
3624void BestPractices::ManualPostCallRecordCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3625 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore,
3626 VkResult result) {
3627 // AMD best practice
3628 if (result == VK_SUCCESS) {
3629 num_semaphore_objects++;
3630 }
3631}
3632
3633bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3634 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3635 bool skip = false;
3636 if (VendorCheckEnabled(kBPVendorAMD)) {
3637 if (num_semaphore_objects > kMaxRecommendedSemaphoreObjectsSizeAMD) {
3638 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3639 "%s Performance warning: High number of vkSemaphore objects created."
3640 "Minimize the amount of queue synchronization that is used. "
3641 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3642 VendorSpecificTag(kBPVendorAMD));
3643 }
3644 }
3645
3646 return skip;
3647}
3648
3649bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3650 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3651 bool skip = false;
3652 if (VendorCheckEnabled(kBPVendorAMD)) {
3653 if (num_fence_objects > kMaxRecommendedFenceObjectsSizeAMD) {
3654 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3655 "%s Performance warning: High number of VkFence objects created."
3656 "Minimize the amount of CPU-GPU synchronization that is used. "
3657 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3658 VendorSpecificTag(kBPVendorAMD));
3659 }
3660 }
3661
3662 return skip;
3663}
3664
Sam Walls8e77e4f2020-03-16 20:47:40 +00003665void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3666
3667bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3668 // look for a cache hit
3669 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3670 if (hit != _entries.end()) {
3671 // mark the cache hit as being most recently used
3672 hit->age = iteration++;
3673 return true;
3674 }
3675
3676 // if there's no cache hit, we need to model the entry being inserted into the cache
3677 CacheEntry new_entry = {value, iteration};
3678 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3679 // if there is still space left in the cache, use the next available slot
3680 *(_entries.begin() + iteration) = new_entry;
3681 } else {
3682 // otherwise replace the least recently used cache entry
3683 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3684 *lru = new_entry;
3685 }
3686 iteration++;
3687 return false;
3688}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003689
3690bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3691 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003692 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003693 bool skip = false;
3694 if (swapchain_data && swapchain_data->images.size() == 0) {
3695 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3696 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3697 "vkGetSwapchainImagesKHR after swapchain creation.");
3698 }
3699 return skip;
3700}
3701
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003702void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3703 if (no_pointer) {
3704 if (UNCALLED == call_state) {
3705 call_state = QUERY_COUNT;
3706 }
3707 } else { // Save queue family properties
3708 call_state = QUERY_DETAILS;
3709 }
3710}
3711
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003712void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3713 uint32_t* pQueueFamilyPropertyCount,
3714 VkQueueFamilyProperties* pQueueFamilyProperties) {
3715 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3716 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003717 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003718 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003719 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3720 nullptr == pQueueFamilyProperties);
3721 }
3722}
3723
3724void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3725 uint32_t* pQueueFamilyPropertyCount,
3726 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3727 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3728 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003729 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003730 if (bp_pd_state) {
3731 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3732 nullptr == pQueueFamilyProperties);
3733 }
3734}
3735
3736void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3737 uint32_t* pQueueFamilyPropertyCount,
3738 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3739 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3740 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003741 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003742 if (bp_pd_state) {
3743 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3744 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003745 }
3746}
3747
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003748void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3749 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003750 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003751 if (bp_pd_state) {
3752 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3753 }
3754}
3755
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003756void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3757 VkPhysicalDeviceFeatures2* pFeatures) {
3758 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003759 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003760 if (bp_pd_state) {
3761 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3762 }
3763}
3764
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003765void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3766 VkPhysicalDeviceFeatures2* pFeatures) {
3767 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003768 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003769 if (bp_pd_state) {
3770 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3771 }
3772}
3773
3774void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3775 VkSurfaceKHR surface,
3776 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3777 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003778 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003779 if (bp_pd_state) {
3780 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3781 }
3782}
3783
3784void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3785 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3786 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003787 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003788 if (bp_pd_state) {
3789 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3790 }
3791}
3792
3793void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3794 VkSurfaceKHR surface,
3795 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3796 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003797 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003798 if (bp_pd_state) {
3799 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3800 }
3801}
3802
3803void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3804 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3805 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003806 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003807 if (bp_pd_data) {
3808 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3809
3810 if (*pPresentModeCount) {
3811 if (call_state < QUERY_COUNT) {
3812 call_state = QUERY_COUNT;
3813 }
3814 }
3815 if (pPresentModes) {
3816 if (call_state < QUERY_DETAILS) {
3817 call_state = QUERY_DETAILS;
3818 }
3819 }
3820 }
3821}
3822
3823void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3824 uint32_t* pSurfaceFormatCount,
3825 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003826 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003827 if (bp_pd_data) {
3828 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3829
3830 if (*pSurfaceFormatCount) {
3831 if (call_state < QUERY_COUNT) {
3832 call_state = QUERY_COUNT;
3833 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003834 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003835 }
3836 if (pSurfaceFormats) {
3837 if (call_state < QUERY_DETAILS) {
3838 call_state = QUERY_DETAILS;
3839 }
3840 }
3841 }
3842}
3843
3844void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3845 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3846 uint32_t* pSurfaceFormatCount,
3847 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003848 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003849 if (bp_pd_data) {
3850 if (*pSurfaceFormatCount) {
3851 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3852 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3853 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003854 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003855 }
3856 if (pSurfaceFormats) {
3857 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3858 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3859 }
3860 }
3861 }
3862}
3863
3864void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3865 uint32_t* pPropertyCount,
3866 VkDisplayPlanePropertiesKHR* pProperties,
3867 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003868 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003869 if (bp_pd_data) {
3870 if (*pPropertyCount) {
3871 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3872 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3873 }
3874 }
3875 if (pProperties) {
3876 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3877 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3878 }
3879 }
3880 }
3881}
3882
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003883void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3884 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3885 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003886 auto swapchain_state = std::static_pointer_cast<SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003887 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3888 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3889 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003890 }
3891 }
3892}
3893
Nadav Gevaf0808442021-05-21 13:51:25 -04003894void BestPractices::ManualPostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
3895 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003896 if (VK_SUCCESS == result) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003897 if ((pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
3898 robust_buffer_access = true;
3899 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003900 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003901}
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003902
3903void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3904 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3905
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003906 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003907 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003908 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003909 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003910 auto cb = GetCBState(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003911 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003912 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003913 }
3914 }
3915 }
3916}