blob: 49f9af98f35174d604704d78ec0fa06f5d3c4243 [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)) ||
133 ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2))) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700134 skip |=
135 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
136 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600137 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700138 if (dep_info.target.length() == 0) {
139 skip |= LogWarning(instance, vuid,
140 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
141 "without replacement.",
142 api_name, extension_name);
143 } else {
144 skip |= LogWarning(instance, vuid,
145 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
146 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
147 }
148 }
149 }
150 return skip;
151}
152
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200153bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
154{
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700155 bool skip = false;
156 auto dep_info_it = special_use_extensions.find(extension_name);
157
158 if (dep_info_it != special_use_extensions.end()) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200159 const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s "
160 "and it is strongly recommended that it be otherwise avoided.";
161 auto& special_uses = dep_info_it->second;
sfricke-samsungef15e482022-01-26 11:32:49 -0800162
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700163 if (special_uses.find("cadsupport") != std::string::npos) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800164 skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
165 "specialized functionality used by CAD/CAM applications");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700166 }
167 if (special_uses.find("d3demulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200168 skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name,
169 "D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700170 }
171 if (special_uses.find("devtools") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200172 skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
173 "developer tools such as capture-replay libraries");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700174 }
175 if (special_uses.find("debugging") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200176 skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
177 "use by applications when debugging");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700178 }
179 if (special_uses.find("glemulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200180 skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700181 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200182 "specific to those APIs");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700183 }
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700184 }
185 return skip;
186}
187
Nadav Gevaf0808442021-05-21 13:51:25 -0400188void BestPractices::InitDeviceValidationObject(bool add_obj, ValidationObject* inst_obj, ValidationObject* dev_obj) {
189 if (add_obj) {
190 ValidationStateTracker::InitDeviceValidationObject(add_obj, inst_obj, dev_obj);
191 }
192}
193
194
Camden5b184be2019-08-13 07:50:19 -0600195bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500196 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600197 bool skip = false;
198
199 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
200 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800201 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700202 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
203 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600204 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600205 uint32_t specified_version =
206 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
207 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700208 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200209 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600210 }
211
212 return skip;
213}
214
Camden5b184be2019-08-13 07:50:19 -0600215bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500216 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600217 bool skip = false;
218
219 // get API version of physical device passed when creating device.
220 VkPhysicalDeviceProperties physical_device_properties{};
221 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500222 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600223
224 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600225 if (api_version > device_api_version) {
226 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600227 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600228
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700229 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
230 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
231 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600232 }
233
234 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
235 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800236 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700237 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
238 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600239 }
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600240 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700241 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200242 skip |= ValidateSpecialUseExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600243 }
244
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600245 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600246 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700247 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
248 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600249 }
250
Nadav Gevaf0808442021-05-21 13:51:25 -0400251 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
Szilard Papp7d2c7952020-06-22 14:38:13 +0100252 (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
253 skip |= LogPerformanceWarning(
254 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
Nadav Gevaf0808442021-05-21 13:51:25 -0400255 "%s %s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100256 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
257 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
258 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
Nadav Gevaf0808442021-05-21 13:51:25 -0400259 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100260 }
261
Camden5b184be2019-08-13 07:50:19 -0600262 return skip;
263}
264
265bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500266 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600267 bool skip = false;
268
269 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700270 std::stringstream buffer_hex;
271 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600272
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700273 skip |= LogWarning(
274 device, kVUID_BestPractices_SharingModeExclusive,
275 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
276 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700277 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600278 }
279
280 return skip;
281}
282
283bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500284 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600285 bool skip = false;
286
287 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700288 std::stringstream image_hex;
289 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600290
291 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700292 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
293 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
294 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700295 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600296 }
297
Attilio Provenzano02859b22020-02-27 14:17:28 +0000298 if (VendorCheckEnabled(kBPVendorArm)) {
299 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
300 skip |= LogPerformanceWarning(
301 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
302 "%s vkCreateImage(): Trying to create an image with %u samples. "
303 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
304 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
305 }
306
307 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
308 skip |= LogPerformanceWarning(
309 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
310 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
311 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
312 "and do not need to be backed by physical storage. "
313 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
314 VendorSpecificTag(kBPVendorArm));
315 }
316 }
317
Nadav Gevaf0808442021-05-21 13:51:25 -0400318 if (VendorCheckEnabled(kBPVendorAMD)) {
319 std::stringstream image_hex;
320 image_hex << "0x" << std::hex << HandleToUint64(pImage);
321
322 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
323 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
324 skip |= LogPerformanceWarning(device,
325 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
326 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
327 "Using a SHARING_MODE_CONCURRENT "
328 "is not recommended with color and depth targets",
329 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
330 }
331
332 if ((pCreateInfo->usage &
333 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
334 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
335 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
336 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
337 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
338 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
339 }
340
341 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
342 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
343 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
344 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
345 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
346 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
347 }
348 }
349
Camden5b184be2019-08-13 07:50:19 -0600350 return skip;
351}
352
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100353void BestPractices::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
354 ValidationStateTracker::PreCallRecordDestroyImage(device, image, pAllocator);
355 ReleaseImageUsageState(image);
356}
357
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200358void BestPractices::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
Tony-LunarG299187b2021-05-28 09:29:12 -0600359 if (VK_NULL_HANDLE != swapchain) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600360 auto chain = Get<SWAPCHAIN_NODE>(swapchain);
Tony-LunarG299187b2021-05-28 09:29:12 -0600361 for (auto& image : chain->images) {
362 if (image.image_state) {
363 ReleaseImageUsageState(image.image_state->image());
364 }
365 }
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200366 }
367 ValidationStateTracker::PreCallRecordDestroySwapchainKHR(device, swapchain, pAllocator);
368}
369
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100370IMAGE_STATE_BP* BestPractices::GetImageUsageState(VkImage vk_image) {
371 auto itr = imageUsageMap.find(vk_image);
372 if (itr != imageUsageMap.end()) {
373 return &itr->second;
374 } else {
375 auto& state = imageUsageMap[vk_image];
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600376 auto image = Get<IMAGE_STATE>(vk_image);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600377 state.image = image.get();
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100378 state.usages.resize(image->createInfo.arrayLayers);
379 for (auto& mips : state.usages) {
380 mips.resize(image->createInfo.mipLevels, IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED);
381 }
382 return &state;
383 }
384}
385
386void BestPractices::ReleaseImageUsageState(VkImage image) {
387 auto itr = imageUsageMap.find(image);
388 if (itr != imageUsageMap.end()) {
389 imageUsageMap.erase(itr);
390 }
391}
392
Camden5b184be2019-08-13 07:50:19 -0600393bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500394 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600395 bool skip = false;
396
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600397 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600398 if (bp_pd_state) {
399 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
400 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
401 "vkCreateSwapchainKHR() called before getting surface capabilities from "
402 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
403 }
Camden83a9c372019-08-14 11:41:38 -0600404
Shannon McPherson73e58c82021-03-05 17:14:26 -0700405 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
406 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600407 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
408 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
409 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
410 }
Camden83a9c372019-08-14 11:41:38 -0600411
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600412 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
413 skip |= LogWarning(
414 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
415 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
416 }
Camden83a9c372019-08-14 11:41:38 -0600417 }
418
Camden5b184be2019-08-13 07:50:19 -0600419 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700420 skip |=
421 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600422 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700423 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
424 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600425 }
426
Szilard Papp48a6da32020-06-10 14:41:59 +0100427 if (pCreateInfo->minImageCount == 2) {
428 skip |= LogPerformanceWarning(
429 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
430 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
431 ", which means double buffering is going "
432 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
433 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
434 "3 to use triple buffering to maximize performance in such cases.",
435 pCreateInfo->minImageCount);
436 }
437
Szilard Pappd5f0f812020-06-22 09:01:29 +0100438 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
439 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
440 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
441 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
442 "Presentation modes which are not FIFO will present the latest available frame and discard other "
443 "frame(s) if any.",
444 VendorSpecificTag(kBPVendorArm));
445 }
446
Camden5b184be2019-08-13 07:50:19 -0600447 return skip;
448}
449
450bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
451 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500452 const VkAllocationCallbacks* pAllocator,
453 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600454 bool skip = false;
455
456 for (uint32_t i = 0; i < swapchainCount; i++) {
457 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700458 skip |= LogWarning(
459 device, kVUID_BestPractices_SharingModeExclusive,
460 "Warning: A shared swapchain (index %" PRIu32
461 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
462 "queues (queueFamilyIndexCount of %" PRIu32 ").",
463 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600464 }
465 }
466
467 return skip;
468}
469
470bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500471 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600472 bool skip = false;
473
474 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
475 VkFormat format = pCreateInfo->pAttachments[i].format;
476 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
477 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
478 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700479 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
480 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
481 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
482 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
483 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600484 }
485 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700486 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
487 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
488 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
489 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
490 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600491 }
492 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000493
494 const auto& attachment = pCreateInfo->pAttachments[i];
495 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
496 bool access_requires_memory =
497 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
498
499 if (FormatHasStencil(format)) {
500 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
501 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
502 }
503
504 if (access_requires_memory) {
505 skip |= LogPerformanceWarning(
506 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
507 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
508 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
509 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
510 i, static_cast<uint32_t>(attachment.samples));
511 }
512 }
Camden5b184be2019-08-13 07:50:19 -0600513 }
514
515 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
516 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
517 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
518 }
519
520 return skip;
521}
522
Tony-LunarG767180f2020-04-23 14:03:59 -0600523bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
524 const VkImageView* image_views) const {
525 bool skip = false;
526
527 // Check for non-transient attachments that should be transient and vice versa
528 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200529 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600530 bool attachment_should_be_transient =
531 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
532
533 if (FormatHasStencil(attachment.format)) {
534 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
535 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
536 }
537
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600538 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600539 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600540 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600541
542 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
543
544 // The check for an image that should not be transient applies to all GPUs
545 if (!attachment_should_be_transient && image_is_transient) {
546 skip |= LogPerformanceWarning(
547 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
548 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
549 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
550 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
551 i);
552 }
553
554 bool supports_lazy = false;
555 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
556 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
557 supports_lazy = true;
558 }
559 }
560
561 // The check for an image that should be transient only applies to GPUs supporting
562 // lazily allocated memory
563 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
564 skip |= LogPerformanceWarning(
565 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
566 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
567 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
568 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
569 i);
570 }
571 }
572 }
573 return skip;
574}
575
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000576bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
577 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
578 bool skip = false;
579
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600580 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800581 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600582 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000583 }
584
585 return skip;
586}
587
Sam Wallse746d522020-03-16 21:20:23 +0000588bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
589 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
590 bool skip = false;
591 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
592
593 if (!skip) {
594 const auto& pool_handle = pAllocateInfo->descriptorPool;
595 auto iter = descriptor_pool_freed_count.find(pool_handle);
596 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
597 // this warning is specific to Arm
598 if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) {
599 skip |= LogPerformanceWarning(
600 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
601 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
602 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
603 VendorSpecificTag(kBPVendorArm));
604 }
605 }
606
607 return skip;
608}
609
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600610void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
611 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000612 if (result == VK_SUCCESS) {
613 // find the free count for the pool we allocated into
614 auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool);
615 if (iter != descriptor_pool_freed_count.end()) {
616 // we record successful allocations by subtracting the allocation count from the last recorded free count
617 const auto alloc_count = pAllocateInfo->descriptorSetCount;
618 // clamp the unsigned subtraction to the range [0, last_free_count]
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700619 if (iter->second > alloc_count) {
Sam Wallse746d522020-03-16 21:20:23 +0000620 iter->second -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700621 } else {
Sam Wallse746d522020-03-16 21:20:23 +0000622 iter->second = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700623 }
Sam Wallse746d522020-03-16 21:20:23 +0000624 }
625 }
626}
627
628void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
629 const VkDescriptorSet* pDescriptorSets, VkResult result) {
630 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
631 if (result == VK_SUCCESS) {
632 // we want to track frees because we're interested in suggesting re-use
633 auto iter = descriptor_pool_freed_count.find(descriptorPool);
634 if (iter == descriptor_pool_freed_count.end()) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700635 descriptor_pool_freed_count.emplace(descriptorPool, descriptorSetCount);
Sam Wallse746d522020-03-16 21:20:23 +0000636 } else {
637 iter->second += descriptorSetCount;
638 }
639 }
640}
641
Camden5b184be2019-08-13 07:50:19 -0600642bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500643 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600644 bool skip = false;
645
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500646 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700647 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
648 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600649 }
650
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000651 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
652 skip |= LogPerformanceWarning(
653 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600654 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
655 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000656 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
657 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
658 }
659
Camden83a9c372019-08-14 11:41:38 -0600660 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
661
662 return skip;
663}
664
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600665void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
666 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
667 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700668 if (result != VK_SUCCESS) {
669 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
670 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800671 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700672 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600673 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700674 return;
675 }
676 num_mem_objects++;
677}
Camden Stocker9738af92019-10-16 13:54:03 -0700678
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600679void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
680 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700681 auto error = std::find(error_codes.begin(), error_codes.end(), result);
682 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000683 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
684 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
685
686 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
687 if (common_failure != common_failure_codes.end()) {
688 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
689 } else {
690 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
691 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700692 return;
693 }
694 auto success = std::find(success_codes.begin(), success_codes.end(), result);
695 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600696 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
697 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500698 }
699}
700
Jeff Bolz5c801d12019-10-09 10:38:45 -0500701bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
702 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700703 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600704 bool skip = false;
705
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700706 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600707
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700708 for (const auto& item : mem_info->ObjectBindings()) {
709 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600710 LogObjectList objlist(device);
711 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600712 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600713 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 -0600714 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600715 }
716
Camden5b184be2019-08-13 07:50:19 -0600717 return skip;
718}
719
720void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700721 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
Camden5b184be2019-08-13 07:50:19 -0600722 if (memory != VK_NULL_HANDLE) {
723 num_mem_objects--;
724 }
725}
726
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000727bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600728 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700729 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600730
sfricke-samsunge2441192019-11-06 14:07:57 -0800731 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700732 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
733 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
734 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600735 }
736
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700737 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000738
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300739 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000740 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
741 skip |= LogPerformanceWarning(
742 device, kVUID_BestPractices_SmallDedicatedAllocation,
743 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600744 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
745 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000746 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
747 }
748
Camden Stockerb603cc82019-09-03 10:09:02 -0600749 return skip;
750}
751
752bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500753 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600754 bool skip = false;
755 const char* api_name = "BindBufferMemory()";
756
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000757 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600758
759 return skip;
760}
761
762bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500763 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600764 char api_name[64];
765 bool skip = false;
766
767 for (uint32_t i = 0; i < bindInfoCount; i++) {
768 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000769 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600770 }
771
772 return skip;
773}
Camden Stockerb603cc82019-09-03 10:09:02 -0600774
775bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500776 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600777 char api_name[64];
778 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600779
Camden Stocker8b798ab2019-09-03 10:33:28 -0600780 for (uint32_t i = 0; i < bindInfoCount; i++) {
781 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000782 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600783 }
784
785 return skip;
786}
787
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000788bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600789 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700790 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600791
sfricke-samsung71bc6572020-04-29 15:49:43 -0700792 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600793 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700794 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
795 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
796 api_name, report_data->FormatHandle(image).c_str());
797 }
798 } else {
799 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
800 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600801 }
802
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700803 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000804
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600805 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000806 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
807 skip |= LogPerformanceWarning(
808 device, kVUID_BestPractices_SmallDedicatedAllocation,
809 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600810 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
811 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000812 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
813 }
814
815 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
816 // make sure this type is actually used.
817 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
818 // (i.e.most tile - based renderers)
819 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
820 bool supports_lazy = false;
821 uint32_t suggested_type = 0;
822
823 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600824 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000825 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
826 supports_lazy = true;
827 suggested_type = i;
828 break;
829 }
830 }
831 }
832
833 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
834
835 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
836 skip |= LogPerformanceWarning(
837 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600838 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000839 "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 -0600840 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600841 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000842 }
843 }
844
Camden Stocker8b798ab2019-09-03 10:33:28 -0600845 return skip;
846}
847
848bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500849 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600850 bool skip = false;
851 const char* api_name = "vkBindImageMemory()";
852
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000853 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600854
855 return skip;
856}
857
858bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500859 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600860 char api_name[64];
861 bool skip = false;
862
863 for (uint32_t i = 0; i < bindInfoCount; i++) {
864 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700865 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600866 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
867 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600868 }
869
870 return skip;
871}
872
873bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500874 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600875 char api_name[64];
876 bool skip = false;
877
878 for (uint32_t i = 0; i < bindInfoCount; i++) {
879 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000880 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600881 }
882
883 return skip;
884}
Camden83a9c372019-08-14 11:41:38 -0600885
Attilio Provenzano02859b22020-02-27 14:17:28 +0000886static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
887 switch (format) {
888 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
889 case VK_FORMAT_R16_SFLOAT:
890 case VK_FORMAT_R16G16_SFLOAT:
891 case VK_FORMAT_R16G16B16_SFLOAT:
892 case VK_FORMAT_R16G16B16A16_SFLOAT:
893 case VK_FORMAT_R32_SFLOAT:
894 case VK_FORMAT_R32G32_SFLOAT:
895 case VK_FORMAT_R32G32B32_SFLOAT:
896 case VK_FORMAT_R32G32B32A32_SFLOAT:
897 return false;
898
899 default:
900 return true;
901 }
902}
903
904bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
905 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
906 bool skip = false;
907
908 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700909 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000910
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700911 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
912 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
913 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000914 return skip;
915 }
916
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600917 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200918 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000919
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +0200920 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
921 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
922
923 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200924 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000925 uint32_t att = subpass.pColorAttachments[j].attachment;
926
927 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
928 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
929 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
930 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
931 "color attachment #%u makes use "
932 "of a format which cannot be blended at full throughput when using MSAA.",
933 VendorSpecificTag(kBPVendorArm), i, j);
934 }
935 }
936 }
937 }
938
939 return skip;
940}
941
Nadav Gevaf0808442021-05-21 13:51:25 -0400942void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
943 const VkComputePipelineCreateInfo* pCreateInfos,
944 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
945 VkResult result, void* pipe_state) {
946 // AMD best practice
947 pipeline_cache = pipelineCache;
948}
949
Camden5b184be2019-08-13 07:50:19 -0600950bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
951 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600952 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500953 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600954 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
955 pAllocator, pPipelines, cgpl_state_data);
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600956 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600957
958 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700959 skip |= LogPerformanceWarning(
960 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
961 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
962 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600963 }
964
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000965 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200966 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000967
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600968 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200969 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600970 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700971 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
972 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600973 count++;
974 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000975 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600976 if (count > kMaxInstancedVertexBuffers) {
977 skip |= LogPerformanceWarning(
978 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
979 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
980 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
981 count, kMaxInstancedVertexBuffers);
982 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000983 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000984
Szilard Pappaaf2da32020-06-22 10:37:35 +0100985 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
986 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200987 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
988 VendorCheckEnabled(kBPVendorArm)) {
989 skip |= LogPerformanceWarning(
990 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
991 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
992 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
993 "efficiency during rasterization. Consider disabling depthBias or increasing either "
994 "depthBiasConstantFactor or depthBiasSlopeFactor.",
995 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +0100996 }
997
Attilio Provenzano02859b22020-02-27 14:17:28 +0000998 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000999 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001000 if (VendorCheckEnabled(kBPVendorAMD)) {
1001 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1002 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
1003 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1004 "improve cache hit rate", VendorSpecificTag(kBPVendorAMD));
1005 }
1006
1007 if (num_pso > kMaxRecommendedNumberOfPSOAMD) {
1008 skip |=
1009 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
1010 "%s Performance warning: Too many pipelines created, consider consolidation",
1011 VendorSpecificTag(kBPVendorAMD));
1012 }
1013
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -06001014 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001015 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
1016 "%s Performance warning: Use of primitive restart is not recommended",
1017 VendorSpecificTag(kBPVendorAMD));
1018 }
1019
1020 // TODO: this might be too aggressive of a check
1021 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
1022 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
1023 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1024 VendorSpecificTag(kBPVendorAMD));
1025 }
1026 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001027
Camden5b184be2019-08-13 07:50:19 -06001028 return skip;
1029}
1030
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001031void BestPractices::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator)
1032{
1033 auto itr = graphicsPipelineCIs.find(pipeline);
1034 if (itr != graphicsPipelineCIs.end()) {
1035 graphicsPipelineCIs.erase(itr);
1036 }
1037 ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator);
1038}
1039
Sam Walls0961ec02020-03-31 16:39:15 +01001040void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1041 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1042 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1043 VkResult result, void* cgpl_state_data) {
1044 for (size_t i = 0; i < count; i++) {
1045 const auto* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
1046 const VkPipeline pipeline_handle = pPipelines[i];
1047
1048 // record depth stencil state and color blend states for depth pre-pass tracking purposes
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001049 GraphicsPipelineCIs& cis = graphicsPipelineCIs[pipeline_handle];
Sam Walls0961ec02020-03-31 16:39:15 +01001050
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001051 auto& create_info = cgpl_state->pCreateInfos[i];
Andrej Redeky0d513072022-02-13 14:38:20 +01001052
1053 if (create_info.renderPass == VK_NULL_HANDLE) {
1054 // TODO: this is necessary to avoid crashing
1055 LogWarning(device, kVUID_BestPractices_DynamicRendering_NotSupported,
1056 "vkCreateGraphicsPipelines: pCreateInfos[%" PRIu32
1057 "].renderPass is VK_NULL_HANDLE, VK_KHR_dynamic_rendering is not supported.\n",
1058 static_cast<uint32_t>(i));
1059 continue;
1060 }
1061
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001062 auto rp = Get<RENDER_PASS_STATE>(create_info.renderPass);
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001063
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001064 if (create_info.pColorBlendState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001065 // pColorBlendState is ignored if the pipeline has rasterization disabled or if no color attachments are used
1066 bool uses_color_attachments = false;
1067 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1068 uses_color_attachments |= rp->UsesColorAttachment(j);
1069 }
1070 if (uses_color_attachments && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1071 cis.colorBlendStateCI.emplace(create_info.pColorBlendState);
1072 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001073 }
1074
1075 if (create_info.pDepthStencilState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001076 // pDepthStencilState is ignored if the pipeline has rasterization disabled or if no depth/stencil attachment is used
1077 bool uses_depth_stencil = false;
1078 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1079 uses_depth_stencil |= rp->UsesDepthStencilAttachment(j);
1080 }
1081 if (uses_depth_stencil && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1082 cis.depthStencilStateCI.emplace(create_info.pDepthStencilState);
1083 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001084 }
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001085 // 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 +02001086 auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1087 cis.accessFramebufferAttachments.clear();
1088
1089 if (cis.colorBlendStateCI) {
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001090 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1091 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, cis.colorBlendStateCI->attachmentCount);
1092 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001093 if (cis.colorBlendStateCI->pAttachments[j].colorWriteMask != 0) {
1094 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1095 if (attachment != VK_ATTACHMENT_UNUSED) {
1096 cis.accessFramebufferAttachments.push_back({ attachment, VK_IMAGE_ASPECT_COLOR_BIT });
1097 }
1098 }
1099 }
1100 }
1101
1102 if (cis.depthStencilStateCI && (cis.depthStencilStateCI->depthTestEnable ||
1103 cis.depthStencilStateCI->depthBoundsTestEnable ||
1104 cis.depthStencilStateCI->stencilTestEnable)) {
1105 uint32_t attachment = subpass.pDepthStencilAttachment ?
1106 subpass.pDepthStencilAttachment->attachment :
1107 VK_ATTACHMENT_UNUSED;
1108 if (attachment != VK_ATTACHMENT_UNUSED) {
1109 VkImageAspectFlags aspects = 0;
1110 if (cis.depthStencilStateCI->depthTestEnable || cis.depthStencilStateCI->depthBoundsTestEnable) {
1111 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1112 }
1113 if (cis.depthStencilStateCI->stencilTestEnable) {
1114 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1115 }
1116 cis.accessFramebufferAttachments.push_back({ attachment, aspects });
1117 }
1118 }
Sam Walls0961ec02020-03-31 16:39:15 +01001119 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001120
1121 // AMD best practice
1122 pipeline_cache = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001123}
1124
Camden5b184be2019-08-13 07:50:19 -06001125bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1126 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001127 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001128 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001129 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1130 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001131
1132 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001133 skip |= LogPerformanceWarning(
1134 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1135 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1136 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001137 }
1138
Nadav Gevaf0808442021-05-21 13:51:25 -04001139 if (VendorCheckEnabled(kBPVendorAMD)) {
1140 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1141 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1142 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1143 "improve cache hit rate",
1144 VendorSpecificTag(kBPVendorAMD));
1145 }
1146 }
1147
sfricke-samsung86d055a2022-02-11 14:43:50 -08001148 for (uint32_t i = 0; i < createInfoCount; i++) {
1149 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1150 if (VendorCheckEnabled(kBPVendorArm)) {
1151 skip |= ValidateCreateComputePipelineArm(createInfo);
1152 }
1153
1154 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1155 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1156 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1157 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1158 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1159 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1160 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1161 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1162 i);
1163 }
1164 }
1165 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001166 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001167
1168 return skip;
1169}
1170
1171bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1172 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001173 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001174 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001175 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1176 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001177
1178 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001179 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001180
1181 uint32_t thread_count = x * y * z;
1182
1183 // Generate a priori warnings about work group sizes.
1184 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1185 skip |= LogPerformanceWarning(
1186 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1187 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1188 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1189 "groups with less than %u threads, especially when using barrier() or shared memory.",
1190 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1191 }
1192
1193 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1194 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1195 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1196 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1197 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1198 "%u, %u) is not aligned to %u "
1199 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1200 "leave threads idle on the shader "
1201 "core.",
1202 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1203 kThreadGroupDispatchCountAlignmentArm);
1204 }
1205
sfricke-samsungef15e482022-01-26 11:32:49 -08001206 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1207 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001208
1209 unsigned dimensions = 0;
1210 if (x > 1) dimensions++;
1211 if (y > 1) dimensions++;
1212 if (z > 1) dimensions++;
1213 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1214 dimensions = std::max(dimensions, 1u);
1215
1216 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1217 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1218 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1219 bool accesses_2d = false;
1220 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001221 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001222 if (dim < 0) continue;
1223 auto spvdim = spv::Dim(dim);
1224 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1225 }
1226
1227 if (accesses_2d && dimensions < 2) {
1228 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1229 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1230 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1231 "exhibiting poor spatial locality with respect to one or more shader resources.",
1232 VendorSpecificTag(kBPVendorArm), x, y, z);
1233 }
1234
Camden5b184be2019-08-13 07:50:19 -06001235 return skip;
1236}
1237
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001238bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001239 bool skip = false;
1240
1241 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001242 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1243 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001244 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001245 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1246 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001247 }
1248
1249 return skip;
1250}
1251
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001252bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1253 bool skip = false;
1254
1255 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1256 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1257 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1258 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1259 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1260 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1261 }
1262
1263 return skip;
1264}
1265
1266bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1267 bool skip = false;
1268 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1269
1270 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1271 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1272
1273 return skip;
1274}
1275
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001276void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001277 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1278 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1279 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1280 LogPerformanceWarning(
1281 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1282 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1283 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1284 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1285 "convenient opportunity.",
1286 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1287 }
1288 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001289
1290 // AMD best practice
1291 // end-of-frame cleanup
1292 num_queue_submissions = 0;
1293 num_barriers_objects = 0;
1294 pipelines_used_in_frame.clear();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001295}
1296
Jeff Bolz5c801d12019-10-09 10:38:45 -05001297bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1298 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001299 bool skip = false;
1300
1301 for (uint32_t submit = 0; submit < submitCount; submit++) {
1302 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1303 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1304 }
1305 }
1306
1307 return skip;
1308}
1309
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001310bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1311 VkFence fence) const {
1312 bool skip = false;
1313
1314 for (uint32_t submit = 0; submit < submitCount; submit++) {
1315 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1316 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1317 }
1318 }
1319
1320 return skip;
1321}
1322
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001323bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1324 VkFence fence) const {
1325 bool skip = false;
1326
1327 for (uint32_t submit = 0; submit < submitCount; submit++) {
1328 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1329 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1330 }
1331 }
1332
1333 return skip;
1334}
1335
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001336bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1337 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1338 bool skip = false;
1339
1340 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1341 skip |= LogPerformanceWarning(
1342 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1343 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1344 "pool instead.");
1345 }
1346
1347 return skip;
1348}
1349
1350bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1351 const VkCommandBufferBeginInfo* pBeginInfo) const {
1352 bool skip = false;
1353
1354 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1355 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1356 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1357 }
1358
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001359 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1360 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001361 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1362 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1363 VendorSpecificTag(kBPVendorArm));
1364 }
1365
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001366 return skip;
1367}
1368
Jeff Bolz5c801d12019-10-09 10:38:45 -05001369bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001370 bool skip = false;
1371
1372 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1373
1374 return skip;
1375}
1376
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001377bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1378 const VkDependencyInfoKHR* pDependencyInfo) const {
1379 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1380}
1381
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001382bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1383 const VkDependencyInfo* pDependencyInfo) const {
1384 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1385}
1386
Jeff Bolz5c801d12019-10-09 10:38:45 -05001387bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1388 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001389 bool skip = false;
1390
1391 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1392
1393 return skip;
1394}
1395
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001396bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1397 VkPipelineStageFlags2KHR stageMask) const {
1398 bool skip = false;
1399
1400 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1401
1402 return skip;
1403}
1404
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001405bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1406 VkPipelineStageFlags2 stageMask) const {
1407 bool skip = false;
1408
1409 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1410
1411 return skip;
1412}
1413
Camden5b184be2019-08-13 07:50:19 -06001414bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1415 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1416 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1417 uint32_t bufferMemoryBarrierCount,
1418 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1419 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001420 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001421 bool skip = false;
1422
1423 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1424 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1425
1426 return skip;
1427}
1428
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001429bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1430 const VkDependencyInfoKHR* pDependencyInfos) const {
1431 bool skip = false;
1432 for (uint32_t i = 0; i < eventCount; i++) {
1433 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1434 }
1435
1436 return skip;
1437}
1438
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001439bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1440 const VkDependencyInfo* pDependencyInfos) const {
1441 bool skip = false;
1442 for (uint32_t i = 0; i < eventCount; i++) {
1443 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1444 }
1445
1446 return skip;
1447}
1448
Camden5b184be2019-08-13 07:50:19 -06001449bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1450 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1451 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1452 uint32_t bufferMemoryBarrierCount,
1453 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1454 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001455 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001456 bool skip = false;
1457
1458 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1459 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1460
Nadav Gevaf0808442021-05-21 13:51:25 -04001461 if (VendorCheckEnabled(kBPVendorAMD)) {
1462 if (num_barriers_objects + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
1463 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
1464 "%s Performance warning: In this frame, %" PRIu32 " barriers were already submitted. Barriers have a high cost and can "
1465 "stall the GPU. "
1466 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1467 VendorSpecificTag(kBPVendorAMD), num_barriers_objects);
1468 }
1469
1470 std::array<VkImageLayout, 3> read_layouts = {
1471 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1472 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1473 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1474 };
1475
1476 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1477 // read to read barriers
1478 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1479 bool old_is_read_layout = found != read_layouts.end();
1480 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1481 bool new_is_read_layout = found != read_layouts.end();
1482 if (old_is_read_layout && new_is_read_layout) {
1483 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1484 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1485 "time you use it.",
1486 VendorSpecificTag(kBPVendorAMD));
1487 }
1488
1489 // general with no storage
1490 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1491 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1492 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1493 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1494 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1495 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1496 VendorSpecificTag(kBPVendorAMD));
1497 }
1498 }
1499 }
1500 }
1501
Camden5b184be2019-08-13 07:50:19 -06001502 return skip;
1503}
1504
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001505bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1506 const VkDependencyInfoKHR* pDependencyInfo) const {
1507 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1508}
1509
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001510bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1511 const VkDependencyInfo* pDependencyInfo) const {
1512 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1513}
1514
Camden5b184be2019-08-13 07:50:19 -06001515bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001516 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001517 bool skip = false;
1518
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001519 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1520
1521 return skip;
1522}
1523
1524bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1525 VkQueryPool queryPool, uint32_t query) const {
1526 bool skip = false;
1527
1528 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001529
1530 return skip;
1531}
1532
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001533bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1534 VkQueryPool queryPool, uint32_t query) const {
1535 bool skip = false;
1536
1537 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1538
1539 return skip;
1540}
1541
Sam Walls0961ec02020-03-31 16:39:15 +01001542void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1543 VkPipeline pipeline) {
1544 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1545
Nadav Gevaf0808442021-05-21 13:51:25 -04001546 // AMD best practice
1547 pipelines_used_in_frame.emplace(pipeline);
1548
Sam Walls0961ec02020-03-31 16:39:15 +01001549 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
1550 // check for depth/blend state tracking
1551 auto gp_cis = graphicsPipelineCIs.find(pipeline);
1552 if (gp_cis != graphicsPipelineCIs.end()) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001553 auto cb_node = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001554 assert(cb_node);
1555 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001556
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001557 render_pass_state.nextDrawTouchesAttachments = gp_cis->second.accessFramebufferAttachments;
1558 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001559
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001560 const auto& blend_state = gp_cis->second.colorBlendStateCI;
1561 const auto& stencil_state = gp_cis->second.depthStencilStateCI;
Sam Walls0961ec02020-03-31 16:39:15 +01001562
1563 if (blend_state) {
1564 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001565 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001566 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1567 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001568 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001569 }
1570 }
1571 }
1572
1573 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001574 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001575
1576 if (stencil_state && stencil_state->depthTestEnable) {
1577 switch (stencil_state->depthCompareOp) {
1578 case VK_COMPARE_OP_EQUAL:
1579 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1580 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001581 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001582 break;
1583 default:
1584 break;
1585 }
1586 }
Sam Walls0961ec02020-03-31 16:39:15 +01001587 }
1588 }
1589}
1590
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001591static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1592 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1593 const auto& subpass_info = createInfo.pSubpasses[subpass];
1594 if (subpass_info.pResolveAttachments) {
1595 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1596 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1597 }
1598 }
1599 }
1600
1601 return false;
1602}
1603
Attilio Provenzano02859b22020-02-27 14:17:28 +00001604static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1605 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001606 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001607
1608 // If an attachment is ever used as a color attachment,
1609 // resolve attachment or depth stencil attachment,
1610 // it needs to exist on tile at some point.
1611
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001612 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1613 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001614 }
1615
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001616 if (subpass_info.pResolveAttachments) {
1617 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1618 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1619 }
1620 }
1621
1622 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001623 }
1624
1625 return false;
1626}
1627
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001628static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1629 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1630 return false;
1631 }
1632
1633 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001634 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001635
1636 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1637 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1638 return true;
1639 }
1640 }
1641 }
1642
1643 return false;
1644}
1645
Attilio Provenzano02859b22020-02-27 14:17:28 +00001646bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1647 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1648 bool skip = false;
1649
1650 if (!pRenderPassBegin) {
1651 return skip;
1652 }
1653
Gareth Webbdc6549a2021-06-16 03:52:24 +01001654 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1655 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1656 "This render pass has a zero-size render area. It cannot write to any attachments, "
1657 "and can only be used for side effects such as layout transitions.");
1658 }
1659
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001660 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001661 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001662 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001663 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001664 if (rpabi) {
1665 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1666 }
1667 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001668 // Check if any attachments have LOAD operation on them
1669 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001670 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001671
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001672 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001673 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001674 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001675 }
1676
1677 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001678 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001679 }
1680
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001681 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001682
1683 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001684 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1685 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001686 }
1687
1688 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001689 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1690 skip |= LogPerformanceWarning(
1691 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1692 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1693 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001694 "which will copy in total %u pixels (renderArea = "
1695 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001696 VendorSpecificTag(kBPVendorArm), att,
1697 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1698 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1699 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001700 }
1701 }
1702 }
1703
1704 return skip;
1705}
1706
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001707void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1708 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001709 if (view) {
Jeremy Gebbenb4d17012021-07-08 13:18:15 -06001710 QueueValidateImage(funcs, function_name, GetImageUsageState(view->create_info.image), usage,
1711 view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001712 }
1713}
1714
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001715void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1716 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001717 const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001718 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001719
1720 // If we're viewing a 3D slice, ignore base array layer.
1721 // The entire 3D subresource is accessed as one atomic unit.
1722 const uint32_t base_array_layer = image->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
1723
1724 const uint32_t max_layers = image->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001725 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
1726 const uint32_t max_levels = image->createInfo.mipLevels - subresource_range.baseMipLevel;
1727 const uint32_t mip_levels = std::min(image->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001728
1729 for (uint32_t layer = 0; layer < array_layers; layer++) {
1730 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001731 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1732 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001733 }
1734 }
1735}
1736
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001737void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1738 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001739 const VkImageSubresourceLayers& subresource_layers) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001740 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001741 const uint32_t max_layers = image->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
1742 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001743
1744 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001745 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001746 }
1747}
1748
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001749void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1750 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001751 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07001752 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker&, const QUEUE_STATE&,
1753 const CMD_BUFFER_STATE&) -> bool {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001754 ValidateImageInQueue(function_name, state, usage, array_layer, mip_level);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001755 return false;
1756 });
1757}
1758
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001759void BestPractices::ValidateImageInQueueArm(const char* function_name, IMAGE_STATE* image,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001760 IMAGE_SUBRESOURCE_USAGE_BP last_usage,
1761 IMAGE_SUBRESOURCE_USAGE_BP usage,
1762 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001763 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001764 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 -06001765 !image->IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001766 LogPerformanceWarning(
1767 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001768 "%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 +02001769 "image was used, it was written to with STORE_OP_STORE. "
1770 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1771 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001772 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001773 } 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 +02001774 LogPerformanceWarning(
1775 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001776 "%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 +02001777 "image was used, it was written to with vkCmdClear*Image(). "
1778 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1779 "tile-based architectures."
1780 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001781 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001782 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
1783 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE ||
1784 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1785 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE ||
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001786 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001787 const char *last_cmd = nullptr;
1788 const char *vuid = nullptr;
1789 const char *suggestion = nullptr;
1790
1791 switch (last_usage) {
1792 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1793 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1794 last_cmd = "vkCmdBlitImage";
1795 suggestion =
1796 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1797 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1798 "which avoids the memory roundtrip.";
1799 break;
1800 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1801 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1802 last_cmd = "vkCmdClear*Image";
1803 suggestion =
1804 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1805 "tile-based architectures. "
1806 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1807 break;
1808 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1809 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1810 last_cmd = "vkCmdCopy*Image";
1811 suggestion =
1812 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1813 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1814 "which avoids the memory roundtrip.";
1815 break;
1816 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1817 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1818 last_cmd = "vkCmdResolveImage";
1819 suggestion =
1820 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1821 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1822 "which avoids the memory roundtrip.";
1823 break;
1824 default:
1825 break;
1826 }
1827
1828 LogPerformanceWarning(
1829 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001830 "%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 +01001831 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001832 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001833 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001834}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001835
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001836void BestPractices::ValidateImageInQueue(const char* function_name, IMAGE_STATE_BP* state,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001837 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer,
1838 uint32_t mip_level) {
1839 IMAGE_STATE* image = state->image;
1840 IMAGE_SUBRESOURCE_USAGE_BP last_usage = state->usages[array_layer][mip_level];
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001841 state->usages[array_layer][mip_level] = usage;
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001842 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001843 ValidateImageInQueueArm(function_name, image, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001844 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001845}
1846
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001847void BestPractices::AddDeferredQueueOperations(CMD_BUFFER_STATE_BP* cb) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001848 cb->queue_submit_functions.insert(cb->queue_submit_functions.end(),
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001849 cb->queue_submit_functions_after_render_pass.begin(),
1850 cb->queue_submit_functions_after_render_pass.end());
1851 cb->queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001852}
1853
1854void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1855 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001856 auto cb_node = GetCBState(commandBuffer);
1857 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001858}
1859
1860void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1861 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001862 auto cb_node = GetCBState(commandBuffer);
1863 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001864}
1865
1866void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1867 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001868 auto cb_node = GetCBState(commandBuffer);
1869 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001870}
1871
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001872void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1873 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001874 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001875 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001876 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1877}
1878
1879void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1880 const VkRenderPassBeginInfo* pRenderPassBegin,
1881 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1882 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1883 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1884}
1885
1886void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1887 const VkRenderPassBeginInfo* pRenderPassBegin,
1888 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1889 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1890 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1891}
1892
1893void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001894
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001895 if (!pRenderPassBegin) {
1896 return;
1897 }
1898
Jeremy Gebben9f537102021-10-05 16:37:12 -06001899 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001900
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001901 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001902 if (rp_state) {
1903 // Check load ops
1904 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001905 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001906
1907 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1908 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1909 continue;
1910 }
1911
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001912 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001913
1914 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1915 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001916 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001917 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1918 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001919 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001920 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001921 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001922 }
1923
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001924 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001925 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001926
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001927 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001928 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1929 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001930 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001931 }
1932 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001933 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001934 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001935
Jeremy Gebben9f537102021-10-05 16:37:12 -06001936 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001937 }
1938
1939 // Check store ops
1940 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001941 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001942
1943 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1944 continue;
1945 }
1946
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001947 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001948
1949 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1950 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001951 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001952 }
1953
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001954 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001955
Jeremy Gebben9f537102021-10-05 16:37:12 -06001956 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001957 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001958 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1959 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001960 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001961 }
1962 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001963 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001964 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001965
Jeremy Gebben9f537102021-10-05 16:37:12 -06001966 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001967 }
1968 }
1969}
1970
Attilio Provenzano02859b22020-02-27 14:17:28 +00001971bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1972 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001973 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1974 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001975 return skip;
1976}
1977
1978bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1979 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001980 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001981 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1982 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001983 return skip;
1984}
1985
1986bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001987 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001988 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1989 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001990 return skip;
1991}
1992
Sam Walls0961ec02020-03-31 16:39:15 +01001993void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1994 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001995 // Reset the renderpass state
Jeremy Gebben9f537102021-10-05 16:37:12 -06001996 auto cb = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001997 cb->hasDrawCmd = false;
1998 assert(cb);
1999 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002000 render_pass_state.touchesAttachments.clear();
2001 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002002 render_pass_state.numDrawCallsDepthOnly = 0;
2003 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2004 render_pass_state.colorAttachment = false;
2005 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002006 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002007 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002008
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002009 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002010
2011 // track depth / color attachment usage within the renderpass
2012 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2013 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002014 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002015
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002016 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002017 }
2018}
2019
2020void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2021 VkSubpassContents contents) {
2022 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2023 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2024}
2025
2026void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2027 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2028 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2029 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2030}
2031
2032void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2033 const VkRenderPassBeginInfo* pRenderPassBegin,
2034 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2035 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2036 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2037}
2038
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002039// Generic function to handle validation for all CmdDraw* type functions
2040bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2041 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002042 const auto cb_state = GetCBState(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002043 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002044 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2045 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002046 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002047
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002048 // Verify vertex binding
2049 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
2050 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002051 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002052 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002053 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2054 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002055 }
2056 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002057
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002058 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002059 if (pipe) {
2060 const auto* rp_state = pipe->rp_state.get();
2061 if (rp_state) {
2062 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2063 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002064 const auto& create_info = pipe->create_info.graphics;
2065 const uint32_t depth_stencil_attachment =
2066 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2067 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2068 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002069 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2070 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2071 }
2072 }
2073 }
2074 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002075 }
2076 return skip;
2077}
2078
Sam Walls0961ec02020-03-31 16:39:15 +01002079void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002080 auto cb_node = GetCBState(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002081 assert(cb_node);
2082 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002083 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002084 RecordCmdDrawTypeArm(render_pass_state, draw_count, caller);
2085 }
2086
2087 if (render_pass_state.drawTouchAttachments) {
2088 for (auto& touch : render_pass_state.nextDrawTouchesAttachments) {
2089 RecordAttachmentAccess(render_pass_state, touch.framebufferAttachment, touch.aspects);
2090 }
2091 // No need to touch the same attachments over and over.
2092 render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002093 }
2094}
2095
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002096void BestPractices::RecordCmdDrawTypeArm(RenderPassState& render_pass_state, uint32_t draw_count, const char* caller) {
2097 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2098 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2099 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002100 }
2101}
2102
Camden5b184be2019-08-13 07:50:19 -06002103bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002104 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002105 bool skip = false;
2106
2107 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002108 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2109 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002110 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002111 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002112
2113 return skip;
2114}
2115
Sam Walls0961ec02020-03-31 16:39:15 +01002116void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2117 uint32_t firstVertex, uint32_t firstInstance) {
2118 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2119 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2120}
2121
Camden5b184be2019-08-13 07:50:19 -06002122bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002123 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002124 bool skip = false;
2125
2126 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002127 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2128 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002129 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002130 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2131
Attilio Provenzano02859b22020-02-27 14:17:28 +00002132 // Check if we reached the limit for small indexed draw calls.
2133 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben9f537102021-10-05 16:37:12 -06002134 const auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002135 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002136 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2137 VendorCheckEnabled(kBPVendorArm)) {
2138 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002139 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002140 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2141 "You can try batching drawcalls or instancing when applicable.",
2142 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2143 }
2144
Sam Walls8e77e4f2020-03-16 20:47:40 +00002145 if (VendorCheckEnabled(kBPVendorArm)) {
2146 ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2147 }
2148
2149 return skip;
2150}
2151
2152bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2153 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2154 bool skip = false;
2155
2156 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Jeremy Gebben9f537102021-10-05 16:37:12 -06002157 const auto cmd_state = GetCBState(commandBuffer);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002158 if (cmd_state == nullptr) return skip;
2159
locke-lunarg1ae57d62020-11-18 10:49:19 -07002160 const auto* ib_state = cmd_state->index_buffer_binding.buffer_state.get();
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002161 if (ib_state == nullptr || cmd_state->index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002162
2163 const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002164 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002165 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2166 const void* ib_mem = ib_mem_state.p_driver_data;
2167 bool primitive_restart_enable = false;
2168
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002169 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2170 const auto& pipeline_binding_iter = cmd_state->lastBound[lv_bind_point];
2171 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002172
Jeremy Gebben11af9792021-08-20 10:20:09 -06002173 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2174 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002175 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002176
2177 // 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 -06002178 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002179 uint32_t scan_stride;
2180 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2181 scan_stride = sizeof(uint8_t);
2182 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2183 scan_stride = sizeof(uint16_t);
2184 } else {
2185 scan_stride = sizeof(uint32_t);
2186 }
2187
2188 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2189 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2190
2191 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2192 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2193 // irrespective of whether or not they're part of the draw call.
2194
2195 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2196 uint32_t min_index = ~0u;
2197 // start with maximum as 0 and adjust to indices in the buffer
2198 uint32_t max_index = 0u;
2199
2200 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2201 // for the given index buffer
2202 uint32_t vertex_shade_count = 0;
2203
2204 PostTransformLRUCacheModel post_transform_cache;
2205
2206 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2207 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2208 // target architecture.
2209 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2210 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2211 post_transform_cache.resize(32);
2212
2213 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2214 uint32_t scan_index;
2215 uint32_t primitive_restart_value;
2216 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2217 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2218 primitive_restart_value = 0xFF;
2219 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2220 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2221 primitive_restart_value = 0xFFFF;
2222 } else {
2223 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2224 primitive_restart_value = 0xFFFFFFFF;
2225 }
2226
2227 max_index = std::max(max_index, scan_index);
2228 min_index = std::min(min_index, scan_index);
2229
2230 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2231 bool in_cache = post_transform_cache.query_cache(scan_index);
2232 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2233 if (!in_cache) vertex_shade_count++;
2234 }
2235 }
2236
2237 // 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 +01002238 // 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
2239 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002240
2241 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002242 skip |=
2243 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2244 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2245 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2246 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2247 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2248 VendorSpecificTag(kBPVendorArm),
2249 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002250 return skip;
2251 }
2252
2253 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2254 // 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 +01002255 const size_t refs_per_bucket = 64;
2256 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2257
2258 const uint32_t n_indices = max_index - min_index + 1;
2259 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2260 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2261
2262 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2263 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002264
2265 // To avoid using too much memory, we run over the indices again.
2266 // Knowing the size from the last scan allows us to record index usage with bitsets
2267 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2268 uint32_t scan_index;
2269 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2270 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2271 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2272 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2273 } else {
2274 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2275 }
2276 // keep track of the set of all indices used to reference vertices in the draw call
2277 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002278 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2279 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002280 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2281 }
2282
2283 uint32_t vertex_reference_count = 0;
2284 for (const auto& bitset : vertex_reference_buckets) {
2285 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2286 }
2287
2288 // 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 -07002289 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002290 // 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 -07002291 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002292
2293 if (utilization < 0.5f) {
2294 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2295 "%s The indices which were specified for the draw call only utilise approximately "
2296 "%.02f%% of the bound vertex buffer.",
2297 VendorSpecificTag(kBPVendorArm), utilization);
2298 }
2299
2300 if (cache_hit_rate <= 0.5f) {
2301 skip |=
2302 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2303 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2304 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2305 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2306 "recently shaded vertices.",
2307 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2308 }
2309 }
2310
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002311 return skip;
2312}
2313
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002314bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2315 const VkCommandBuffer* pCommandBuffers) const {
2316 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002317 const auto primary = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002318 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002319 const auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002320 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002321 continue;
2322 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002323 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002324 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002325 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(clear.rects.size()), clear.rects.data())) {
2326 skip |= ValidateClearAttachment(commandBuffer, primary.get(), clear.framebufferAttachment, clear.colorAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002327 clear.aspects, true);
2328 }
2329 }
2330 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002331
2332 if (VendorCheckEnabled(kBPVendorAMD)) {
2333 if (commandBufferCount > 0) {
2334 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2335 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2336 VendorSpecificTag(kBPVendorAMD));
2337 }
2338 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002339 return skip;
2340}
2341
2342void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2343 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002344 auto primary = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002345 auto& primary_state = primary->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002346
2347 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002348 auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002349 if (secondary_cb == nullptr) {
2350 continue;
2351 }
2352 auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002353
2354 for (auto& early_clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002355 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2356 RecordAttachmentClearAttachments(primary.get(), primary_state, early_clear.framebufferAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002357 early_clear.colorAttachment, early_clear.aspects,
Hans-Kristian Arntzenfa8ef9f2021-06-29 12:07:59 +02002358 uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002359 } else {
2360 RecordAttachmentAccess(primary_state, early_clear.framebufferAttachment,
2361 early_clear.aspects);
2362 }
2363 }
2364
2365 for (auto& touch : secondary.touchesAttachments) {
2366 RecordAttachmentAccess(primary_state, touch.framebufferAttachment,
2367 touch.aspects);
2368 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002369
2370 primary_state.numDrawCallsDepthEqualCompare += secondary.numDrawCallsDepthEqualCompare;
2371 primary_state.numDrawCallsDepthOnly += secondary.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002372
Jeremy Gebben9f537102021-10-05 16:37:12 -06002373 auto second_state = GetCBState(pCommandBuffers[i]);
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002374 if (second_state->hasDrawCmd) {
2375 primary->hasDrawCmd = true;
2376 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002377 }
2378
2379 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2380}
2381
2382void BestPractices::RecordAttachmentAccess(RenderPassState& state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2383 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
2384 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002385 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002386 return info.framebufferAttachment == fb_attachment;
2387 });
2388
2389 if (itr != state.touchesAttachments.end()) {
2390 itr->aspects |= aspects;
2391 } else {
2392 state.touchesAttachments.push_back({ fb_attachment, aspects });
2393 }
2394}
2395
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002396void BestPractices::RecordAttachmentClearAttachments(CMD_BUFFER_STATE_BP* cmd_state, RenderPassState& state, uint32_t fb_attachment,
2397 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2398 const VkClearRect* pRects) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002399 // If we observe a full clear before any other access to a frame buffer attachment,
2400 // we have candidate for redundant clear attachments.
2401 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002402 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002403 return info.framebufferAttachment == fb_attachment;
2404 });
2405
2406 uint32_t new_aspects = aspects;
2407 if (itr != state.touchesAttachments.end()) {
2408 new_aspects = aspects & ~itr->aspects;
2409 itr->aspects |= aspects;
2410 } else {
2411 state.touchesAttachments.push_back({ fb_attachment, aspects });
2412 }
2413
2414 if (new_aspects == 0) {
2415 return;
2416 }
2417
2418 if (cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2419 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2420 // CmdExecuteCommands.
2421 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2422 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2423 }
2424}
2425
2426void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2427 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2428 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002429 auto cmd_state = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002430 RENDER_PASS_STATE* rp_state = cmd_state->activeRenderPass.get();
2431 FRAMEBUFFER_STATE* fb_state = cmd_state->activeFramebuffer.get();
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002432 RenderPassState& tracking_state = cmd_state->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002433 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2434
2435 if (rectCount == 0 || !rp_state) {
2436 return;
2437 }
2438
2439 if (!is_secondary && !fb_state) {
2440 return;
2441 }
2442
2443 // 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 -06002444 bool full_clear = ClearAttachmentsIsFullClear(cmd_state.get(), rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002445
2446 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2447 for (uint32_t i = 0; i < attachmentCount; i++) {
2448 auto& attachment = pClearAttachments[i];
2449 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2450 VkImageAspectFlags aspects = attachment.aspectMask;
2451
2452 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2453 if (subpass.pDepthStencilAttachment) {
2454 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2455 }
2456 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2457 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
2458 }
2459
2460 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2461 if (full_clear) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002462 RecordAttachmentClearAttachments(cmd_state.get(), tracking_state, fb_attachment, attachment.colorAttachment,
2463 aspects, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002464 } else {
2465 RecordAttachmentAccess(tracking_state, fb_attachment, aspects);
2466 }
2467 }
2468 }
2469
2470 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments,
2471 rectCount, pRects);
2472}
2473
Attilio Provenzano02859b22020-02-27 14:17:28 +00002474void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2475 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2476 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2477 firstInstance);
2478
Jeremy Gebben9f537102021-10-05 16:37:12 -06002479 auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002480 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2481 cmd_state->small_indexed_draw_call_count++;
2482 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002483
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002484 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002485}
2486
Sam Walls0961ec02020-03-31 16:39:15 +01002487void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2488 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2489 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2490 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2491}
2492
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002493bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2494 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2495 uint32_t maxDrawCount, uint32_t stride) const {
2496 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2497
2498 return skip;
2499}
2500
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002501bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2502 VkDeviceSize offset, VkBuffer countBuffer,
2503 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2504 uint32_t stride) const {
2505 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002506
2507 return skip;
2508}
2509
2510bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002511 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002512 bool skip = false;
2513
2514 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002515 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2516 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002517 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002518 }
2519
2520 return skip;
2521}
2522
Sam Walls0961ec02020-03-31 16:39:15 +01002523void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2524 uint32_t count, uint32_t stride) {
2525 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2526 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2527}
2528
Camden5b184be2019-08-13 07:50:19 -06002529bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002530 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002531 bool skip = false;
2532
2533 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002534 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2535 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002536 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002537 }
2538
2539 return skip;
2540}
2541
Sam Walls0961ec02020-03-31 16:39:15 +01002542void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2543 uint32_t count, uint32_t stride) {
2544 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2545 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2546}
2547
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002548void BestPractices::ValidateBoundDescriptorSets(VkCommandBuffer commandBuffer, const char* function_name) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002549 auto cb_state = GetCBState(commandBuffer);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002550
2551 if (cb_state) {
2552 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002553 const auto& layout = *descriptor_set->GetLayout();
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002554
2555 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2556 // For bindless scenarios, we should not attempt to track descriptor set state.
2557 // It is highly uncertain which resources are actually bound.
2558 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2559 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2560 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
2561 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2562 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2563 continue;
2564 }
2565
2566 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002567 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002568 VkImageView image_view{VK_NULL_HANDLE};
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002569
2570 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2571 switch (descriptor->GetClass()) {
2572 case cvdescriptorset::DescriptorClass::Image: {
2573 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2574 image_view = image_descriptor->GetImageView();
2575 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002576 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002577 }
2578 case cvdescriptorset::DescriptorClass::ImageSampler: {
2579 if (const auto image_sampler_descriptor =
2580 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2581 image_view = image_sampler_descriptor->GetImageView();
2582 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002583 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002584 }
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002585 default:
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002586 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002587 }
2588
2589 if (image_view) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002590 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002591 QueueValidateImageView(cb_state->queue_submit_functions, function_name, image_view_state.get(),
2592 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002593 }
2594 }
2595 }
2596 }
2597 }
2598}
2599
2600void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2601 uint32_t firstVertex, uint32_t firstInstance) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002602 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002603}
2604
2605void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2606 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002607 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002608}
2609
2610void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2611 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002612 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002613}
2614
Camden5b184be2019-08-13 07:50:19 -06002615bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002616 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002617 bool skip = false;
2618
2619 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002620 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2621 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2622 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2623 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002624 }
2625
2626 return skip;
2627}
Camden83a9c372019-08-14 11:41:38 -06002628
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002629bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2630 bool skip = false;
2631 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2632 skip |= ValidateCmdEndRenderPass(commandBuffer);
2633 return skip;
2634}
2635
2636bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2637 bool skip = false;
2638 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2639 skip |= ValidateCmdEndRenderPass(commandBuffer);
2640 return skip;
2641}
2642
Sam Walls0961ec02020-03-31 16:39:15 +01002643bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2644 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002645 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002646 skip |= ValidateCmdEndRenderPass(commandBuffer);
2647 return skip;
2648}
2649
2650bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2651 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002652 const auto cmd = GetCBState(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002653
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002654 if (cmd == nullptr) return skip;
2655 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002656
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002657 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2658 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2659 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002660 if (uses_depth) {
2661 skip |= LogPerformanceWarning(
2662 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2663 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2664 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2665 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2666 VendorSpecificTag(kBPVendorArm));
2667 }
2668
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002669 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2670
2671 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2672
2673 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2674 // it is redundant to have it be part of the render pass.
2675 // Only consider it redundant if it will actually consume bandwidth, i.e.
2676 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2677 // as is using pure input attachments.
2678 // CLEAR -> STORE might be considered a "useful" thing to do, but
2679 // the optimal thing to do is to defer the clear until you're actually
2680 // going to render to the image.
2681
2682 uint32_t num_attachments = rp->createInfo.attachmentCount;
2683 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002684 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2685 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002686 continue;
2687 }
2688
2689 auto& attachment = rp->createInfo.pAttachments[i];
2690
2691 VkImageAspectFlags bandwidth_aspects = 0;
2692
2693 if (!FormatIsStencilOnly(attachment.format) &&
2694 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2695 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2696 if (FormatHasDepth(attachment.format)) {
2697 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2698 } else {
2699 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2700 }
2701 }
2702
2703 if (FormatHasStencil(attachment.format) &&
2704 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2705 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2706 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2707 }
2708
2709 if (!bandwidth_aspects) {
2710 continue;
2711 }
2712
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002713 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
2714 [&](const AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002715 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002716 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002717 untouched_aspects &= ~itr->aspects;
2718 }
2719
2720 if (untouched_aspects) {
2721 skip |= LogPerformanceWarning(
2722 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2723 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2724 "was never accessed by a pipeline or clear command. "
2725 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2726 "if the attachments are not intended to be accessed.",
2727 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2728 }
2729 }
2730 }
2731
Sam Walls0961ec02020-03-31 16:39:15 +01002732 return skip;
2733}
2734
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002735void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002736 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002737}
2738
2739void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002740 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002741}
2742
Camden Stocker9c051442019-11-06 14:28:43 -08002743bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2744 const char* api_name) const {
2745 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002746 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002747
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002748 if (bp_pd_state) {
2749 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2750 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2751 "Potential problem with calling %s() without first retrieving properties from "
2752 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2753 api_name);
2754 }
Camden Stocker9c051442019-11-06 14:28:43 -08002755 }
2756
2757 return skip;
2758}
2759
Camden83a9c372019-08-14 11:41:38 -06002760bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002761 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002762 bool skip = false;
2763
Camden Stocker9c051442019-11-06 14:28:43 -08002764 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002765
Camden Stocker9c051442019-11-06 14:28:43 -08002766 return skip;
2767}
2768
2769bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2770 uint32_t planeIndex,
2771 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2772 bool skip = false;
2773
2774 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2775
2776 return skip;
2777}
2778
2779bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2780 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2781 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2782 bool skip = false;
2783
2784 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002785
2786 return skip;
2787}
Camden05de2d42019-08-19 10:23:56 -06002788
2789bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002790 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002791 bool skip = false;
2792
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002793 auto swapchain_state = std::static_pointer_cast<const SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Camden05de2d42019-08-19 10:23:56 -06002794
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002795 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002796 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002797 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002798 skip |=
2799 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2800 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2801 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002802 }
Camden05de2d42019-08-19 10:23:56 -06002803
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002804 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2805 skip |= LogWarning(
2806 device, kVUID_BestPractices_Swapchain_InvalidCount,
2807 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002808 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002809 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2810 }
2811 }
2812
Camden05de2d42019-08-19 10:23:56 -06002813 return skip;
2814}
2815
2816// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002817bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002818 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002819 const CALL_STATE call_state,
2820 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002821 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002822 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2823 if (UNCALLED == call_state) {
2824 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002825 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002826 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2827 "recommended "
2828 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2829 caller_name, caller_name);
2830 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002831 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2832 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002833 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2834 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2835 ". It is recommended to instead receive all the properties by calling %s with "
2836 "pQueueFamilyPropertyCount that was "
2837 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002838 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002839 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002840 }
2841
2842 return skip;
2843}
2844
Jeff Bolz5c801d12019-10-09 10:38:45 -05002845bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2846 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002847 bool skip = false;
2848
2849 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002850 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002851 if (!as_state->memory_requirements_checked) {
2852 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2853 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2854 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002855 skip |= LogWarning(
2856 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002857 "vkBindAccelerationStructureMemoryNV(): "
2858 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2859 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2860 }
2861 }
2862
2863 return skip;
2864}
2865
Camden05de2d42019-08-19 10:23:56 -06002866bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2867 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002868 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002869 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002870 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002871 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002872 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2873 "vkGetPhysicalDeviceQueueFamilyProperties()");
2874 }
2875 return false;
Camden05de2d42019-08-19 10:23:56 -06002876}
2877
Mike Schuchardt2df08912020-12-15 16:28:09 -08002878bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2879 uint32_t* pQueueFamilyPropertyCount,
2880 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002881 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002882 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002883 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002884 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2885 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2886 }
2887 return false;
Camden05de2d42019-08-19 10:23:56 -06002888}
2889
Jeff Bolz5c801d12019-10-09 10:38:45 -05002890bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002891 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002892 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002893 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002894 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002895 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2896 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2897 }
2898 return false;
Camden05de2d42019-08-19 10:23:56 -06002899}
2900
2901bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2902 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002903 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002904 if (!pSurfaceFormats) return false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002905 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002906 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002907 bool skip = false;
2908 if (call_state == UNCALLED) {
2909 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2910 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002911 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2912 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2913 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002914 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002915 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002916 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2917 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2918 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2919 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002920 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002921 }
2922 }
2923 return skip;
2924}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002925
2926bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002927 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002928 bool skip = false;
2929
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002930 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2931 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002932 // 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 -07002933 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002934 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2935 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002936 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002937 // 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 -07002938 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2939 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002940 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002941 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002942 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002943 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002944 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002945 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2946 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2947 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002948 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002949 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2950 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002951 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002952 }
2953 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002954 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002955 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002956 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002957 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2958 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002959 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002960 }
2961 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002962 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2963 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002964 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002965 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002966 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002967 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002968 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002969 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2970 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2971 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002972 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002973 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2974 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002975 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002976 }
2977 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002978 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002979 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002980 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002981 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2982 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002983 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002984 }
2985 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2986 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002987 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002988 }
2989 }
2990 }
2991 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002992 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2993 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002994 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002995 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002996 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2997 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002998 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002999 }
3000 }
3001 }
3002
3003 return skip;
3004}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003005
Mark Lobodzinski84101d72020-04-24 09:43:48 -06003006void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
3007 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003008 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003009 return;
3010 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003011
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003012 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3013 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
3014 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3015 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003016 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003017 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003018 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003019 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003020 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3021 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
3022 image_state->sparse_metadata_bound = true;
3023 }
3024 }
3025 }
3026 }
3027}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003028
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003029bool BestPractices::ClearAttachmentsIsFullClear(const CMD_BUFFER_STATE_BP* cmd, uint32_t rectCount,
3030 const VkClearRect* pRects) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003031 if (cmd->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3032 // We don't know the accurate render area in a secondary,
3033 // so assume we clear the entire frame buffer.
3034 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
3035 return true;
3036 }
3037
3038 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
3039 for (uint32_t i = 0; i < rectCount; i++) {
3040 auto& rect = pRects[i];
3041 auto& render_area = cmd->activeRenderPassBeginInfo.renderArea;
3042 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
3043 return true;
3044 }
3045 }
3046
3047 return false;
3048}
3049
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003050bool BestPractices::ValidateClearAttachment(VkCommandBuffer commandBuffer, const CMD_BUFFER_STATE_BP* cmd, uint32_t fb_attachment,
3051 uint32_t color_attachment, VkImageAspectFlags aspects, bool secondary) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003052 const RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3053 bool skip = false;
3054
3055 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3056 return skip;
3057 }
3058
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003059 const auto& rp_state = cmd->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003060
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003061 auto attachment_itr = std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02003062 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003063 return info.framebufferAttachment == fb_attachment;
3064 });
3065
3066 // Only report aspects which haven't been touched yet.
3067 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003068 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003069 new_aspects &= ~attachment_itr->aspects;
3070 }
3071
3072 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
3073 if (!cmd->hasDrawCmd) {
3074 skip |= LogPerformanceWarning(
3075 commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003076 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3077 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003078 report_data->FormatHandle(commandBuffer).c_str());
3079 }
3080
3081 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3082 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3083 skip |= LogPerformanceWarning(
3084 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3085 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3086 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3087 "it is more efficient.",
3088 secondary ? "vkCmdExecuteCommands(): " : "",
3089 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
3090 }
3091
3092 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3093 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3094 skip |= LogPerformanceWarning(
3095 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3096 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3097 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3098 "it is more efficient.",
3099 secondary ? "vkCmdExecuteCommands(): " : "",
3100 report_data->FormatHandle(commandBuffer).c_str());
3101 }
3102
3103 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3104 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3105 skip |= LogPerformanceWarning(
3106 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3107 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3108 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3109 "it is more efficient.",
3110 secondary ? "vkCmdExecuteCommands(): " : "",
3111 report_data->FormatHandle(commandBuffer).c_str());
3112 }
3113
3114 return skip;
3115}
3116
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003117bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003118 const VkClearAttachment* pAttachments, uint32_t rectCount,
3119 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003120 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003121 const auto cb_node = GetCBState(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003122 if (!cb_node) return skip;
3123
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003124 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3125 // Defer checks to ExecuteCommands.
3126 return skip;
3127 }
3128
3129 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben9f537102021-10-05 16:37:12 -06003130 if (!ClearAttachmentsIsFullClear(cb_node.get(), rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003131 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003132 }
3133
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003134 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3135 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003136 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003137 if (rp) {
3138 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3139
3140 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003141 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003142
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003143 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3144 uint32_t color_attachment = attachment.colorAttachment;
3145 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003146 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, color_attachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003147 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003148 }
3149
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003150 if (subpass.pDepthStencilAttachment &&
3151 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003152 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003153 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, VK_ATTACHMENT_UNUSED,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003154 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003155 }
3156 }
3157 }
3158
Nadav Gevaf0808442021-05-21 13:51:25 -04003159 if (VendorCheckEnabled(kBPVendorAMD)) {
3160 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3161 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3162 bool black_check = false;
3163 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3164 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3165 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3166 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3167 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3168
3169 bool white_check = false;
3170 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3171 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3172 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3173 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3174 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3175
3176 if (black_check && white_check) {
3177 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3178 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3179 "Consider changing to one of the following:"
3180 "RGBA(0, 0, 0, 0) "
3181 "RGBA(0, 0, 0, 1) "
3182 "RGBA(1, 1, 1, 0) "
3183 "RGBA(1, 1, 1, 1)",
3184 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3185 }
3186 } else {
3187 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3188 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3189 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3190 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3191 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3192 "attachment %" PRId32 " is not a fast clear value."
3193 "Consider changing to one of the following:"
3194 "D=0.0f, S=0"
3195 "D=1.0f, S=0",
3196 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3197 }
3198 }
3199 }
3200 }
3201
Camden Stockerf55721f2019-09-09 11:04:49 -06003202 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003203}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003204
3205bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3206 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3207 const VkImageResolve* pRegions) const {
3208 bool skip = false;
3209
3210 skip |= VendorCheckEnabled(kBPVendorArm) &&
3211 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3212 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3213 "This is a very slow and extremely bandwidth intensive path. "
3214 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3215 VendorSpecificTag(kBPVendorArm));
3216
3217 return skip;
3218}
3219
Jeff Leger178b1e52020-10-05 12:22:23 -04003220bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3221 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3222 bool skip = false;
3223
3224 skip |= VendorCheckEnabled(kBPVendorArm) &&
3225 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3226 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3227 "This is a very slow and extremely bandwidth intensive path. "
3228 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3229 VendorSpecificTag(kBPVendorArm));
3230
3231 return skip;
3232}
3233
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003234bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3235 const VkResolveImageInfo2* pResolveImageInfo) const {
3236 bool skip = false;
3237
3238 skip |= VendorCheckEnabled(kBPVendorArm) &&
3239 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3240 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3241 "This is a very slow and extremely bandwidth intensive path. "
3242 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3243 VendorSpecificTag(kBPVendorArm));
3244
3245 return skip;
3246}
3247
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003248void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3249 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3250 const VkImageResolve* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003251 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003252 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003253 auto* src = GetImageUsageState(srcImage);
3254 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003255
3256 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003257 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3258 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003259 }
3260}
3261
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003262void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3263 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003264 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003265 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003266 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3267 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003268 uint32_t regionCount = pResolveImageInfo->regionCount;
3269
3270 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003271 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3272 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003273 }
3274}
3275
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003276void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3277 const VkResolveImageInfo2* pResolveImageInfo) {
3278 auto cb = GetCBState(commandBuffer);
3279 auto& funcs = cb->queue_submit_functions;
3280 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3281 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
3282 uint32_t regionCount = pResolveImageInfo->regionCount;
3283
3284 for (uint32_t i = 0; i < regionCount; i++) {
3285 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3286 pResolveImageInfo->pRegions[i].srcSubresource);
3287 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3288 pResolveImageInfo->pRegions[i].dstSubresource);
3289 }
3290}
3291
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003292void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3293 const VkClearColorValue* pColor, uint32_t rangeCount,
3294 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003295 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003296 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003297 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003298
3299 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003300 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003301 }
3302}
3303
3304void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3305 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3306 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003307 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003308 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003309 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003310
3311 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003312 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003313 }
3314}
3315
3316void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3317 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3318 const VkImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003319 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003320 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003321 auto* src = GetImageUsageState(srcImage);
3322 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003323
3324 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003325 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3326 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003327 }
3328}
3329
3330void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3331 VkImageLayout dstImageLayout, uint32_t regionCount,
3332 const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003333 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003334 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003335 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003336
3337 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003338 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003339 }
3340}
3341
3342void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3343 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003344 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003345 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003346 auto* src = GetImageUsageState(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003347
3348 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003349 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003350 }
3351}
3352
3353void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3354 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3355 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003356 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003357 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003358 auto* src = GetImageUsageState(srcImage);
3359 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003360
3361 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003362 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3363 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003364 }
3365}
3366
Attilio Provenzano02859b22020-02-27 14:17:28 +00003367bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3368 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3369 bool skip = false;
3370
3371 if (VendorCheckEnabled(kBPVendorArm)) {
3372 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3373 skip |= LogPerformanceWarning(
3374 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3375 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3376 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3377 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003378 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003379 }
3380
3381 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3382 skip |= LogPerformanceWarning(
3383 device, kVUID_BestPractices_CreateSampler_LodClamping,
3384 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3385 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3386 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3387 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3388 }
3389
3390 if (pCreateInfo->mipLodBias != 0.0f) {
3391 skip |=
3392 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3393 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3394 "descriptors being created and may cause reduced performance.",
3395 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3396 }
3397
3398 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3399 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3400 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3401 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3402 skip |= LogPerformanceWarning(
3403 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3404 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3405 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3406 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3407 VendorSpecificTag(kBPVendorArm));
3408 }
3409
3410 if (pCreateInfo->unnormalizedCoordinates) {
3411 skip |= LogPerformanceWarning(
3412 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3413 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3414 "descriptors being created and may cause reduced performance.",
3415 VendorSpecificTag(kBPVendorArm));
3416 }
3417
3418 if (pCreateInfo->anisotropyEnable) {
3419 skip |= LogPerformanceWarning(
3420 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3421 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3422 "and may cause reduced performance.",
3423 VendorSpecificTag(kBPVendorArm));
3424 }
3425 }
3426
3427 return skip;
3428}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003429
Nadav Gevaf0808442021-05-21 13:51:25 -04003430void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3431 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3432 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3433 void* cgpl_state) {
3434 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3435 pPipelines);
3436 // AMD best practice
3437 num_pso += createInfoCount;
3438}
3439
3440bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3441 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3442 const VkCopyDescriptorSet* pDescriptorCopies) const {
3443 bool skip = false;
3444 if (VendorCheckEnabled(kBPVendorAMD)) {
3445 if (descriptorCopyCount > 0) {
3446 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3447 "%s Performance warning: copying descriptor sets is not recommended",
3448 VendorSpecificTag(kBPVendorAMD));
3449 }
3450 }
3451
3452 return skip;
3453}
3454
3455bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3456 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3457 const VkAllocationCallbacks* pAllocator,
3458 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3459 bool skip = false;
3460 if (VendorCheckEnabled(kBPVendorAMD)) {
3461 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3462 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3463 "vkUpdateDescriptorSet instead",
3464 VendorSpecificTag(kBPVendorAMD));
3465 }
3466
3467 return skip;
3468}
3469
3470bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3471 const VkClearColorValue* pColor, uint32_t rangeCount,
3472 const VkImageSubresourceRange* pRanges) const {
3473 bool skip = false;
3474 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003475 skip |= LogPerformanceWarning(
3476 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003477 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3478 "vkCmdClearAttachments instead",
3479 VendorSpecificTag(kBPVendorAMD));
3480 }
3481
3482 return skip;
3483}
3484
3485bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3486 VkImageLayout imageLayout,
3487 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3488 const VkImageSubresourceRange* pRanges) const {
3489 bool skip = false;
3490 if (VendorCheckEnabled(kBPVendorAMD)) {
3491 skip |= LogPerformanceWarning(
3492 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3493 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3494 "vkCmdClearAttachments instead",
3495 VendorSpecificTag(kBPVendorAMD));
3496 }
3497
3498 return skip;
3499}
3500
3501bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3502 const VkAllocationCallbacks* pAllocator,
3503 VkPipelineLayout* pPipelineLayout) const {
3504 bool skip = false;
3505 if (VendorCheckEnabled(kBPVendorAMD)) {
3506 // Descriptor sets cost 1 DWORD each.
3507 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3508 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3509 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3510 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3511 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003512 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Nadav Gevaf0808442021-05-21 13:51:25 -04003513 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * (robust_buffer_access ? 4 : 2);
3514 }
3515
3516 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3517 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3518 }
3519
3520 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3521 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3522 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3523 "Descriptor sets cost 1 DWORD each. "
3524 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3525 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3526 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3527 VendorSpecificTag(kBPVendorAMD));
3528 }
3529 }
3530
3531 return skip;
3532}
3533
3534bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3535 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3536 const VkImageCopy* pRegions) const {
3537 bool skip = false;
3538 std::stringstream src_image_hex;
3539 std::stringstream dst_image_hex;
3540 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3541 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3542
3543 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003544 auto src_state = Get<IMAGE_STATE>(srcImage);
3545 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003546
3547 if (src_state && dst_state) {
3548 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3549 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3550 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3551 skip |=
3552 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3553 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3554 "image (vkCmdCopyImageToBuffer) "
3555 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3556 "copies when converting between linear and optimal images",
3557 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3558 }
3559 }
3560 }
3561
3562 return skip;
3563}
3564
3565bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3566 VkPipeline pipeline) const {
3567 bool skip = false;
3568
3569 if (VendorCheckEnabled(kBPVendorAMD)) {
3570 if (pipelines_used_in_frame.find(pipeline) != pipelines_used_in_frame.end()) {
3571 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3572 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3573 "for example, by sorting draw calls by pipeline.",
3574 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3575 }
3576 }
3577
3578 return skip;
3579}
3580
3581void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3582 VkFence fence, VkResult result) {
3583 // AMD best practice
3584 num_queue_submissions += submitCount;
3585}
3586
3587bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3588 bool skip = false;
3589
3590 if (VendorCheckEnabled(kBPVendorAMD)) {
3591 if (num_queue_submissions > kNumberOfSubmissionWarningLimitAMD) {
3592 skip |= LogPerformanceWarning(
3593 device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3594 "%s Performance warning: command buffers submitted %" PRId32 " times this frame. Submitting command buffers has a CPU "
3595 "and GPU overhead. Submit fewer times to incur less overhead.",
3596 VendorSpecificTag(kBPVendorAMD), num_queue_submissions);
3597 }
3598 }
3599
3600 return skip;
3601}
3602
3603void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3604 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3605 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3606 uint32_t bufferMemoryBarrierCount,
3607 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3608 uint32_t imageMemoryBarrierCount,
3609 const VkImageMemoryBarrier* pImageMemoryBarriers) {
3610 num_barriers_objects += memoryBarrierCount;
3611 num_barriers_objects += imageMemoryBarrierCount;
3612 num_barriers_objects += bufferMemoryBarrierCount;
3613}
3614
3615void BestPractices::ManualPostCallRecordCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3616 const VkAllocationCallbacks* pAllocator, VkFence* pFence, VkResult result) {
3617 // AMD best practice
3618 if (result == VK_SUCCESS) {
3619 num_fence_objects++;
3620 }
3621}
3622
3623void BestPractices::ManualPostCallRecordCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3624 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore,
3625 VkResult result) {
3626 // AMD best practice
3627 if (result == VK_SUCCESS) {
3628 num_semaphore_objects++;
3629 }
3630}
3631
3632bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3633 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3634 bool skip = false;
3635 if (VendorCheckEnabled(kBPVendorAMD)) {
3636 if (num_semaphore_objects > kMaxRecommendedSemaphoreObjectsSizeAMD) {
3637 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3638 "%s Performance warning: High number of vkSemaphore objects created."
3639 "Minimize the amount of queue synchronization that is used. "
3640 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3641 VendorSpecificTag(kBPVendorAMD));
3642 }
3643 }
3644
3645 return skip;
3646}
3647
3648bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3649 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3650 bool skip = false;
3651 if (VendorCheckEnabled(kBPVendorAMD)) {
3652 if (num_fence_objects > kMaxRecommendedFenceObjectsSizeAMD) {
3653 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3654 "%s Performance warning: High number of VkFence objects created."
3655 "Minimize the amount of CPU-GPU synchronization that is used. "
3656 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3657 VendorSpecificTag(kBPVendorAMD));
3658 }
3659 }
3660
3661 return skip;
3662}
3663
Sam Walls8e77e4f2020-03-16 20:47:40 +00003664void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3665
3666bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3667 // look for a cache hit
3668 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3669 if (hit != _entries.end()) {
3670 // mark the cache hit as being most recently used
3671 hit->age = iteration++;
3672 return true;
3673 }
3674
3675 // if there's no cache hit, we need to model the entry being inserted into the cache
3676 CacheEntry new_entry = {value, iteration};
3677 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3678 // if there is still space left in the cache, use the next available slot
3679 *(_entries.begin() + iteration) = new_entry;
3680 } else {
3681 // otherwise replace the least recently used cache entry
3682 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3683 *lru = new_entry;
3684 }
3685 iteration++;
3686 return false;
3687}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003688
3689bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3690 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003691 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003692 bool skip = false;
3693 if (swapchain_data && swapchain_data->images.size() == 0) {
3694 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3695 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3696 "vkGetSwapchainImagesKHR after swapchain creation.");
3697 }
3698 return skip;
3699}
3700
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003701void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3702 if (no_pointer) {
3703 if (UNCALLED == call_state) {
3704 call_state = QUERY_COUNT;
3705 }
3706 } else { // Save queue family properties
3707 call_state = QUERY_DETAILS;
3708 }
3709}
3710
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003711void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3712 uint32_t* pQueueFamilyPropertyCount,
3713 VkQueueFamilyProperties* pQueueFamilyProperties) {
3714 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3715 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003716 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003717 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003718 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3719 nullptr == pQueueFamilyProperties);
3720 }
3721}
3722
3723void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3724 uint32_t* pQueueFamilyPropertyCount,
3725 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3726 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3727 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003728 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003729 if (bp_pd_state) {
3730 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3731 nullptr == pQueueFamilyProperties);
3732 }
3733}
3734
3735void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3736 uint32_t* pQueueFamilyPropertyCount,
3737 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3738 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3739 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003740 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003741 if (bp_pd_state) {
3742 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3743 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003744 }
3745}
3746
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003747void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3748 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003749 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003750 if (bp_pd_state) {
3751 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3752 }
3753}
3754
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003755void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3756 VkPhysicalDeviceFeatures2* pFeatures) {
3757 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003758 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003759 if (bp_pd_state) {
3760 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3761 }
3762}
3763
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003764void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3765 VkPhysicalDeviceFeatures2* pFeatures) {
3766 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003767 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003768 if (bp_pd_state) {
3769 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3770 }
3771}
3772
3773void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3774 VkSurfaceKHR surface,
3775 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3776 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003777 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003778 if (bp_pd_state) {
3779 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3780 }
3781}
3782
3783void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3784 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3785 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003786 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003787 if (bp_pd_state) {
3788 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3789 }
3790}
3791
3792void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3793 VkSurfaceKHR surface,
3794 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3795 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003796 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003797 if (bp_pd_state) {
3798 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3799 }
3800}
3801
3802void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3803 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3804 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003805 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003806 if (bp_pd_data) {
3807 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3808
3809 if (*pPresentModeCount) {
3810 if (call_state < QUERY_COUNT) {
3811 call_state = QUERY_COUNT;
3812 }
3813 }
3814 if (pPresentModes) {
3815 if (call_state < QUERY_DETAILS) {
3816 call_state = QUERY_DETAILS;
3817 }
3818 }
3819 }
3820}
3821
3822void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3823 uint32_t* pSurfaceFormatCount,
3824 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003825 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003826 if (bp_pd_data) {
3827 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3828
3829 if (*pSurfaceFormatCount) {
3830 if (call_state < QUERY_COUNT) {
3831 call_state = QUERY_COUNT;
3832 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003833 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003834 }
3835 if (pSurfaceFormats) {
3836 if (call_state < QUERY_DETAILS) {
3837 call_state = QUERY_DETAILS;
3838 }
3839 }
3840 }
3841}
3842
3843void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3844 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3845 uint32_t* pSurfaceFormatCount,
3846 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003847 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003848 if (bp_pd_data) {
3849 if (*pSurfaceFormatCount) {
3850 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3851 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3852 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003853 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003854 }
3855 if (pSurfaceFormats) {
3856 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3857 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3858 }
3859 }
3860 }
3861}
3862
3863void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3864 uint32_t* pPropertyCount,
3865 VkDisplayPlanePropertiesKHR* pProperties,
3866 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003867 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003868 if (bp_pd_data) {
3869 if (*pPropertyCount) {
3870 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3871 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3872 }
3873 }
3874 if (pProperties) {
3875 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3876 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3877 }
3878 }
3879 }
3880}
3881
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003882void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3883 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3884 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003885 auto swapchain_state = std::static_pointer_cast<SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003886 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3887 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3888 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003889 }
3890 }
3891}
3892
Nadav Gevaf0808442021-05-21 13:51:25 -04003893void BestPractices::ManualPostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
3894 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003895 if (VK_SUCCESS == result) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003896 if ((pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
3897 robust_buffer_access = true;
3898 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003899 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003900}
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003901
3902void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3903 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3904
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003905 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003906 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003907 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003908 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003909 auto cb = GetCBState(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003910 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003911 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003912 }
3913 }
3914 }
3915}