blob: 9a828ff7bb1e9e3744f064d5bcb7a97340ea05fd [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];
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001052 auto rp = Get<RENDER_PASS_STATE>(create_info.renderPass);
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001053
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001054 if (create_info.pColorBlendState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001055 // pColorBlendState is ignored if the pipeline has rasterization disabled or if no color attachments are used
1056 bool uses_color_attachments = false;
1057 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1058 uses_color_attachments |= rp->UsesColorAttachment(j);
1059 }
1060 if (uses_color_attachments && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1061 cis.colorBlendStateCI.emplace(create_info.pColorBlendState);
1062 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001063 }
1064
1065 if (create_info.pDepthStencilState) {
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001066 // pDepthStencilState is ignored if the pipeline has rasterization disabled or if no depth/stencil attachment is used
1067 bool uses_depth_stencil = false;
1068 for (uint32_t j = 0; j < rp->createInfo.subpassCount; j++) {
1069 uses_depth_stencil |= rp->UsesDepthStencilAttachment(j);
1070 }
1071 if (uses_depth_stencil && !create_info.pRasterizationState->rasterizerDiscardEnable) {
1072 cis.depthStencilStateCI.emplace(create_info.pDepthStencilState);
1073 }
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001074 }
Tony-LunarGf9b894b2022-02-08 15:01:37 -07001075
Jeremy Gebben81b5b792021-11-11 14:54:53 -07001076 if (create_info.renderPass == VK_NULL_HANDLE) {
1077 // TODO: this is necessary to avoid crashing
1078 LogWarning(device, kVUID_BestPractices_DynamicRendering_NotSupported,
1079 "vkCreateGraphicsPipelines: pCreateInfos[%" PRIu32 "].renderPass is VK_NULL_HANDLE, VK_KHR_dynamic_rendering is not supported.\n",
1080 static_cast<uint32_t>(i));
1081 continue;
1082 }
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001083 // 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 +02001084 auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1085 cis.accessFramebufferAttachments.clear();
1086
1087 if (cis.colorBlendStateCI) {
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001088 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1089 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, cis.colorBlendStateCI->attachmentCount);
1090 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +02001091 if (cis.colorBlendStateCI->pAttachments[j].colorWriteMask != 0) {
1092 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1093 if (attachment != VK_ATTACHMENT_UNUSED) {
1094 cis.accessFramebufferAttachments.push_back({ attachment, VK_IMAGE_ASPECT_COLOR_BIT });
1095 }
1096 }
1097 }
1098 }
1099
1100 if (cis.depthStencilStateCI && (cis.depthStencilStateCI->depthTestEnable ||
1101 cis.depthStencilStateCI->depthBoundsTestEnable ||
1102 cis.depthStencilStateCI->stencilTestEnable)) {
1103 uint32_t attachment = subpass.pDepthStencilAttachment ?
1104 subpass.pDepthStencilAttachment->attachment :
1105 VK_ATTACHMENT_UNUSED;
1106 if (attachment != VK_ATTACHMENT_UNUSED) {
1107 VkImageAspectFlags aspects = 0;
1108 if (cis.depthStencilStateCI->depthTestEnable || cis.depthStencilStateCI->depthBoundsTestEnable) {
1109 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1110 }
1111 if (cis.depthStencilStateCI->stencilTestEnable) {
1112 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1113 }
1114 cis.accessFramebufferAttachments.push_back({ attachment, aspects });
1115 }
1116 }
Sam Walls0961ec02020-03-31 16:39:15 +01001117 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001118
1119 // AMD best practice
1120 pipeline_cache = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001121}
1122
Camden5b184be2019-08-13 07:50:19 -06001123bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1124 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001125 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001126 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001127 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1128 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001129
1130 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001131 skip |= LogPerformanceWarning(
1132 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1133 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1134 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001135 }
1136
Nadav Gevaf0808442021-05-21 13:51:25 -04001137 if (VendorCheckEnabled(kBPVendorAMD)) {
1138 if (pipelineCache && pipeline_cache && pipelineCache != pipeline_cache) {
1139 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1140 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
1141 "improve cache hit rate",
1142 VendorSpecificTag(kBPVendorAMD));
1143 }
1144 }
1145
sfricke-samsung86d055a2022-02-11 14:43:50 -08001146 for (uint32_t i = 0; i < createInfoCount; i++) {
1147 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1148 if (VendorCheckEnabled(kBPVendorArm)) {
1149 skip |= ValidateCreateComputePipelineArm(createInfo);
1150 }
1151
1152 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1153 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1154 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1155 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1156 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1157 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1158 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1159 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1160 i);
1161 }
1162 }
1163 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001164 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001165
1166 return skip;
1167}
1168
1169bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1170 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001171 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001172 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001173 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1174 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001175
1176 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001177 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001178
1179 uint32_t thread_count = x * y * z;
1180
1181 // Generate a priori warnings about work group sizes.
1182 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1183 skip |= LogPerformanceWarning(
1184 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1185 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1186 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1187 "groups with less than %u threads, especially when using barrier() or shared memory.",
1188 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1189 }
1190
1191 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1192 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1193 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1194 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1195 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1196 "%u, %u) is not aligned to %u "
1197 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1198 "leave threads idle on the shader "
1199 "core.",
1200 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1201 kThreadGroupDispatchCountAlignmentArm);
1202 }
1203
sfricke-samsungef15e482022-01-26 11:32:49 -08001204 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1205 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001206
1207 unsigned dimensions = 0;
1208 if (x > 1) dimensions++;
1209 if (y > 1) dimensions++;
1210 if (z > 1) dimensions++;
1211 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1212 dimensions = std::max(dimensions, 1u);
1213
1214 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1215 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1216 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1217 bool accesses_2d = false;
1218 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001219 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001220 if (dim < 0) continue;
1221 auto spvdim = spv::Dim(dim);
1222 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1223 }
1224
1225 if (accesses_2d && dimensions < 2) {
1226 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1227 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1228 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1229 "exhibiting poor spatial locality with respect to one or more shader resources.",
1230 VendorSpecificTag(kBPVendorArm), x, y, z);
1231 }
1232
Camden5b184be2019-08-13 07:50:19 -06001233 return skip;
1234}
1235
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001236bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001237 bool skip = false;
1238
1239 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001240 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1241 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001242 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001243 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1244 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001245 }
1246
1247 return skip;
1248}
1249
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001250bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1251 bool skip = false;
1252
1253 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1254 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1255 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1256 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1257 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1258 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1259 }
1260
1261 return skip;
1262}
1263
1264bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1265 bool skip = false;
1266 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1267
1268 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1269 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1270
1271 return skip;
1272}
1273
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001274void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001275 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1276 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1277 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1278 LogPerformanceWarning(
1279 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1280 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1281 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1282 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1283 "convenient opportunity.",
1284 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1285 }
1286 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001287
1288 // AMD best practice
1289 // end-of-frame cleanup
1290 num_queue_submissions = 0;
1291 num_barriers_objects = 0;
1292 pipelines_used_in_frame.clear();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001293}
1294
Jeff Bolz5c801d12019-10-09 10:38:45 -05001295bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1296 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001297 bool skip = false;
1298
1299 for (uint32_t submit = 0; submit < submitCount; submit++) {
1300 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1301 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1302 }
1303 }
1304
1305 return skip;
1306}
1307
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001308bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1309 VkFence fence) const {
1310 bool skip = false;
1311
1312 for (uint32_t submit = 0; submit < submitCount; submit++) {
1313 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1314 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1315 }
1316 }
1317
1318 return skip;
1319}
1320
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001321bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1322 VkFence fence) const {
1323 bool skip = false;
1324
1325 for (uint32_t submit = 0; submit < submitCount; submit++) {
1326 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1327 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1328 }
1329 }
1330
1331 return skip;
1332}
1333
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001334bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1335 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1336 bool skip = false;
1337
1338 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1339 skip |= LogPerformanceWarning(
1340 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1341 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1342 "pool instead.");
1343 }
1344
1345 return skip;
1346}
1347
1348bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1349 const VkCommandBufferBeginInfo* pBeginInfo) const {
1350 bool skip = false;
1351
1352 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1353 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1354 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1355 }
1356
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001357 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1358 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001359 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1360 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1361 VendorSpecificTag(kBPVendorArm));
1362 }
1363
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001364 return skip;
1365}
1366
Jeff Bolz5c801d12019-10-09 10:38:45 -05001367bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001368 bool skip = false;
1369
1370 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1371
1372 return skip;
1373}
1374
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001375bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1376 const VkDependencyInfoKHR* pDependencyInfo) const {
1377 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1378}
1379
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001380bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1381 const VkDependencyInfo* pDependencyInfo) const {
1382 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1383}
1384
Jeff Bolz5c801d12019-10-09 10:38:45 -05001385bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1386 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001387 bool skip = false;
1388
1389 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1390
1391 return skip;
1392}
1393
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001394bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1395 VkPipelineStageFlags2KHR stageMask) const {
1396 bool skip = false;
1397
1398 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1399
1400 return skip;
1401}
1402
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001403bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1404 VkPipelineStageFlags2 stageMask) const {
1405 bool skip = false;
1406
1407 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1408
1409 return skip;
1410}
1411
Camden5b184be2019-08-13 07:50:19 -06001412bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1413 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1414 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1415 uint32_t bufferMemoryBarrierCount,
1416 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1417 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001418 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001419 bool skip = false;
1420
1421 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1422 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1423
1424 return skip;
1425}
1426
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001427bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1428 const VkDependencyInfoKHR* pDependencyInfos) const {
1429 bool skip = false;
1430 for (uint32_t i = 0; i < eventCount; i++) {
1431 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1432 }
1433
1434 return skip;
1435}
1436
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001437bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1438 const VkDependencyInfo* pDependencyInfos) const {
1439 bool skip = false;
1440 for (uint32_t i = 0; i < eventCount; i++) {
1441 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1442 }
1443
1444 return skip;
1445}
1446
Camden5b184be2019-08-13 07:50:19 -06001447bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1448 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1449 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1450 uint32_t bufferMemoryBarrierCount,
1451 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1452 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001453 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001454 bool skip = false;
1455
1456 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1457 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1458
Nadav Gevaf0808442021-05-21 13:51:25 -04001459 if (VendorCheckEnabled(kBPVendorAMD)) {
1460 if (num_barriers_objects + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
1461 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
1462 "%s Performance warning: In this frame, %" PRIu32 " barriers were already submitted. Barriers have a high cost and can "
1463 "stall the GPU. "
1464 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1465 VendorSpecificTag(kBPVendorAMD), num_barriers_objects);
1466 }
1467
1468 std::array<VkImageLayout, 3> read_layouts = {
1469 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1470 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1471 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1472 };
1473
1474 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1475 // read to read barriers
1476 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1477 bool old_is_read_layout = found != read_layouts.end();
1478 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1479 bool new_is_read_layout = found != read_layouts.end();
1480 if (old_is_read_layout && new_is_read_layout) {
1481 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1482 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1483 "time you use it.",
1484 VendorSpecificTag(kBPVendorAMD));
1485 }
1486
1487 // general with no storage
1488 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1489 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1490 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1491 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1492 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1493 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1494 VendorSpecificTag(kBPVendorAMD));
1495 }
1496 }
1497 }
1498 }
1499
Camden5b184be2019-08-13 07:50:19 -06001500 return skip;
1501}
1502
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001503bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1504 const VkDependencyInfoKHR* pDependencyInfo) const {
1505 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1506}
1507
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001508bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1509 const VkDependencyInfo* pDependencyInfo) const {
1510 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1511}
1512
Camden5b184be2019-08-13 07:50:19 -06001513bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001514 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001515 bool skip = false;
1516
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001517 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1518
1519 return skip;
1520}
1521
1522bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1523 VkQueryPool queryPool, uint32_t query) const {
1524 bool skip = false;
1525
1526 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001527
1528 return skip;
1529}
1530
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001531bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1532 VkQueryPool queryPool, uint32_t query) const {
1533 bool skip = false;
1534
1535 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1536
1537 return skip;
1538}
1539
Sam Walls0961ec02020-03-31 16:39:15 +01001540void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1541 VkPipeline pipeline) {
1542 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1543
Nadav Gevaf0808442021-05-21 13:51:25 -04001544 // AMD best practice
1545 pipelines_used_in_frame.emplace(pipeline);
1546
Sam Walls0961ec02020-03-31 16:39:15 +01001547 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
1548 // check for depth/blend state tracking
1549 auto gp_cis = graphicsPipelineCIs.find(pipeline);
1550 if (gp_cis != graphicsPipelineCIs.end()) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001551 auto cb_node = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001552 assert(cb_node);
1553 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001554
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001555 render_pass_state.nextDrawTouchesAttachments = gp_cis->second.accessFramebufferAttachments;
1556 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001557
Jeremy Gebben396d06a2021-08-12 11:03:04 -06001558 const auto& blend_state = gp_cis->second.colorBlendStateCI;
1559 const auto& stencil_state = gp_cis->second.depthStencilStateCI;
Sam Walls0961ec02020-03-31 16:39:15 +01001560
1561 if (blend_state) {
1562 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001563 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001564 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1565 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001566 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001567 }
1568 }
1569 }
1570
1571 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001572 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001573
1574 if (stencil_state && stencil_state->depthTestEnable) {
1575 switch (stencil_state->depthCompareOp) {
1576 case VK_COMPARE_OP_EQUAL:
1577 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1578 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001579 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001580 break;
1581 default:
1582 break;
1583 }
1584 }
Sam Walls0961ec02020-03-31 16:39:15 +01001585 }
1586 }
1587}
1588
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001589static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1590 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1591 const auto& subpass_info = createInfo.pSubpasses[subpass];
1592 if (subpass_info.pResolveAttachments) {
1593 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1594 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1595 }
1596 }
1597 }
1598
1599 return false;
1600}
1601
Attilio Provenzano02859b22020-02-27 14:17:28 +00001602static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1603 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001604 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001605
1606 // If an attachment is ever used as a color attachment,
1607 // resolve attachment or depth stencil attachment,
1608 // it needs to exist on tile at some point.
1609
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001610 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1611 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001612 }
1613
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001614 if (subpass_info.pResolveAttachments) {
1615 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1616 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1617 }
1618 }
1619
1620 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001621 }
1622
1623 return false;
1624}
1625
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001626static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1627 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1628 return false;
1629 }
1630
1631 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001632 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001633
1634 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1635 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1636 return true;
1637 }
1638 }
1639 }
1640
1641 return false;
1642}
1643
Attilio Provenzano02859b22020-02-27 14:17:28 +00001644bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1645 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1646 bool skip = false;
1647
1648 if (!pRenderPassBegin) {
1649 return skip;
1650 }
1651
Gareth Webbdc6549a2021-06-16 03:52:24 +01001652 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1653 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1654 "This render pass has a zero-size render area. It cannot write to any attachments, "
1655 "and can only be used for side effects such as layout transitions.");
1656 }
1657
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001658 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001659 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001660 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001661 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001662 if (rpabi) {
1663 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1664 }
1665 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001666 // Check if any attachments have LOAD operation on them
1667 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001668 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001669
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001670 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001671 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001672 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001673 }
1674
1675 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001676 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001677 }
1678
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001679 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001680
1681 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001682 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1683 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001684 }
1685
1686 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001687 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1688 skip |= LogPerformanceWarning(
1689 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1690 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1691 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001692 "which will copy in total %u pixels (renderArea = "
1693 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001694 VendorSpecificTag(kBPVendorArm), att,
1695 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1696 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1697 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001698 }
1699 }
1700 }
1701
1702 return skip;
1703}
1704
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001705void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1706 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001707 if (view) {
Jeremy Gebbenb4d17012021-07-08 13:18:15 -06001708 QueueValidateImage(funcs, function_name, GetImageUsageState(view->create_info.image), usage,
1709 view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001710 }
1711}
1712
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001713void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1714 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001715 const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001716 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001717
1718 // If we're viewing a 3D slice, ignore base array layer.
1719 // The entire 3D subresource is accessed as one atomic unit.
1720 const uint32_t base_array_layer = image->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
1721
1722 const uint32_t max_layers = image->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001723 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
1724 const uint32_t max_levels = image->createInfo.mipLevels - subresource_range.baseMipLevel;
1725 const uint32_t mip_levels = std::min(image->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001726
1727 for (uint32_t layer = 0; layer < array_layers; layer++) {
1728 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001729 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1730 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001731 }
1732 }
1733}
1734
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001735void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1736 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001737 const VkImageSubresourceLayers& subresource_layers) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001738 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001739 const uint32_t max_layers = image->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
1740 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001741
1742 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001743 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001744 }
1745}
1746
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001747void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1748 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001749 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07001750 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker&, const QUEUE_STATE&,
1751 const CMD_BUFFER_STATE&) -> bool {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001752 ValidateImageInQueue(function_name, state, usage, array_layer, mip_level);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001753 return false;
1754 });
1755}
1756
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001757void BestPractices::ValidateImageInQueueArm(const char* function_name, IMAGE_STATE* image,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001758 IMAGE_SUBRESOURCE_USAGE_BP last_usage,
1759 IMAGE_SUBRESOURCE_USAGE_BP usage,
1760 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001761 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001762 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 -06001763 !image->IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001764 LogPerformanceWarning(
1765 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001766 "%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 +02001767 "image was used, it was written to with STORE_OP_STORE. "
1768 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1769 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001770 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001771 } 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 +02001772 LogPerformanceWarning(
1773 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001774 "%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 +02001775 "image was used, it was written to with vkCmdClear*Image(). "
1776 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1777 "tile-based architectures."
1778 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001779 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001780 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
1781 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE ||
1782 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1783 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE ||
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001784 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001785 const char *last_cmd = nullptr;
1786 const char *vuid = nullptr;
1787 const char *suggestion = nullptr;
1788
1789 switch (last_usage) {
1790 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1791 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1792 last_cmd = "vkCmdBlitImage";
1793 suggestion =
1794 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1795 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1796 "which avoids the memory roundtrip.";
1797 break;
1798 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1799 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1800 last_cmd = "vkCmdClear*Image";
1801 suggestion =
1802 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1803 "tile-based architectures. "
1804 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1805 break;
1806 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1807 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1808 last_cmd = "vkCmdCopy*Image";
1809 suggestion =
1810 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1811 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1812 "which avoids the memory roundtrip.";
1813 break;
1814 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1815 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1816 last_cmd = "vkCmdResolveImage";
1817 suggestion =
1818 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1819 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1820 "which avoids the memory roundtrip.";
1821 break;
1822 default:
1823 break;
1824 }
1825
1826 LogPerformanceWarning(
1827 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001828 "%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 +01001829 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001830 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001831 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001832}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001833
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001834void BestPractices::ValidateImageInQueue(const char* function_name, IMAGE_STATE_BP* state,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001835 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer,
1836 uint32_t mip_level) {
1837 IMAGE_STATE* image = state->image;
1838 IMAGE_SUBRESOURCE_USAGE_BP last_usage = state->usages[array_layer][mip_level];
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001839 state->usages[array_layer][mip_level] = usage;
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001840 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001841 ValidateImageInQueueArm(function_name, image, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001842 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001843}
1844
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001845void BestPractices::AddDeferredQueueOperations(CMD_BUFFER_STATE_BP* cb) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001846 cb->queue_submit_functions.insert(cb->queue_submit_functions.end(),
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001847 cb->queue_submit_functions_after_render_pass.begin(),
1848 cb->queue_submit_functions_after_render_pass.end());
1849 cb->queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001850}
1851
1852void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1853 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001854 auto cb_node = GetCBState(commandBuffer);
1855 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001856}
1857
1858void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1859 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001860 auto cb_node = GetCBState(commandBuffer);
1861 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001862}
1863
1864void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1865 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001866 auto cb_node = GetCBState(commandBuffer);
1867 AddDeferredQueueOperations(cb_node.get());
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001868}
1869
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001870void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1871 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001872 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001873 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001874 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1875}
1876
1877void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1878 const VkRenderPassBeginInfo* pRenderPassBegin,
1879 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1880 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1881 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1882}
1883
1884void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1885 const VkRenderPassBeginInfo* pRenderPassBegin,
1886 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1887 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1888 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1889}
1890
1891void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001892
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001893 if (!pRenderPassBegin) {
1894 return;
1895 }
1896
Jeremy Gebben9f537102021-10-05 16:37:12 -06001897 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001898
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001899 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001900 if (rp_state) {
1901 // Check load ops
1902 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001903 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001904
1905 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1906 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1907 continue;
1908 }
1909
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001910 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001911
1912 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1913 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001914 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001915 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1916 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001917 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001918 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001919 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001920 }
1921
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001922 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001923 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001924
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001925 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001926 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1927 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001928 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001929 }
1930 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001931 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001932 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001933
Jeremy Gebben9f537102021-10-05 16:37:12 -06001934 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001935 }
1936
1937 // Check store ops
1938 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001939 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001940
1941 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1942 continue;
1943 }
1944
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001945 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001946
1947 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1948 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001949 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001950 }
1951
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001952 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001953
Jeremy Gebben9f537102021-10-05 16:37:12 -06001954 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001955 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001956 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1957 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001958 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001959 }
1960 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001961 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001962 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001963
Jeremy Gebben9f537102021-10-05 16:37:12 -06001964 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001965 }
1966 }
1967}
1968
Attilio Provenzano02859b22020-02-27 14:17:28 +00001969bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1970 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001971 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1972 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001973 return skip;
1974}
1975
1976bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1977 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001978 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001979 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1980 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001981 return skip;
1982}
1983
1984bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001985 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001986 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1987 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001988 return skip;
1989}
1990
Sam Walls0961ec02020-03-31 16:39:15 +01001991void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1992 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001993 // Reset the renderpass state
Jeremy Gebben9f537102021-10-05 16:37:12 -06001994 auto cb = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001995 cb->hasDrawCmd = false;
1996 assert(cb);
1997 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02001998 render_pass_state.touchesAttachments.clear();
1999 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002000 render_pass_state.numDrawCallsDepthOnly = 0;
2001 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2002 render_pass_state.colorAttachment = false;
2003 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002004 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002005 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002006
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002007 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002008
2009 // track depth / color attachment usage within the renderpass
2010 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2011 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002012 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002013
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002014 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002015 }
2016}
2017
2018void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2019 VkSubpassContents contents) {
2020 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2021 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2022}
2023
2024void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2025 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2026 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2027 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2028}
2029
2030void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2031 const VkRenderPassBeginInfo* pRenderPassBegin,
2032 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2033 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2034 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2035}
2036
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002037// Generic function to handle validation for all CmdDraw* type functions
2038bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2039 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002040 const auto cb_state = GetCBState(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002041 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002042 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2043 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002044 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002045
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002046 // Verify vertex binding
2047 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
2048 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002049 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002050 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002051 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2052 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002053 }
2054 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002055
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002056 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002057 if (pipe) {
2058 const auto* rp_state = pipe->rp_state.get();
2059 if (rp_state) {
2060 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2061 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002062 const auto& create_info = pipe->create_info.graphics;
2063 const uint32_t depth_stencil_attachment =
2064 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2065 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2066 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002067 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2068 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2069 }
2070 }
2071 }
2072 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002073 }
2074 return skip;
2075}
2076
Sam Walls0961ec02020-03-31 16:39:15 +01002077void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002078 auto cb_node = GetCBState(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002079 assert(cb_node);
2080 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002081 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002082 RecordCmdDrawTypeArm(render_pass_state, draw_count, caller);
2083 }
2084
2085 if (render_pass_state.drawTouchAttachments) {
2086 for (auto& touch : render_pass_state.nextDrawTouchesAttachments) {
2087 RecordAttachmentAccess(render_pass_state, touch.framebufferAttachment, touch.aspects);
2088 }
2089 // No need to touch the same attachments over and over.
2090 render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002091 }
2092}
2093
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002094void BestPractices::RecordCmdDrawTypeArm(RenderPassState& render_pass_state, uint32_t draw_count, const char* caller) {
2095 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2096 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2097 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002098 }
2099}
2100
Camden5b184be2019-08-13 07:50:19 -06002101bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002102 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002103 bool skip = false;
2104
2105 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002106 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2107 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002108 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002109 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002110
2111 return skip;
2112}
2113
Sam Walls0961ec02020-03-31 16:39:15 +01002114void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2115 uint32_t firstVertex, uint32_t firstInstance) {
2116 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2117 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2118}
2119
Camden5b184be2019-08-13 07:50:19 -06002120bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002121 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002122 bool skip = false;
2123
2124 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002125 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2126 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002127 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002128 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2129
Attilio Provenzano02859b22020-02-27 14:17:28 +00002130 // Check if we reached the limit for small indexed draw calls.
2131 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben9f537102021-10-05 16:37:12 -06002132 const auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002133 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002134 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2135 VendorCheckEnabled(kBPVendorArm)) {
2136 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002137 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002138 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2139 "You can try batching drawcalls or instancing when applicable.",
2140 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2141 }
2142
Sam Walls8e77e4f2020-03-16 20:47:40 +00002143 if (VendorCheckEnabled(kBPVendorArm)) {
2144 ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2145 }
2146
2147 return skip;
2148}
2149
2150bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2151 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2152 bool skip = false;
2153
2154 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Jeremy Gebben9f537102021-10-05 16:37:12 -06002155 const auto cmd_state = GetCBState(commandBuffer);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002156 if (cmd_state == nullptr) return skip;
2157
locke-lunarg1ae57d62020-11-18 10:49:19 -07002158 const auto* ib_state = cmd_state->index_buffer_binding.buffer_state.get();
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002159 if (ib_state == nullptr || cmd_state->index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002160
2161 const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002162 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002163 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2164 const void* ib_mem = ib_mem_state.p_driver_data;
2165 bool primitive_restart_enable = false;
2166
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002167 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2168 const auto& pipeline_binding_iter = cmd_state->lastBound[lv_bind_point];
2169 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002170
Jeremy Gebben11af9792021-08-20 10:20:09 -06002171 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2172 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002173 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002174
2175 // 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 -06002176 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002177 uint32_t scan_stride;
2178 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2179 scan_stride = sizeof(uint8_t);
2180 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2181 scan_stride = sizeof(uint16_t);
2182 } else {
2183 scan_stride = sizeof(uint32_t);
2184 }
2185
2186 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2187 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2188
2189 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2190 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2191 // irrespective of whether or not they're part of the draw call.
2192
2193 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2194 uint32_t min_index = ~0u;
2195 // start with maximum as 0 and adjust to indices in the buffer
2196 uint32_t max_index = 0u;
2197
2198 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2199 // for the given index buffer
2200 uint32_t vertex_shade_count = 0;
2201
2202 PostTransformLRUCacheModel post_transform_cache;
2203
2204 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2205 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2206 // target architecture.
2207 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2208 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2209 post_transform_cache.resize(32);
2210
2211 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2212 uint32_t scan_index;
2213 uint32_t primitive_restart_value;
2214 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2215 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2216 primitive_restart_value = 0xFF;
2217 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2218 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2219 primitive_restart_value = 0xFFFF;
2220 } else {
2221 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2222 primitive_restart_value = 0xFFFFFFFF;
2223 }
2224
2225 max_index = std::max(max_index, scan_index);
2226 min_index = std::min(min_index, scan_index);
2227
2228 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2229 bool in_cache = post_transform_cache.query_cache(scan_index);
2230 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2231 if (!in_cache) vertex_shade_count++;
2232 }
2233 }
2234
2235 // 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 +01002236 // 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
2237 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002238
2239 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002240 skip |=
2241 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2242 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2243 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2244 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2245 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2246 VendorSpecificTag(kBPVendorArm),
2247 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002248 return skip;
2249 }
2250
2251 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2252 // 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 +01002253 const size_t refs_per_bucket = 64;
2254 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2255
2256 const uint32_t n_indices = max_index - min_index + 1;
2257 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2258 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2259
2260 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2261 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002262
2263 // To avoid using too much memory, we run over the indices again.
2264 // Knowing the size from the last scan allows us to record index usage with bitsets
2265 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2266 uint32_t scan_index;
2267 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2268 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2269 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2270 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2271 } else {
2272 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2273 }
2274 // keep track of the set of all indices used to reference vertices in the draw call
2275 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002276 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2277 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002278 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2279 }
2280
2281 uint32_t vertex_reference_count = 0;
2282 for (const auto& bitset : vertex_reference_buckets) {
2283 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2284 }
2285
2286 // 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 -07002287 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002288 // 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 -07002289 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002290
2291 if (utilization < 0.5f) {
2292 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2293 "%s The indices which were specified for the draw call only utilise approximately "
2294 "%.02f%% of the bound vertex buffer.",
2295 VendorSpecificTag(kBPVendorArm), utilization);
2296 }
2297
2298 if (cache_hit_rate <= 0.5f) {
2299 skip |=
2300 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2301 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2302 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2303 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2304 "recently shaded vertices.",
2305 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2306 }
2307 }
2308
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002309 return skip;
2310}
2311
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002312bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2313 const VkCommandBuffer* pCommandBuffers) const {
2314 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002315 const auto primary = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002316 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002317 const auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002318 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002319 continue;
2320 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002321 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002322 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002323 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(clear.rects.size()), clear.rects.data())) {
2324 skip |= ValidateClearAttachment(commandBuffer, primary.get(), clear.framebufferAttachment, clear.colorAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002325 clear.aspects, true);
2326 }
2327 }
2328 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002329
2330 if (VendorCheckEnabled(kBPVendorAMD)) {
2331 if (commandBufferCount > 0) {
2332 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2333 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2334 VendorSpecificTag(kBPVendorAMD));
2335 }
2336 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002337 return skip;
2338}
2339
2340void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2341 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002342 auto primary = GetCBState(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002343 auto& primary_state = primary->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002344
2345 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002346 auto secondary_cb = GetCBState(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002347 if (secondary_cb == nullptr) {
2348 continue;
2349 }
2350 auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002351
2352 for (auto& early_clear : secondary.earlyClearAttachments) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002353 if (ClearAttachmentsIsFullClear(primary.get(), uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2354 RecordAttachmentClearAttachments(primary.get(), primary_state, early_clear.framebufferAttachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002355 early_clear.colorAttachment, early_clear.aspects,
Hans-Kristian Arntzenfa8ef9f2021-06-29 12:07:59 +02002356 uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002357 } else {
2358 RecordAttachmentAccess(primary_state, early_clear.framebufferAttachment,
2359 early_clear.aspects);
2360 }
2361 }
2362
2363 for (auto& touch : secondary.touchesAttachments) {
2364 RecordAttachmentAccess(primary_state, touch.framebufferAttachment,
2365 touch.aspects);
2366 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002367
2368 primary_state.numDrawCallsDepthEqualCompare += secondary.numDrawCallsDepthEqualCompare;
2369 primary_state.numDrawCallsDepthOnly += secondary.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002370
Jeremy Gebben9f537102021-10-05 16:37:12 -06002371 auto second_state = GetCBState(pCommandBuffers[i]);
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002372 if (second_state->hasDrawCmd) {
2373 primary->hasDrawCmd = true;
2374 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002375 }
2376
2377 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2378}
2379
2380void BestPractices::RecordAttachmentAccess(RenderPassState& state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2381 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
2382 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002383 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002384 return info.framebufferAttachment == fb_attachment;
2385 });
2386
2387 if (itr != state.touchesAttachments.end()) {
2388 itr->aspects |= aspects;
2389 } else {
2390 state.touchesAttachments.push_back({ fb_attachment, aspects });
2391 }
2392}
2393
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002394void BestPractices::RecordAttachmentClearAttachments(CMD_BUFFER_STATE_BP* cmd_state, RenderPassState& state, uint32_t fb_attachment,
2395 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2396 const VkClearRect* pRects) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002397 // If we observe a full clear before any other access to a frame buffer attachment,
2398 // we have candidate for redundant clear attachments.
2399 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002400 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002401 return info.framebufferAttachment == fb_attachment;
2402 });
2403
2404 uint32_t new_aspects = aspects;
2405 if (itr != state.touchesAttachments.end()) {
2406 new_aspects = aspects & ~itr->aspects;
2407 itr->aspects |= aspects;
2408 } else {
2409 state.touchesAttachments.push_back({ fb_attachment, aspects });
2410 }
2411
2412 if (new_aspects == 0) {
2413 return;
2414 }
2415
2416 if (cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2417 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2418 // CmdExecuteCommands.
2419 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2420 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2421 }
2422}
2423
2424void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2425 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2426 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002427 auto cmd_state = GetCBState(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002428 RENDER_PASS_STATE* rp_state = cmd_state->activeRenderPass.get();
2429 FRAMEBUFFER_STATE* fb_state = cmd_state->activeFramebuffer.get();
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002430 RenderPassState& tracking_state = cmd_state->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002431 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2432
2433 if (rectCount == 0 || !rp_state) {
2434 return;
2435 }
2436
2437 if (!is_secondary && !fb_state) {
2438 return;
2439 }
2440
2441 // 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 -06002442 bool full_clear = ClearAttachmentsIsFullClear(cmd_state.get(), rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002443
2444 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2445 for (uint32_t i = 0; i < attachmentCount; i++) {
2446 auto& attachment = pClearAttachments[i];
2447 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2448 VkImageAspectFlags aspects = attachment.aspectMask;
2449
2450 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2451 if (subpass.pDepthStencilAttachment) {
2452 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2453 }
2454 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2455 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
2456 }
2457
2458 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2459 if (full_clear) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002460 RecordAttachmentClearAttachments(cmd_state.get(), tracking_state, fb_attachment, attachment.colorAttachment,
2461 aspects, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002462 } else {
2463 RecordAttachmentAccess(tracking_state, fb_attachment, aspects);
2464 }
2465 }
2466 }
2467
2468 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments,
2469 rectCount, pRects);
2470}
2471
Attilio Provenzano02859b22020-02-27 14:17:28 +00002472void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2473 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2474 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2475 firstInstance);
2476
Jeremy Gebben9f537102021-10-05 16:37:12 -06002477 auto cmd_state = GetCBState(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002478 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2479 cmd_state->small_indexed_draw_call_count++;
2480 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002481
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002482 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002483}
2484
Sam Walls0961ec02020-03-31 16:39:15 +01002485void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2486 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2487 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2488 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2489}
2490
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002491bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2492 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2493 uint32_t maxDrawCount, uint32_t stride) const {
2494 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2495
2496 return skip;
2497}
2498
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002499bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2500 VkDeviceSize offset, VkBuffer countBuffer,
2501 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2502 uint32_t stride) const {
2503 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002504
2505 return skip;
2506}
2507
2508bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002509 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002510 bool skip = false;
2511
2512 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002513 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2514 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002515 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002516 }
2517
2518 return skip;
2519}
2520
Sam Walls0961ec02020-03-31 16:39:15 +01002521void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2522 uint32_t count, uint32_t stride) {
2523 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2524 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2525}
2526
Camden5b184be2019-08-13 07:50:19 -06002527bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002528 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002529 bool skip = false;
2530
2531 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002532 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2533 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002534 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002535 }
2536
2537 return skip;
2538}
2539
Sam Walls0961ec02020-03-31 16:39:15 +01002540void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2541 uint32_t count, uint32_t stride) {
2542 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2543 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2544}
2545
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002546void BestPractices::ValidateBoundDescriptorSets(VkCommandBuffer commandBuffer, const char* function_name) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002547 auto cb_state = GetCBState(commandBuffer);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002548
2549 if (cb_state) {
2550 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002551 const auto& layout = *descriptor_set->GetLayout();
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002552
2553 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2554 // For bindless scenarios, we should not attempt to track descriptor set state.
2555 // It is highly uncertain which resources are actually bound.
2556 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2557 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2558 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
2559 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2560 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2561 continue;
2562 }
2563
2564 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002565 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002566 VkImageView image_view{VK_NULL_HANDLE};
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002567
2568 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2569 switch (descriptor->GetClass()) {
2570 case cvdescriptorset::DescriptorClass::Image: {
2571 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2572 image_view = image_descriptor->GetImageView();
2573 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002574 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002575 }
2576 case cvdescriptorset::DescriptorClass::ImageSampler: {
2577 if (const auto image_sampler_descriptor =
2578 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2579 image_view = image_sampler_descriptor->GetImageView();
2580 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002581 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002582 }
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002583 default:
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002584 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002585 }
2586
2587 if (image_view) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002588 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002589 QueueValidateImageView(cb_state->queue_submit_functions, function_name, image_view_state.get(),
2590 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002591 }
2592 }
2593 }
2594 }
2595 }
2596}
2597
2598void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2599 uint32_t firstVertex, uint32_t firstInstance) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002600 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002601}
2602
2603void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2604 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002605 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002606}
2607
2608void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2609 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002610 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002611}
2612
Camden5b184be2019-08-13 07:50:19 -06002613bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002614 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002615 bool skip = false;
2616
2617 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002618 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2619 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2620 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2621 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002622 }
2623
2624 return skip;
2625}
Camden83a9c372019-08-14 11:41:38 -06002626
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002627bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2628 bool skip = false;
2629 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2630 skip |= ValidateCmdEndRenderPass(commandBuffer);
2631 return skip;
2632}
2633
2634bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2635 bool skip = false;
2636 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2637 skip |= ValidateCmdEndRenderPass(commandBuffer);
2638 return skip;
2639}
2640
Sam Walls0961ec02020-03-31 16:39:15 +01002641bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2642 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002643 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002644 skip |= ValidateCmdEndRenderPass(commandBuffer);
2645 return skip;
2646}
2647
2648bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2649 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002650 const auto cmd = GetCBState(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002651
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002652 if (cmd == nullptr) return skip;
2653 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002654
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002655 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2656 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2657 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002658 if (uses_depth) {
2659 skip |= LogPerformanceWarning(
2660 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2661 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2662 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2663 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2664 VendorSpecificTag(kBPVendorArm));
2665 }
2666
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002667 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2668
2669 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2670
2671 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2672 // it is redundant to have it be part of the render pass.
2673 // Only consider it redundant if it will actually consume bandwidth, i.e.
2674 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2675 // as is using pure input attachments.
2676 // CLEAR -> STORE might be considered a "useful" thing to do, but
2677 // the optimal thing to do is to defer the clear until you're actually
2678 // going to render to the image.
2679
2680 uint32_t num_attachments = rp->createInfo.attachmentCount;
2681 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002682 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2683 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002684 continue;
2685 }
2686
2687 auto& attachment = rp->createInfo.pAttachments[i];
2688
2689 VkImageAspectFlags bandwidth_aspects = 0;
2690
2691 if (!FormatIsStencilOnly(attachment.format) &&
2692 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2693 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2694 if (FormatHasDepth(attachment.format)) {
2695 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2696 } else {
2697 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2698 }
2699 }
2700
2701 if (FormatHasStencil(attachment.format) &&
2702 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2703 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2704 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2705 }
2706
2707 if (!bandwidth_aspects) {
2708 continue;
2709 }
2710
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002711 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
2712 [&](const AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002713 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002714 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002715 untouched_aspects &= ~itr->aspects;
2716 }
2717
2718 if (untouched_aspects) {
2719 skip |= LogPerformanceWarning(
2720 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2721 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2722 "was never accessed by a pipeline or clear command. "
2723 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2724 "if the attachments are not intended to be accessed.",
2725 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2726 }
2727 }
2728 }
2729
Sam Walls0961ec02020-03-31 16:39:15 +01002730 return skip;
2731}
2732
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002733void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002734 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002735}
2736
2737void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002738 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002739}
2740
Camden Stocker9c051442019-11-06 14:28:43 -08002741bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2742 const char* api_name) const {
2743 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002744 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002745
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002746 if (bp_pd_state) {
2747 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2748 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2749 "Potential problem with calling %s() without first retrieving properties from "
2750 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2751 api_name);
2752 }
Camden Stocker9c051442019-11-06 14:28:43 -08002753 }
2754
2755 return skip;
2756}
2757
Camden83a9c372019-08-14 11:41:38 -06002758bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002759 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002760 bool skip = false;
2761
Camden Stocker9c051442019-11-06 14:28:43 -08002762 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002763
Camden Stocker9c051442019-11-06 14:28:43 -08002764 return skip;
2765}
2766
2767bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2768 uint32_t planeIndex,
2769 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2770 bool skip = false;
2771
2772 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2773
2774 return skip;
2775}
2776
2777bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2778 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2779 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2780 bool skip = false;
2781
2782 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002783
2784 return skip;
2785}
Camden05de2d42019-08-19 10:23:56 -06002786
2787bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002788 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002789 bool skip = false;
2790
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002791 auto swapchain_state = std::static_pointer_cast<const SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Camden05de2d42019-08-19 10:23:56 -06002792
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002793 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002794 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002795 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002796 skip |=
2797 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2798 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2799 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002800 }
Camden05de2d42019-08-19 10:23:56 -06002801
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002802 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2803 skip |= LogWarning(
2804 device, kVUID_BestPractices_Swapchain_InvalidCount,
2805 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002806 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002807 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2808 }
2809 }
2810
Camden05de2d42019-08-19 10:23:56 -06002811 return skip;
2812}
2813
2814// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002815bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002816 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002817 const CALL_STATE call_state,
2818 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002819 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002820 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2821 if (UNCALLED == call_state) {
2822 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002823 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002824 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2825 "recommended "
2826 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2827 caller_name, caller_name);
2828 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002829 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2830 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002831 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2832 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2833 ". It is recommended to instead receive all the properties by calling %s with "
2834 "pQueueFamilyPropertyCount that was "
2835 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002836 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002837 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002838 }
2839
2840 return skip;
2841}
2842
Jeff Bolz5c801d12019-10-09 10:38:45 -05002843bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2844 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002845 bool skip = false;
2846
2847 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002848 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002849 if (!as_state->memory_requirements_checked) {
2850 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2851 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2852 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002853 skip |= LogWarning(
2854 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002855 "vkBindAccelerationStructureMemoryNV(): "
2856 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2857 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2858 }
2859 }
2860
2861 return skip;
2862}
2863
Camden05de2d42019-08-19 10:23:56 -06002864bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2865 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002866 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002867 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002868 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002869 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002870 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2871 "vkGetPhysicalDeviceQueueFamilyProperties()");
2872 }
2873 return false;
Camden05de2d42019-08-19 10:23:56 -06002874}
2875
Mike Schuchardt2df08912020-12-15 16:28:09 -08002876bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2877 uint32_t* pQueueFamilyPropertyCount,
2878 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002879 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002880 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002881 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002882 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2883 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2884 }
2885 return false;
Camden05de2d42019-08-19 10:23:56 -06002886}
2887
Jeff Bolz5c801d12019-10-09 10:38:45 -05002888bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002889 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002890 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002891 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002892 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002893 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2894 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2895 }
2896 return false;
Camden05de2d42019-08-19 10:23:56 -06002897}
2898
2899bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2900 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002901 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002902 if (!pSurfaceFormats) return false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06002903 const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002904 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002905 bool skip = false;
2906 if (call_state == UNCALLED) {
2907 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2908 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002909 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2910 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2911 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002912 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002913 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002914 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2915 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2916 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2917 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002918 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002919 }
2920 }
2921 return skip;
2922}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002923
2924bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002925 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002926 bool skip = false;
2927
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002928 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2929 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002930 // 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 -07002931 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002932 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2933 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002934 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002935 // 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 -07002936 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2937 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002938 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002939 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002940 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002941 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002942 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002943 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2944 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2945 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002946 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002947 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2948 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002949 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002950 }
2951 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002952 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002953 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002954 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002955 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2956 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002957 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002958 }
2959 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002960 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2961 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002962 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002963 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002964 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002965 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002966 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002967 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2968 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2969 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002970 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002971 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2972 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002973 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002974 }
2975 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002976 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002977 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002978 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002979 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2980 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002981 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002982 }
2983 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2984 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002985 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002986 }
2987 }
2988 }
2989 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002990 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2991 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002992 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002993 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002994 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2995 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002996 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002997 }
2998 }
2999 }
3000
3001 return skip;
3002}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003003
Mark Lobodzinski84101d72020-04-24 09:43:48 -06003004void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
3005 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003006 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07003007 return;
3008 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003009
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003010 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
3011 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
3012 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
3013 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04003014 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003015 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003016 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003017 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05003018 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
3019 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
3020 image_state->sparse_metadata_bound = true;
3021 }
3022 }
3023 }
3024 }
3025}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003026
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003027bool BestPractices::ClearAttachmentsIsFullClear(const CMD_BUFFER_STATE_BP* cmd, uint32_t rectCount,
3028 const VkClearRect* pRects) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003029 if (cmd->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3030 // We don't know the accurate render area in a secondary,
3031 // so assume we clear the entire frame buffer.
3032 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
3033 return true;
3034 }
3035
3036 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
3037 for (uint32_t i = 0; i < rectCount; i++) {
3038 auto& rect = pRects[i];
3039 auto& render_area = cmd->activeRenderPassBeginInfo.renderArea;
3040 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
3041 return true;
3042 }
3043 }
3044
3045 return false;
3046}
3047
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003048bool BestPractices::ValidateClearAttachment(VkCommandBuffer commandBuffer, const CMD_BUFFER_STATE_BP* cmd, uint32_t fb_attachment,
3049 uint32_t color_attachment, VkImageAspectFlags aspects, bool secondary) const {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003050 const RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3051 bool skip = false;
3052
3053 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3054 return skip;
3055 }
3056
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003057 const auto& rp_state = cmd->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003058
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003059 auto attachment_itr = std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02003060 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003061 return info.framebufferAttachment == fb_attachment;
3062 });
3063
3064 // Only report aspects which haven't been touched yet.
3065 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003066 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003067 new_aspects &= ~attachment_itr->aspects;
3068 }
3069
3070 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
3071 if (!cmd->hasDrawCmd) {
3072 skip |= LogPerformanceWarning(
3073 commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003074 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3075 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003076 report_data->FormatHandle(commandBuffer).c_str());
3077 }
3078
3079 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3080 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3081 skip |= LogPerformanceWarning(
3082 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3083 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3084 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3085 "it is more efficient.",
3086 secondary ? "vkCmdExecuteCommands(): " : "",
3087 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
3088 }
3089
3090 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3091 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3092 skip |= LogPerformanceWarning(
3093 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3094 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3095 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3096 "it is more efficient.",
3097 secondary ? "vkCmdExecuteCommands(): " : "",
3098 report_data->FormatHandle(commandBuffer).c_str());
3099 }
3100
3101 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3102 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3103 skip |= LogPerformanceWarning(
3104 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3105 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3106 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3107 "it is more efficient.",
3108 secondary ? "vkCmdExecuteCommands(): " : "",
3109 report_data->FormatHandle(commandBuffer).c_str());
3110 }
3111
3112 return skip;
3113}
3114
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003115bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003116 const VkClearAttachment* pAttachments, uint32_t rectCount,
3117 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003118 bool skip = false;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003119 const auto cb_node = GetCBState(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003120 if (!cb_node) return skip;
3121
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003122 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3123 // Defer checks to ExecuteCommands.
3124 return skip;
3125 }
3126
3127 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben9f537102021-10-05 16:37:12 -06003128 if (!ClearAttachmentsIsFullClear(cb_node.get(), rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003129 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003130 }
3131
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003132 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3133 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003134 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003135 if (rp) {
3136 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3137
3138 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003139 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003140
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003141 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3142 uint32_t color_attachment = attachment.colorAttachment;
3143 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003144 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, color_attachment,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003145 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003146 }
3147
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003148 if (subpass.pDepthStencilAttachment &&
3149 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003150 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben9f537102021-10-05 16:37:12 -06003151 skip |= ValidateClearAttachment(commandBuffer, cb_node.get(), fb_attachment, VK_ATTACHMENT_UNUSED,
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003152 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003153 }
3154 }
3155 }
3156
Nadav Gevaf0808442021-05-21 13:51:25 -04003157 if (VendorCheckEnabled(kBPVendorAMD)) {
3158 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3159 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3160 bool black_check = false;
3161 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3162 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3163 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3164 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3165 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3166
3167 bool white_check = false;
3168 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3169 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3170 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3171 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3172 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3173
3174 if (black_check && white_check) {
3175 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3176 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3177 "Consider changing to one of the following:"
3178 "RGBA(0, 0, 0, 0) "
3179 "RGBA(0, 0, 0, 1) "
3180 "RGBA(1, 1, 1, 0) "
3181 "RGBA(1, 1, 1, 1)",
3182 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3183 }
3184 } else {
3185 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3186 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3187 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3188 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3189 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3190 "attachment %" PRId32 " is not a fast clear value."
3191 "Consider changing to one of the following:"
3192 "D=0.0f, S=0"
3193 "D=1.0f, S=0",
3194 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3195 }
3196 }
3197 }
3198 }
3199
Camden Stockerf55721f2019-09-09 11:04:49 -06003200 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003201}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003202
3203bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3204 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3205 const VkImageResolve* pRegions) const {
3206 bool skip = false;
3207
3208 skip |= VendorCheckEnabled(kBPVendorArm) &&
3209 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3210 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3211 "This is a very slow and extremely bandwidth intensive path. "
3212 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3213 VendorSpecificTag(kBPVendorArm));
3214
3215 return skip;
3216}
3217
Jeff Leger178b1e52020-10-05 12:22:23 -04003218bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3219 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3220 bool skip = false;
3221
3222 skip |= VendorCheckEnabled(kBPVendorArm) &&
3223 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3224 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3225 "This is a very slow and extremely bandwidth intensive path. "
3226 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3227 VendorSpecificTag(kBPVendorArm));
3228
3229 return skip;
3230}
3231
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003232bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3233 const VkResolveImageInfo2* pResolveImageInfo) const {
3234 bool skip = false;
3235
3236 skip |= VendorCheckEnabled(kBPVendorArm) &&
3237 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3238 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3239 "This is a very slow and extremely bandwidth intensive path. "
3240 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3241 VendorSpecificTag(kBPVendorArm));
3242
3243 return skip;
3244}
3245
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003246void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3247 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3248 const VkImageResolve* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003249 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003250 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003251 auto* src = GetImageUsageState(srcImage);
3252 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003253
3254 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003255 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3256 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003257 }
3258}
3259
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003260void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3261 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003262 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003263 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003264 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3265 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003266 uint32_t regionCount = pResolveImageInfo->regionCount;
3267
3268 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003269 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3270 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003271 }
3272}
3273
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003274void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3275 const VkResolveImageInfo2* pResolveImageInfo) {
3276 auto cb = GetCBState(commandBuffer);
3277 auto& funcs = cb->queue_submit_functions;
3278 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
3279 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
3280 uint32_t regionCount = pResolveImageInfo->regionCount;
3281
3282 for (uint32_t i = 0; i < regionCount; i++) {
3283 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3284 pResolveImageInfo->pRegions[i].srcSubresource);
3285 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3286 pResolveImageInfo->pRegions[i].dstSubresource);
3287 }
3288}
3289
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003290void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3291 const VkClearColorValue* pColor, uint32_t rangeCount,
3292 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003293 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003294 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003295 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003296
3297 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003298 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003299 }
3300}
3301
3302void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3303 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3304 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003305 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003306 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003307 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003308
3309 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003310 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003311 }
3312}
3313
3314void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3315 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3316 const VkImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003317 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003318 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003319 auto* src = GetImageUsageState(srcImage);
3320 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003321
3322 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003323 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3324 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003325 }
3326}
3327
3328void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3329 VkImageLayout dstImageLayout, uint32_t regionCount,
3330 const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003331 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003332 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003333 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003334
3335 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003336 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003337 }
3338}
3339
3340void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3341 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003342 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003343 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003344 auto* src = GetImageUsageState(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003345
3346 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003347 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003348 }
3349}
3350
3351void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3352 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3353 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003354 auto cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003355 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01003356 auto* src = GetImageUsageState(srcImage);
3357 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003358
3359 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003360 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3361 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003362 }
3363}
3364
Attilio Provenzano02859b22020-02-27 14:17:28 +00003365bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3366 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3367 bool skip = false;
3368
3369 if (VendorCheckEnabled(kBPVendorArm)) {
3370 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3371 skip |= LogPerformanceWarning(
3372 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3373 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3374 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3375 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003376 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003377 }
3378
3379 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3380 skip |= LogPerformanceWarning(
3381 device, kVUID_BestPractices_CreateSampler_LodClamping,
3382 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3383 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3384 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3385 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3386 }
3387
3388 if (pCreateInfo->mipLodBias != 0.0f) {
3389 skip |=
3390 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3391 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3392 "descriptors being created and may cause reduced performance.",
3393 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3394 }
3395
3396 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3397 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3398 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3399 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3400 skip |= LogPerformanceWarning(
3401 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3402 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3403 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3404 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3405 VendorSpecificTag(kBPVendorArm));
3406 }
3407
3408 if (pCreateInfo->unnormalizedCoordinates) {
3409 skip |= LogPerformanceWarning(
3410 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3411 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3412 "descriptors being created and may cause reduced performance.",
3413 VendorSpecificTag(kBPVendorArm));
3414 }
3415
3416 if (pCreateInfo->anisotropyEnable) {
3417 skip |= LogPerformanceWarning(
3418 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3419 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3420 "and may cause reduced performance.",
3421 VendorSpecificTag(kBPVendorArm));
3422 }
3423 }
3424
3425 return skip;
3426}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003427
Nadav Gevaf0808442021-05-21 13:51:25 -04003428void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3429 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3430 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3431 void* cgpl_state) {
3432 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3433 pPipelines);
3434 // AMD best practice
3435 num_pso += createInfoCount;
3436}
3437
3438bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3439 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3440 const VkCopyDescriptorSet* pDescriptorCopies) const {
3441 bool skip = false;
3442 if (VendorCheckEnabled(kBPVendorAMD)) {
3443 if (descriptorCopyCount > 0) {
3444 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3445 "%s Performance warning: copying descriptor sets is not recommended",
3446 VendorSpecificTag(kBPVendorAMD));
3447 }
3448 }
3449
3450 return skip;
3451}
3452
3453bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3454 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3455 const VkAllocationCallbacks* pAllocator,
3456 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3457 bool skip = false;
3458 if (VendorCheckEnabled(kBPVendorAMD)) {
3459 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3460 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3461 "vkUpdateDescriptorSet instead",
3462 VendorSpecificTag(kBPVendorAMD));
3463 }
3464
3465 return skip;
3466}
3467
3468bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3469 const VkClearColorValue* pColor, uint32_t rangeCount,
3470 const VkImageSubresourceRange* pRanges) const {
3471 bool skip = false;
3472 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003473 skip |= LogPerformanceWarning(
3474 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003475 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3476 "vkCmdClearAttachments instead",
3477 VendorSpecificTag(kBPVendorAMD));
3478 }
3479
3480 return skip;
3481}
3482
3483bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3484 VkImageLayout imageLayout,
3485 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3486 const VkImageSubresourceRange* pRanges) const {
3487 bool skip = false;
3488 if (VendorCheckEnabled(kBPVendorAMD)) {
3489 skip |= LogPerformanceWarning(
3490 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3491 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3492 "vkCmdClearAttachments instead",
3493 VendorSpecificTag(kBPVendorAMD));
3494 }
3495
3496 return skip;
3497}
3498
3499bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3500 const VkAllocationCallbacks* pAllocator,
3501 VkPipelineLayout* pPipelineLayout) const {
3502 bool skip = false;
3503 if (VendorCheckEnabled(kBPVendorAMD)) {
3504 // Descriptor sets cost 1 DWORD each.
3505 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3506 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3507 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3508 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3509 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003510 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Nadav Gevaf0808442021-05-21 13:51:25 -04003511 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * (robust_buffer_access ? 4 : 2);
3512 }
3513
3514 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3515 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3516 }
3517
3518 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3519 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3520 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3521 "Descriptor sets cost 1 DWORD each. "
3522 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3523 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3524 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3525 VendorSpecificTag(kBPVendorAMD));
3526 }
3527 }
3528
3529 return skip;
3530}
3531
3532bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3533 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3534 const VkImageCopy* pRegions) const {
3535 bool skip = false;
3536 std::stringstream src_image_hex;
3537 std::stringstream dst_image_hex;
3538 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3539 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3540
3541 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003542 auto src_state = Get<IMAGE_STATE>(srcImage);
3543 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003544
3545 if (src_state && dst_state) {
3546 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3547 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3548 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3549 skip |=
3550 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3551 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3552 "image (vkCmdCopyImageToBuffer) "
3553 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3554 "copies when converting between linear and optimal images",
3555 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3556 }
3557 }
3558 }
3559
3560 return skip;
3561}
3562
3563bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3564 VkPipeline pipeline) const {
3565 bool skip = false;
3566
3567 if (VendorCheckEnabled(kBPVendorAMD)) {
3568 if (pipelines_used_in_frame.find(pipeline) != pipelines_used_in_frame.end()) {
3569 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3570 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3571 "for example, by sorting draw calls by pipeline.",
3572 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3573 }
3574 }
3575
3576 return skip;
3577}
3578
3579void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3580 VkFence fence, VkResult result) {
3581 // AMD best practice
3582 num_queue_submissions += submitCount;
3583}
3584
3585bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3586 bool skip = false;
3587
3588 if (VendorCheckEnabled(kBPVendorAMD)) {
3589 if (num_queue_submissions > kNumberOfSubmissionWarningLimitAMD) {
3590 skip |= LogPerformanceWarning(
3591 device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3592 "%s Performance warning: command buffers submitted %" PRId32 " times this frame. Submitting command buffers has a CPU "
3593 "and GPU overhead. Submit fewer times to incur less overhead.",
3594 VendorSpecificTag(kBPVendorAMD), num_queue_submissions);
3595 }
3596 }
3597
3598 return skip;
3599}
3600
3601void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3602 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3603 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3604 uint32_t bufferMemoryBarrierCount,
3605 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3606 uint32_t imageMemoryBarrierCount,
3607 const VkImageMemoryBarrier* pImageMemoryBarriers) {
3608 num_barriers_objects += memoryBarrierCount;
3609 num_barriers_objects += imageMemoryBarrierCount;
3610 num_barriers_objects += bufferMemoryBarrierCount;
3611}
3612
3613void BestPractices::ManualPostCallRecordCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3614 const VkAllocationCallbacks* pAllocator, VkFence* pFence, VkResult result) {
3615 // AMD best practice
3616 if (result == VK_SUCCESS) {
3617 num_fence_objects++;
3618 }
3619}
3620
3621void BestPractices::ManualPostCallRecordCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3622 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore,
3623 VkResult result) {
3624 // AMD best practice
3625 if (result == VK_SUCCESS) {
3626 num_semaphore_objects++;
3627 }
3628}
3629
3630bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3631 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3632 bool skip = false;
3633 if (VendorCheckEnabled(kBPVendorAMD)) {
3634 if (num_semaphore_objects > kMaxRecommendedSemaphoreObjectsSizeAMD) {
3635 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3636 "%s Performance warning: High number of vkSemaphore objects created."
3637 "Minimize the amount of queue synchronization that is used. "
3638 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3639 VendorSpecificTag(kBPVendorAMD));
3640 }
3641 }
3642
3643 return skip;
3644}
3645
3646bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3647 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3648 bool skip = false;
3649 if (VendorCheckEnabled(kBPVendorAMD)) {
3650 if (num_fence_objects > kMaxRecommendedFenceObjectsSizeAMD) {
3651 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3652 "%s Performance warning: High number of VkFence objects created."
3653 "Minimize the amount of CPU-GPU synchronization that is used. "
3654 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3655 VendorSpecificTag(kBPVendorAMD));
3656 }
3657 }
3658
3659 return skip;
3660}
3661
Sam Walls8e77e4f2020-03-16 20:47:40 +00003662void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3663
3664bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3665 // look for a cache hit
3666 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3667 if (hit != _entries.end()) {
3668 // mark the cache hit as being most recently used
3669 hit->age = iteration++;
3670 return true;
3671 }
3672
3673 // if there's no cache hit, we need to model the entry being inserted into the cache
3674 CacheEntry new_entry = {value, iteration};
3675 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3676 // if there is still space left in the cache, use the next available slot
3677 *(_entries.begin() + iteration) = new_entry;
3678 } else {
3679 // otherwise replace the least recently used cache entry
3680 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3681 *lru = new_entry;
3682 }
3683 iteration++;
3684 return false;
3685}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003686
3687bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3688 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003689 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003690 bool skip = false;
3691 if (swapchain_data && swapchain_data->images.size() == 0) {
3692 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3693 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3694 "vkGetSwapchainImagesKHR after swapchain creation.");
3695 }
3696 return skip;
3697}
3698
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003699void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3700 if (no_pointer) {
3701 if (UNCALLED == call_state) {
3702 call_state = QUERY_COUNT;
3703 }
3704 } else { // Save queue family properties
3705 call_state = QUERY_DETAILS;
3706 }
3707}
3708
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003709void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3710 uint32_t* pQueueFamilyPropertyCount,
3711 VkQueueFamilyProperties* pQueueFamilyProperties) {
3712 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3713 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003714 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003715 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003716 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3717 nullptr == pQueueFamilyProperties);
3718 }
3719}
3720
3721void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3722 uint32_t* pQueueFamilyPropertyCount,
3723 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3724 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3725 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003726 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003727 if (bp_pd_state) {
3728 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3729 nullptr == pQueueFamilyProperties);
3730 }
3731}
3732
3733void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3734 uint32_t* pQueueFamilyPropertyCount,
3735 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3736 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3737 pQueueFamilyProperties);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003738 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003739 if (bp_pd_state) {
3740 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3741 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003742 }
3743}
3744
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003745void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3746 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003747 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003748 if (bp_pd_state) {
3749 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3750 }
3751}
3752
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003753void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3754 VkPhysicalDeviceFeatures2* pFeatures) {
3755 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003756 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003757 if (bp_pd_state) {
3758 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3759 }
3760}
3761
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003762void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3763 VkPhysicalDeviceFeatures2* pFeatures) {
3764 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003765 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003766 if (bp_pd_state) {
3767 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3768 }
3769}
3770
3771void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3772 VkSurfaceKHR surface,
3773 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3774 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003775 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003776 if (bp_pd_state) {
3777 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3778 }
3779}
3780
3781void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3782 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3783 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003784 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003785 if (bp_pd_state) {
3786 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3787 }
3788}
3789
3790void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3791 VkSurfaceKHR surface,
3792 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3793 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003794 auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003795 if (bp_pd_state) {
3796 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3797 }
3798}
3799
3800void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3801 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3802 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003803 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003804 if (bp_pd_data) {
3805 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3806
3807 if (*pPresentModeCount) {
3808 if (call_state < QUERY_COUNT) {
3809 call_state = QUERY_COUNT;
3810 }
3811 }
3812 if (pPresentModes) {
3813 if (call_state < QUERY_DETAILS) {
3814 call_state = QUERY_DETAILS;
3815 }
3816 }
3817 }
3818}
3819
3820void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3821 uint32_t* pSurfaceFormatCount,
3822 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003823 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003824 if (bp_pd_data) {
3825 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3826
3827 if (*pSurfaceFormatCount) {
3828 if (call_state < QUERY_COUNT) {
3829 call_state = QUERY_COUNT;
3830 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003831 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003832 }
3833 if (pSurfaceFormats) {
3834 if (call_state < QUERY_DETAILS) {
3835 call_state = QUERY_DETAILS;
3836 }
3837 }
3838 }
3839}
3840
3841void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3842 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3843 uint32_t* pSurfaceFormatCount,
3844 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003845 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003846 if (bp_pd_data) {
3847 if (*pSurfaceFormatCount) {
3848 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3849 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3850 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003851 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003852 }
3853 if (pSurfaceFormats) {
3854 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3855 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3856 }
3857 }
3858 }
3859}
3860
3861void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3862 uint32_t* pPropertyCount,
3863 VkDisplayPlanePropertiesKHR* pProperties,
3864 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003865 auto bp_pd_data = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003866 if (bp_pd_data) {
3867 if (*pPropertyCount) {
3868 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3869 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3870 }
3871 }
3872 if (pProperties) {
3873 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3874 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3875 }
3876 }
3877 }
3878}
3879
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003880void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3881 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3882 VkResult result) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003883 auto swapchain_state = std::static_pointer_cast<SWAPCHAIN_STATE_BP>(Get<SWAPCHAIN_NODE>(swapchain));
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003884 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3885 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3886 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003887 }
3888 }
3889}
3890
Nadav Gevaf0808442021-05-21 13:51:25 -04003891void BestPractices::ManualPostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
3892 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003893 if (VK_SUCCESS == result) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003894 if ((pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
3895 robust_buffer_access = true;
3896 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003897 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003898}
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003899
3900void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3901 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3902
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003903 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003904 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003905 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003906 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003907 auto cb = GetCBState(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003908 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003909 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003910 }
3911 }
3912 }
3913}