blob: 9d7c001172260626c21e144af3c663d81060b5c8 [file] [log] [blame]
Nathaniel Cesario56a96652020-12-30 13:23:42 -07001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
Camdeneaa86ea2019-07-26 11:00:09 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: Camden Stocker <camden@lunarg.com>
18 */
19
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070020#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060021#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060022#include "best_practices_error_enums.h"
Sam Wallsd7ab6db2020-06-19 20:41:54 +010023#include "shader_validation.h"
Jeremy Gebbena3705f42021-01-19 16:47:43 -070024#include "sync_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060025#include "cmd_buffer_state.h"
26#include "device_state.h"
27#include "render_pass_state.h"
Camden5b184be2019-08-13 07:50:19 -060028
29#include <string>
Sam Walls8e77e4f2020-03-16 20:47:40 +000030#include <bitset>
Sam Wallsd7ab6db2020-06-19 20:41:54 +010031#include <memory>
Camden5b184be2019-08-13 07:50:19 -060032
Attilio Provenzano19d6a982020-02-27 12:41:41 +000033struct VendorSpecificInfo {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060034 EnableFlags vendor_id;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000035 std::string name;
36};
37
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070038const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060039 {kBPVendorArm, {vendor_specific_arm, "Arm"}},
Attilio Provenzano19d6a982020-02-27 12:41:41 +000040};
41
42bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070043 for (const auto& vendor : kVendorInfo) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060044 if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000045 return true;
46 }
47 }
48 return false;
49}
50
51const char* VendorSpecificTag(BPVendorFlags vendors) {
52 // Cache built vendor tags in a map
Jeremy Gebbencbf22862021-03-03 12:01:22 -070053 static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000054
55 auto res = tag_map.find(vendors);
56 if (res == tag_map.end()) {
57 // Build the vendor tag string
58 std::stringstream vendor_tag;
59
60 vendor_tag << "[";
61 bool first_vendor = true;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070062 for (const auto& vendor : kVendorInfo) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000063 if (vendors & vendor.first) {
64 if (!first_vendor) {
65 vendor_tag << ", ";
66 }
67 vendor_tag << vendor.second.name;
68 first_vendor = false;
69 }
70 }
71 vendor_tag << "]";
72
73 tag_map[vendors] = vendor_tag.str();
74 res = tag_map.find(vendors);
75 }
76
77 return res->second.c_str();
78}
79
Mark Lobodzinski6167e102020-02-24 17:03:55 -070080const char* DepReasonToString(ExtDeprecationReason reason) {
81 switch (reason) {
82 case kExtPromoted:
83 return "promoted to";
84 break;
85 case kExtObsoleted:
86 return "obsoleted by";
87 break;
88 case kExtDeprecated:
89 return "deprecated by";
90 break;
91 default:
92 return "";
93 break;
94 }
95}
96
97bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
98 const char* vuid) const {
99 bool skip = false;
100 auto dep_info_it = deprecated_extensions.find(extension_name);
101 if (dep_info_it != deprecated_extensions.end()) {
102 auto dep_info = dep_info_it->second;
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600103 if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
104 ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2))) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700105 skip |=
106 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
107 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600108 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700109 if (dep_info.target.length() == 0) {
110 skip |= LogWarning(instance, vuid,
111 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
112 "without replacement.",
113 api_name, extension_name);
114 } else {
115 skip |= LogWarning(instance, vuid,
116 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
117 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
118 }
119 }
120 }
121 return skip;
122}
123
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700124bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const char* vuid) const {
125 bool skip = false;
126 auto dep_info_it = special_use_extensions.find(extension_name);
127
128 if (dep_info_it != special_use_extensions.end()) {
129 auto special_uses = dep_info_it->second;
130 std::string message("is intended to support the following uses: ");
131 if (special_uses.find("cadsupport") != std::string::npos) {
132 message.append("specialized functionality used by CAD/CAM applications, ");
133 }
134 if (special_uses.find("d3demulation") != std::string::npos) {
135 message.append("D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D, ");
136 }
137 if (special_uses.find("devtools") != std::string::npos) {
138 message.append(" developer tools such as capture-replay libraries, ");
139 }
140 if (special_uses.find("debugging") != std::string::npos) {
141 message.append("use by applications when debugging, ");
142 }
143 if (special_uses.find("glemulation") != std::string::npos) {
144 message.append(
145 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
146 "specific to those APIs, ");
147 }
148 message.append("and it is strongly recommended that they be otherwise avoided");
149
150 skip |= LogWarning(instance, vuid, "%s(): Attempting to enable extension %s, but this extension %s.", api_name,
151 extension_name, message.c_str());
152 }
153 return skip;
154}
155
Camden5b184be2019-08-13 07:50:19 -0600156bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500157 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600158 bool skip = false;
159
160 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
161 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800162 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700163 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
164 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600165 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600166 uint32_t specified_version =
167 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
168 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700169 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Mark Lobodzinskia431b772020-11-10 08:12:13 -0700170 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i],
171 kVUID_BestPractices_CreateInstance_SpecialUseExtension);
Camden5b184be2019-08-13 07:50:19 -0600172 }
173
174 return skip;
175}
176
177void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
178 VkInstance* pInstance) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700179 ValidationStateTracker::PreCallRecordCreateInstance(pCreateInfo, pAllocator, pInstance);
Sam Walls53bf7652020-04-21 17:35:15 +0100180
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700181 if (pCreateInfo != nullptr && pCreateInfo->pApplicationInfo != nullptr) {
Sam Walls53bf7652020-04-21 17:35:15 +0100182 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700183 } else {
Sam Walls53bf7652020-04-21 17:35:15 +0100184 instance_api_version = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700185 }
Camden5b184be2019-08-13 07:50:19 -0600186}
187
188bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500189 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600190 bool skip = false;
191
192 // get API version of physical device passed when creating device.
193 VkPhysicalDeviceProperties physical_device_properties{};
194 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500195 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600196
197 // check api versions and warn if instance api Version is higher than version on device.
198 if (instance_api_version > device_api_version) {
Mark Lobodzinski60880782020-08-11 08:02:07 -0600199 std::string inst_api_name = StringAPIVersion(instance_api_version);
200 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600201
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700202 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
203 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
204 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600205 }
206
207 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
208 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800209 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700210 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
211 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600212 }
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700213 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], instance_api_version,
214 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Mark Lobodzinskia431b772020-11-10 08:12:13 -0700215 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i],
216 kVUID_BestPractices_CreateDevice_SpecialUseExtension);
Camden5b184be2019-08-13 07:50:19 -0600217 }
218
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600219 const auto bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
220 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700221 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
222 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600223 }
224
Szilard Papp7d2c7952020-06-22 14:38:13 +0100225 if ((VendorCheckEnabled(kBPVendorArm)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
226 (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
227 skip |= LogPerformanceWarning(
228 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
229 "%s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
230 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
231 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
232 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
233 VendorSpecificTag(kBPVendorArm));
234 }
235
Camden5b184be2019-08-13 07:50:19 -0600236 return skip;
237}
238
239bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500240 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600241 bool skip = false;
242
243 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700244 std::stringstream buffer_hex;
245 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600246
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700247 skip |= LogWarning(
248 device, kVUID_BestPractices_SharingModeExclusive,
249 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
250 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700251 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600252 }
253
254 return skip;
255}
256
257bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500258 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600259 bool skip = false;
260
261 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700262 std::stringstream image_hex;
263 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600264
265 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700266 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
267 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
268 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700269 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600270 }
271
Attilio Provenzano02859b22020-02-27 14:17:28 +0000272 if (VendorCheckEnabled(kBPVendorArm)) {
273 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
274 skip |= LogPerformanceWarning(
275 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
276 "%s vkCreateImage(): Trying to create an image with %u samples. "
277 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
278 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
279 }
280
281 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
282 skip |= LogPerformanceWarning(
283 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
284 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
285 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
286 "and do not need to be backed by physical storage. "
287 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
288 VendorSpecificTag(kBPVendorArm));
289 }
290 }
291
Camden5b184be2019-08-13 07:50:19 -0600292 return skip;
293}
294
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100295void BestPractices::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
296 ValidationStateTracker::PreCallRecordDestroyImage(device, image, pAllocator);
297 ReleaseImageUsageState(image);
298}
299
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200300void BestPractices::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
Tony-LunarG299187b2021-05-28 09:29:12 -0600301 if (VK_NULL_HANDLE != swapchain) {
302 SWAPCHAIN_NODE* chain = GetSwapchainState(swapchain);
303 for (auto& image : chain->images) {
304 if (image.image_state) {
305 ReleaseImageUsageState(image.image_state->image());
306 }
307 }
Hans-Kristian Arntzen92664eb2021-03-29 12:19:48 +0200308 }
309 ValidationStateTracker::PreCallRecordDestroySwapchainKHR(device, swapchain, pAllocator);
310}
311
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +0100312IMAGE_STATE_BP* BestPractices::GetImageUsageState(VkImage vk_image) {
313 auto itr = imageUsageMap.find(vk_image);
314 if (itr != imageUsageMap.end()) {
315 return &itr->second;
316 } else {
317 auto& state = imageUsageMap[vk_image];
318 IMAGE_STATE* image = GetImageState(vk_image);
319 state.image = image;
320 state.usages.resize(image->createInfo.arrayLayers);
321 for (auto& mips : state.usages) {
322 mips.resize(image->createInfo.mipLevels, IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED);
323 }
324 return &state;
325 }
326}
327
328void BestPractices::ReleaseImageUsageState(VkImage image) {
329 auto itr = imageUsageMap.find(image);
330 if (itr != imageUsageMap.end()) {
331 imageUsageMap.erase(itr);
332 }
333}
334
Camden5b184be2019-08-13 07:50:19 -0600335bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500336 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600337 bool skip = false;
338
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600339 const auto* bp_pd_state = GetPhysicalDeviceStateBP();
340 if (bp_pd_state) {
341 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
342 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
343 "vkCreateSwapchainKHR() called before getting surface capabilities from "
344 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
345 }
Camden83a9c372019-08-14 11:41:38 -0600346
Shannon McPherson73e58c82021-03-05 17:14:26 -0700347 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
348 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600349 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
350 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
351 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
352 }
Camden83a9c372019-08-14 11:41:38 -0600353
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600354 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
355 skip |= LogWarning(
356 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
357 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
358 }
Camden83a9c372019-08-14 11:41:38 -0600359 }
360
Camden5b184be2019-08-13 07:50:19 -0600361 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700362 skip |=
363 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600364 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700365 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
366 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600367 }
368
Szilard Papp48a6da32020-06-10 14:41:59 +0100369 if (pCreateInfo->minImageCount == 2) {
370 skip |= LogPerformanceWarning(
371 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
372 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
373 ", which means double buffering is going "
374 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
375 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
376 "3 to use triple buffering to maximize performance in such cases.",
377 pCreateInfo->minImageCount);
378 }
379
Szilard Pappd5f0f812020-06-22 09:01:29 +0100380 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
381 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
382 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
383 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
384 "Presentation modes which are not FIFO will present the latest available frame and discard other "
385 "frame(s) if any.",
386 VendorSpecificTag(kBPVendorArm));
387 }
388
Camden5b184be2019-08-13 07:50:19 -0600389 return skip;
390}
391
392bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
393 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500394 const VkAllocationCallbacks* pAllocator,
395 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600396 bool skip = false;
397
398 for (uint32_t i = 0; i < swapchainCount; i++) {
399 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700400 skip |= LogWarning(
401 device, kVUID_BestPractices_SharingModeExclusive,
402 "Warning: A shared swapchain (index %" PRIu32
403 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
404 "queues (queueFamilyIndexCount of %" PRIu32 ").",
405 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600406 }
407 }
408
409 return skip;
410}
411
412bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500413 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600414 bool skip = false;
415
416 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
417 VkFormat format = pCreateInfo->pAttachments[i].format;
418 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
419 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
420 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700421 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
422 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
423 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
424 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
425 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600426 }
427 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700428 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
429 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
430 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
431 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
432 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600433 }
434 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000435
436 const auto& attachment = pCreateInfo->pAttachments[i];
437 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
438 bool access_requires_memory =
439 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
440
441 if (FormatHasStencil(format)) {
442 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
443 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
444 }
445
446 if (access_requires_memory) {
447 skip |= LogPerformanceWarning(
448 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
449 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
450 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
451 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
452 i, static_cast<uint32_t>(attachment.samples));
453 }
454 }
Camden5b184be2019-08-13 07:50:19 -0600455 }
456
457 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
458 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
459 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
460 }
461
462 return skip;
463}
464
Tony-LunarG767180f2020-04-23 14:03:59 -0600465bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
466 const VkImageView* image_views) const {
467 bool skip = false;
468
469 // Check for non-transient attachments that should be transient and vice versa
470 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200471 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600472 bool attachment_should_be_transient =
473 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
474
475 if (FormatHasStencil(attachment.format)) {
476 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
477 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
478 }
479
480 auto view_state = GetImageViewState(image_views[i]);
481 if (view_state) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200482 const auto& ivci = view_state->create_info;
483 const auto& ici = GetImageState(ivci.image)->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600484
485 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
486
487 // The check for an image that should not be transient applies to all GPUs
488 if (!attachment_should_be_transient && image_is_transient) {
489 skip |= LogPerformanceWarning(
490 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
491 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
492 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
493 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
494 i);
495 }
496
497 bool supports_lazy = false;
498 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
499 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
500 supports_lazy = true;
501 }
502 }
503
504 // The check for an image that should be transient only applies to GPUs supporting
505 // lazily allocated memory
506 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
507 skip |= LogPerformanceWarning(
508 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
509 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
510 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
511 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
512 i);
513 }
514 }
515 }
516 return skip;
517}
518
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000519bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
520 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
521 bool skip = false;
522
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000523 auto rp_state = GetRenderPassState(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800524 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600525 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000526 }
527
528 return skip;
529}
530
Sam Wallse746d522020-03-16 21:20:23 +0000531bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
532 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
533 bool skip = false;
534 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
535
536 if (!skip) {
537 const auto& pool_handle = pAllocateInfo->descriptorPool;
538 auto iter = descriptor_pool_freed_count.find(pool_handle);
539 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
540 // this warning is specific to Arm
541 if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) {
542 skip |= LogPerformanceWarning(
543 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
544 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
545 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
546 VendorSpecificTag(kBPVendorArm));
547 }
548 }
549
550 return skip;
551}
552
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600553void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
554 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000555 if (result == VK_SUCCESS) {
556 // find the free count for the pool we allocated into
557 auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool);
558 if (iter != descriptor_pool_freed_count.end()) {
559 // we record successful allocations by subtracting the allocation count from the last recorded free count
560 const auto alloc_count = pAllocateInfo->descriptorSetCount;
561 // clamp the unsigned subtraction to the range [0, last_free_count]
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700562 if (iter->second > alloc_count) {
Sam Wallse746d522020-03-16 21:20:23 +0000563 iter->second -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700564 } else {
Sam Wallse746d522020-03-16 21:20:23 +0000565 iter->second = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700566 }
Sam Wallse746d522020-03-16 21:20:23 +0000567 }
568 }
569}
570
571void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
572 const VkDescriptorSet* pDescriptorSets, VkResult result) {
573 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
574 if (result == VK_SUCCESS) {
575 // we want to track frees because we're interested in suggesting re-use
576 auto iter = descriptor_pool_freed_count.find(descriptorPool);
577 if (iter == descriptor_pool_freed_count.end()) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700578 descriptor_pool_freed_count.emplace(descriptorPool, descriptorSetCount);
Sam Wallse746d522020-03-16 21:20:23 +0000579 } else {
580 iter->second += descriptorSetCount;
581 }
582 }
583}
584
Camden5b184be2019-08-13 07:50:19 -0600585bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500586 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600587 bool skip = false;
588
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500589 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700590 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
591 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600592 }
593
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000594 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
595 skip |= LogPerformanceWarning(
596 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600597 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
598 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000599 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
600 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
601 }
602
Camden83a9c372019-08-14 11:41:38 -0600603 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
604
605 return skip;
606}
607
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600608void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
609 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
610 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700611 if (result != VK_SUCCESS) {
612 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
613 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800614 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700615 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600616 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700617 return;
618 }
619 num_mem_objects++;
620}
Camden Stocker9738af92019-10-16 13:54:03 -0700621
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600622void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
623 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700624 auto error = std::find(error_codes.begin(), error_codes.end(), result);
625 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000626 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
627 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
628
629 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
630 if (common_failure != common_failure_codes.end()) {
631 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
632 } else {
633 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
634 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700635 return;
636 }
637 auto success = std::find(success_codes.begin(), success_codes.end(), result);
638 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600639 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
640 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500641 }
642}
643
Jeff Bolz5c801d12019-10-09 10:38:45 -0500644bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
645 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700646 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600647 bool skip = false;
648
Camden Stocker9738af92019-10-16 13:54:03 -0700649 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600650
Jeremy Gebben5570abe2021-05-16 18:35:13 -0600651 for (const auto& node: mem_info->ObjectBindings()) {
652 const auto& obj = node->Handle();
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600653 LogObjectList objlist(device);
654 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600655 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600656 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 -0600657 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600658 }
659
Camden5b184be2019-08-13 07:50:19 -0600660 return skip;
661}
662
663void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700664 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
Camden5b184be2019-08-13 07:50:19 -0600665 if (memory != VK_NULL_HANDLE) {
666 num_mem_objects--;
667 }
668}
669
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000670bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600671 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500672 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600673
sfricke-samsunge2441192019-11-06 14:07:57 -0800674 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700675 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
676 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
677 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600678 }
679
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000680 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
681
682 if (mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
683 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
684 skip |= LogPerformanceWarning(
685 device, kVUID_BestPractices_SmallDedicatedAllocation,
686 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600687 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
688 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000689 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
690 }
691
Camden Stockerb603cc82019-09-03 10:09:02 -0600692 return skip;
693}
694
695bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500696 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600697 bool skip = false;
698 const char* api_name = "BindBufferMemory()";
699
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000700 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600701
702 return skip;
703}
704
705bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500706 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600707 char api_name[64];
708 bool skip = false;
709
710 for (uint32_t i = 0; i < bindInfoCount; i++) {
711 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000712 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600713 }
714
715 return skip;
716}
Camden Stockerb603cc82019-09-03 10:09:02 -0600717
718bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500719 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600720 char api_name[64];
721 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600722
Camden Stocker8b798ab2019-09-03 10:33:28 -0600723 for (uint32_t i = 0; i < bindInfoCount; i++) {
724 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000725 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600726 }
727
728 return skip;
729}
730
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000731bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600732 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500733 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600734
sfricke-samsung71bc6572020-04-29 15:49:43 -0700735 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600736 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700737 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
738 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
739 api_name, report_data->FormatHandle(image).c_str());
740 }
741 } else {
742 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
743 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600744 }
745
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000746 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
747
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600748 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000749 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
750 skip |= LogPerformanceWarning(
751 device, kVUID_BestPractices_SmallDedicatedAllocation,
752 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600753 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
754 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000755 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
756 }
757
758 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
759 // make sure this type is actually used.
760 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
761 // (i.e.most tile - based renderers)
762 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
763 bool supports_lazy = false;
764 uint32_t suggested_type = 0;
765
766 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600767 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000768 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
769 supports_lazy = true;
770 suggested_type = i;
771 break;
772 }
773 }
774 }
775
776 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
777
778 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
779 skip |= LogPerformanceWarning(
780 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600781 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000782 "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 -0600783 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600784 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000785 }
786 }
787
Camden Stocker8b798ab2019-09-03 10:33:28 -0600788 return skip;
789}
790
791bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500792 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600793 bool skip = false;
794 const char* api_name = "vkBindImageMemory()";
795
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000796 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600797
798 return skip;
799}
800
801bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500802 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600803 char api_name[64];
804 bool skip = false;
805
806 for (uint32_t i = 0; i < bindInfoCount; i++) {
807 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700808 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600809 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
810 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600811 }
812
813 return skip;
814}
815
816bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500817 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600818 char api_name[64];
819 bool skip = false;
820
821 for (uint32_t i = 0; i < bindInfoCount; i++) {
822 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000823 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600824 }
825
826 return skip;
827}
Camden83a9c372019-08-14 11:41:38 -0600828
Attilio Provenzano02859b22020-02-27 14:17:28 +0000829static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
830 switch (format) {
831 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
832 case VK_FORMAT_R16_SFLOAT:
833 case VK_FORMAT_R16G16_SFLOAT:
834 case VK_FORMAT_R16G16B16_SFLOAT:
835 case VK_FORMAT_R16G16B16A16_SFLOAT:
836 case VK_FORMAT_R32_SFLOAT:
837 case VK_FORMAT_R32G32_SFLOAT:
838 case VK_FORMAT_R32G32B32_SFLOAT:
839 case VK_FORMAT_R32G32B32A32_SFLOAT:
840 return false;
841
842 default:
843 return true;
844 }
845}
846
847bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
848 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
849 bool skip = false;
850
851 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700852 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000853
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700854 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
855 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
856 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000857 return skip;
858 }
859
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700860 auto rp_state = GetRenderPassState(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200861 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000862
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700863 for (uint32_t j = 0; j < create_info->pColorBlendState->attachmentCount; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200864 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000865 uint32_t att = subpass.pColorAttachments[j].attachment;
866
867 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
868 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
869 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
870 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
871 "color attachment #%u makes use "
872 "of a format which cannot be blended at full throughput when using MSAA.",
873 VendorSpecificTag(kBPVendorArm), i, j);
874 }
875 }
876 }
877 }
878
879 return skip;
880}
881
Camden5b184be2019-08-13 07:50:19 -0600882bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
883 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600884 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500885 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600886 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
887 pAllocator, pPipelines, cgpl_state_data);
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600888 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600889
890 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700891 skip |= LogPerformanceWarning(
892 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
893 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
894 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600895 }
896
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000897 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200898 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000899
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600900 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200901 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600902 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700903 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
904 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600905 count++;
906 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000907 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600908 if (count > kMaxInstancedVertexBuffers) {
909 skip |= LogPerformanceWarning(
910 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
911 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
912 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
913 count, kMaxInstancedVertexBuffers);
914 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000915 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000916
Szilard Pappaaf2da32020-06-22 10:37:35 +0100917 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
918 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200919 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
920 VendorCheckEnabled(kBPVendorArm)) {
921 skip |= LogPerformanceWarning(
922 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
923 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
924 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
925 "efficiency during rasterization. Consider disabling depthBias or increasing either "
926 "depthBiasConstantFactor or depthBiasSlopeFactor.",
927 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +0100928 }
929
Attilio Provenzano02859b22020-02-27 14:17:28 +0000930 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000931 }
932
Camden5b184be2019-08-13 07:50:19 -0600933 return skip;
934}
935
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +0200936void BestPractices::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator)
937{
938 auto itr = graphicsPipelineCIs.find(pipeline);
939 if (itr != graphicsPipelineCIs.end()) {
940 graphicsPipelineCIs.erase(itr);
941 }
942 ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator);
943}
944
Sam Walls0961ec02020-03-31 16:39:15 +0100945void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
946 const VkGraphicsPipelineCreateInfo* pCreateInfos,
947 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
948 VkResult result, void* cgpl_state_data) {
949 for (size_t i = 0; i < count; i++) {
950 const auto* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
951 const VkPipeline pipeline_handle = pPipelines[i];
952
953 // record depth stencil state and color blend states for depth pre-pass tracking purposes
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +0200954 GraphicsPipelineCIs& cis = graphicsPipelineCIs[pipeline_handle];
Sam Walls0961ec02020-03-31 16:39:15 +0100955
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +0200956 auto& create_info = cgpl_state->pCreateInfos[i];
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +0200957
958 cis.colorBlendStateCI =
959 create_info.pColorBlendState
960 ? new safe_VkPipelineColorBlendStateCreateInfo(create_info.pColorBlendState)
Tony-LunarG412b1b72020-07-15 10:30:13 -0600961 : nullptr;
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +0200962 cis.depthStencilStateCI =
Tony-LunarG412b1b72020-07-15 10:30:13 -0600963 cgpl_state->pCreateInfos[i].pDepthStencilState
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +0200964 ? new safe_VkPipelineDepthStencilStateCreateInfo(create_info.pDepthStencilState)
Tony-LunarG412b1b72020-07-15 10:30:13 -0600965 : nullptr;
Hans-Kristian Arntzen42c0ef52021-06-14 14:46:25 +0200966
967 // Record which frame buffer attachments we should consider to be accessed when a draw call is performed.
968 RENDER_PASS_STATE* rp = GetRenderPassState(create_info.renderPass);
969 auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
970 cis.accessFramebufferAttachments.clear();
971
972 if (cis.colorBlendStateCI) {
973 for (uint32_t j = 0; j < cis.colorBlendStateCI->attachmentCount; j++) {
974 if (cis.colorBlendStateCI->pAttachments[j].colorWriteMask != 0) {
975 uint32_t attachment = subpass.pColorAttachments[j].attachment;
976 if (attachment != VK_ATTACHMENT_UNUSED) {
977 cis.accessFramebufferAttachments.push_back({ attachment, VK_IMAGE_ASPECT_COLOR_BIT });
978 }
979 }
980 }
981 }
982
983 if (cis.depthStencilStateCI && (cis.depthStencilStateCI->depthTestEnable ||
984 cis.depthStencilStateCI->depthBoundsTestEnable ||
985 cis.depthStencilStateCI->stencilTestEnable)) {
986 uint32_t attachment = subpass.pDepthStencilAttachment ?
987 subpass.pDepthStencilAttachment->attachment :
988 VK_ATTACHMENT_UNUSED;
989 if (attachment != VK_ATTACHMENT_UNUSED) {
990 VkImageAspectFlags aspects = 0;
991 if (cis.depthStencilStateCI->depthTestEnable || cis.depthStencilStateCI->depthBoundsTestEnable) {
992 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
993 }
994 if (cis.depthStencilStateCI->stencilTestEnable) {
995 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
996 }
997 cis.accessFramebufferAttachments.push_back({ attachment, aspects });
998 }
999 }
Sam Walls0961ec02020-03-31 16:39:15 +01001000 }
1001}
1002
Camden5b184be2019-08-13 07:50:19 -06001003bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1004 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001005 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001006 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001007 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1008 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001009
1010 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001011 skip |= LogPerformanceWarning(
1012 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1013 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1014 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001015 }
1016
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001017 if (VendorCheckEnabled(kBPVendorArm)) {
1018 for (size_t i = 0; i < createInfoCount; i++) {
1019 skip |= ValidateCreateComputePipelineArm(pCreateInfos[i]);
1020 }
1021 }
1022
1023 return skip;
1024}
1025
1026bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1027 bool skip = false;
1028 auto* module = GetShaderModuleState(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001029 // Generate warnings about work group sizes based on active resources.
sfricke-samsung962cad92021-04-13 00:46:29 -07001030 auto entrypoint = module->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001031 if (entrypoint == module->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001032
1033 uint32_t x = 1, y = 1, z = 1;
sfricke-samsung962cad92021-04-13 00:46:29 -07001034 module->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001035
1036 uint32_t thread_count = x * y * z;
1037
1038 // Generate a priori warnings about work group sizes.
1039 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1040 skip |= LogPerformanceWarning(
1041 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1042 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1043 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1044 "groups with less than %u threads, especially when using barrier() or shared memory.",
1045 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1046 }
1047
1048 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1049 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1050 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1051 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1052 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1053 "%u, %u) is not aligned to %u "
1054 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1055 "leave threads idle on the shader "
1056 "core.",
1057 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1058 kThreadGroupDispatchCountAlignmentArm);
1059 }
1060
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001061 bool has_writeable_descriptors = false;
locke-lunarg63e4daf2020-08-17 17:53:25 -06001062 bool has_atomic_descriptors = false;
sfricke-samsung962cad92021-04-13 00:46:29 -07001063 auto accessible_ids = module->MarkAccessibleIds(entrypoint);
locke-lunarg63e4daf2020-08-17 17:53:25 -06001064 auto descriptor_uses =
sfricke-samsung962cad92021-04-13 00:46:29 -07001065 module->CollectInterfaceByDescriptorSlot(accessible_ids, &has_writeable_descriptors, &has_atomic_descriptors);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001066
1067 unsigned dimensions = 0;
1068 if (x > 1) dimensions++;
1069 if (y > 1) dimensions++;
1070 if (z > 1) dimensions++;
1071 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1072 dimensions = std::max(dimensions, 1u);
1073
1074 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1075 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1076 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1077 bool accesses_2d = false;
1078 for (const auto& usage : descriptor_uses) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001079 auto dim = module->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001080 if (dim < 0) continue;
1081 auto spvdim = spv::Dim(dim);
1082 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1083 }
1084
1085 if (accesses_2d && dimensions < 2) {
1086 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1087 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1088 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1089 "exhibiting poor spatial locality with respect to one or more shader resources.",
1090 VendorSpecificTag(kBPVendorArm), x, y, z);
1091 }
1092
Camden5b184be2019-08-13 07:50:19 -06001093 return skip;
1094}
1095
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001096bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001097 bool skip = false;
1098
1099 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001100 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1101 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001102 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001103 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1104 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001105 }
1106
1107 return skip;
1108}
1109
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001110bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1111 bool skip = false;
1112
1113 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1114 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1115 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1116 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1117 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1118 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1119 }
1120
1121 return skip;
1122}
1123
1124bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1125 bool skip = false;
1126 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1127
1128 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1129 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1130
1131 return skip;
1132}
1133
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001134void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001135 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1136 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1137 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1138 LogPerformanceWarning(
1139 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1140 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1141 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1142 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1143 "convenient opportunity.",
1144 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1145 }
1146 }
1147}
1148
Jeff Bolz5c801d12019-10-09 10:38:45 -05001149bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1150 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001151 bool skip = false;
1152
1153 for (uint32_t submit = 0; submit < submitCount; submit++) {
1154 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1155 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1156 }
1157 }
1158
1159 return skip;
1160}
1161
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001162bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1163 VkFence fence) const {
1164 bool skip = false;
1165
1166 for (uint32_t submit = 0; submit < submitCount; submit++) {
1167 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1168 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1169 }
1170 }
1171
1172 return skip;
1173}
1174
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001175bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1176 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1177 bool skip = false;
1178
1179 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1180 skip |= LogPerformanceWarning(
1181 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1182 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1183 "pool instead.");
1184 }
1185
1186 return skip;
1187}
1188
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001189void BestPractices::PreCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo)
1190{
1191 // Clear state in case we are a secondary command buffer.
1192 auto& state = cbRenderPassState[commandBuffer];
1193 state = {};
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02001194 auto* cb = GetCBState(commandBuffer);
1195 cb->hasDrawCmd = false;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001196 ValidationStateTracker::PreCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo);
1197}
1198
1199void BestPractices::PreCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool,
1200 const VkAllocationCallbacks *pAllocation)
1201{
1202 COMMAND_POOL_STATE* pool = GetCommandPoolState(commandPool);
1203 if (pool) {
1204 for (auto& cb : pool->commandBuffers) {
1205 auto itr = cbRenderPassState.find(cb);
1206 if (itr != cbRenderPassState.end()) {
1207 cbRenderPassState.erase(itr);
1208 }
1209 }
1210 }
1211 ValidationStateTracker::PreCallRecordDestroyCommandPool(device, commandPool, pAllocation);
1212}
1213
1214void BestPractices::PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool,
1215 uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers) {
1216 for (uint32_t i = 0; i < commandBufferCount; i++) {
1217 auto itr = cbRenderPassState.find(pCommandBuffers[i]);
1218 if (itr != cbRenderPassState.end()) {
1219 cbRenderPassState.erase(itr);
1220 }
1221 }
1222 ValidationStateTracker::PreCallRecordFreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
1223}
1224
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001225bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1226 const VkCommandBufferBeginInfo* pBeginInfo) const {
1227 bool skip = false;
1228
1229 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1230 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1231 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1232 }
1233
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001234 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1235 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001236 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1237 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1238 VendorSpecificTag(kBPVendorArm));
1239 }
1240
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001241 return skip;
1242}
1243
Jeff Bolz5c801d12019-10-09 10:38:45 -05001244bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001245 bool skip = false;
1246
1247 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1248
1249 return skip;
1250}
1251
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001252bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1253 const VkDependencyInfoKHR* pDependencyInfo) const {
1254 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1255}
1256
Jeff Bolz5c801d12019-10-09 10:38:45 -05001257bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1258 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001259 bool skip = false;
1260
1261 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1262
1263 return skip;
1264}
1265
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001266bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1267 VkPipelineStageFlags2KHR stageMask) const {
1268 bool skip = false;
1269
1270 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1271
1272 return skip;
1273}
1274
Camden5b184be2019-08-13 07:50:19 -06001275bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1276 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1277 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1278 uint32_t bufferMemoryBarrierCount,
1279 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1280 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001281 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001282 bool skip = false;
1283
1284 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1285 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1286
1287 return skip;
1288}
1289
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001290bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1291 const VkDependencyInfoKHR* pDependencyInfos) const {
1292 bool skip = false;
1293 for (uint32_t i = 0; i < eventCount; i++) {
1294 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1295 }
1296
1297 return skip;
1298}
1299
Camden5b184be2019-08-13 07:50:19 -06001300bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1301 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1302 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1303 uint32_t bufferMemoryBarrierCount,
1304 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1305 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001306 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001307 bool skip = false;
1308
1309 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1310 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1311
1312 return skip;
1313}
1314
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001315bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1316 const VkDependencyInfoKHR* pDependencyInfo) const {
1317 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1318}
1319
Camden5b184be2019-08-13 07:50:19 -06001320bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001321 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001322 bool skip = false;
1323
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001324 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1325
1326 return skip;
1327}
1328
1329bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1330 VkQueryPool queryPool, uint32_t query) const {
1331 bool skip = false;
1332
1333 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001334
1335 return skip;
1336}
1337
Sam Walls0961ec02020-03-31 16:39:15 +01001338void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1339 VkPipeline pipeline) {
1340 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1341
1342 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
1343 // check for depth/blend state tracking
1344 auto gp_cis = graphicsPipelineCIs.find(pipeline);
1345 if (gp_cis != graphicsPipelineCIs.end()) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001346 auto& render_pass_state = cbRenderPassState[commandBuffer];
Sam Walls0961ec02020-03-31 16:39:15 +01001347
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001348 render_pass_state.nextDrawTouchesAttachments = gp_cis->second.accessFramebufferAttachments;
1349 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001350
Sam Walls0961ec02020-03-31 16:39:15 +01001351 const auto* blend_state = gp_cis->second.colorBlendStateCI;
1352 const auto* stencil_state = gp_cis->second.depthStencilStateCI;
1353
1354 if (blend_state) {
1355 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001356 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001357 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1358 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001359 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001360 }
1361 }
1362 }
1363
1364 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001365 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001366
1367 if (stencil_state && stencil_state->depthTestEnable) {
1368 switch (stencil_state->depthCompareOp) {
1369 case VK_COMPARE_OP_EQUAL:
1370 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1371 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001372 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001373 break;
1374 default:
1375 break;
1376 }
1377 }
Sam Walls0961ec02020-03-31 16:39:15 +01001378 }
1379 }
1380}
1381
Attilio Provenzano02859b22020-02-27 14:17:28 +00001382static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1383 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001384 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001385
1386 // If an attachment is ever used as a color attachment,
1387 // resolve attachment or depth stencil attachment,
1388 // it needs to exist on tile at some point.
1389
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001390 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1391 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001392 }
1393
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001394 if (subpass_info.pResolveAttachments) {
1395 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1396 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1397 }
1398 }
1399
1400 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001401 }
1402
1403 return false;
1404}
1405
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001406static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1407 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1408 return false;
1409 }
1410
1411 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001412 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001413
1414 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1415 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1416 return true;
1417 }
1418 }
1419 }
1420
1421 return false;
1422}
1423
Attilio Provenzano02859b22020-02-27 14:17:28 +00001424bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1425 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1426 bool skip = false;
1427
1428 if (!pRenderPassBegin) {
1429 return skip;
1430 }
1431
1432 auto rp_state = GetRenderPassState(pRenderPassBegin->renderPass);
1433 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001434 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001435 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001436 if (rpabi) {
1437 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1438 }
1439 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001440 // Check if any attachments have LOAD operation on them
1441 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001442 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001443
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001444 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001445 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001446 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001447 }
1448
1449 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001450 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001451 }
1452
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001453 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001454
1455 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001456 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1457 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001458 }
1459
1460 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001461 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1462 skip |= LogPerformanceWarning(
1463 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1464 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1465 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
1466 "which will copy in total %u pixels (renderArea = { %d, %d, %u, %u }) to the tile buffer.",
1467 VendorSpecificTag(kBPVendorArm), att,
1468 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1469 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1470 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001471 }
1472 }
1473 }
1474
1475 return skip;
1476}
1477
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001478void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1479 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001480 if (view) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001481 QueueValidateImage(funcs, function_name, GetImageUsageState(view->create_info.image), usage, view->create_info.subresourceRange);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001482 }
1483}
1484
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001485void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1486 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001487 const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001488 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001489
1490 // If we're viewing a 3D slice, ignore base array layer.
1491 // The entire 3D subresource is accessed as one atomic unit.
1492 const uint32_t base_array_layer = image->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
1493
1494 const uint32_t max_layers = image->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001495 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
1496 const uint32_t max_levels = image->createInfo.mipLevels - subresource_range.baseMipLevel;
1497 const uint32_t mip_levels = std::min(image->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001498
1499 for (uint32_t layer = 0; layer < array_layers; layer++) {
1500 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001501 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1502 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001503 }
1504 }
1505}
1506
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001507void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1508 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001509 const VkImageSubresourceLayers& subresource_layers) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001510 IMAGE_STATE* image = state->image;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001511 const uint32_t max_layers = image->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
1512 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001513
1514 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001515 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001516 }
1517}
1518
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001519void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
1520 IMAGE_STATE_BP* state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001521 uint32_t array_layer, uint32_t mip_level) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001522 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker*, const QUEUE_STATE*) -> bool {
1523 ValidateImageInQueue(function_name, state, usage, array_layer, mip_level);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001524 return false;
1525 });
1526}
1527
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001528void BestPractices::ValidateImageInQueueArm(const char* function_name, IMAGE_STATE* image,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001529 IMAGE_SUBRESOURCE_USAGE_BP last_usage,
1530 IMAGE_SUBRESOURCE_USAGE_BP usage,
1531 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001532 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001533 if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED &&
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001534 !image->is_swapchain_image) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001535 LogPerformanceWarning(
1536 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001537 "%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 +02001538 "image was used, it was written to with STORE_OP_STORE. "
1539 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1540 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001541 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001542 } 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 +02001543 LogPerformanceWarning(
1544 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001545 "%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 +02001546 "image was used, it was written to with vkCmdClear*Image(). "
1547 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1548 "tile-based architectures."
1549 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001550 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001551 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
1552 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE ||
1553 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1554 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE ||
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001555 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001556 const char *last_cmd = nullptr;
1557 const char *vuid = nullptr;
1558 const char *suggestion = nullptr;
1559
1560 switch (last_usage) {
1561 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1562 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1563 last_cmd = "vkCmdBlitImage";
1564 suggestion =
1565 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1566 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1567 "which avoids the memory roundtrip.";
1568 break;
1569 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1570 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1571 last_cmd = "vkCmdClear*Image";
1572 suggestion =
1573 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1574 "tile-based architectures. "
1575 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1576 break;
1577 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1578 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1579 last_cmd = "vkCmdCopy*Image";
1580 suggestion =
1581 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1582 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1583 "which avoids the memory roundtrip.";
1584 break;
1585 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1586 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1587 last_cmd = "vkCmdResolveImage";
1588 suggestion =
1589 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1590 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1591 "which avoids the memory roundtrip.";
1592 break;
1593 default:
1594 break;
1595 }
1596
1597 LogPerformanceWarning(
1598 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001599 "%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 +01001600 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001601 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001602 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001603}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001604
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001605void BestPractices::ValidateImageInQueue(const char* function_name, IMAGE_STATE_BP* state,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001606 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer,
1607 uint32_t mip_level) {
1608 IMAGE_STATE* image = state->image;
1609 IMAGE_SUBRESOURCE_USAGE_BP last_usage = state->usages[array_layer][mip_level];
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001610 state->usages[array_layer][mip_level] = usage;
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001611 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001612 ValidateImageInQueueArm(function_name, image, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001613 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001614}
1615
Hans-Kristian Arntzenb8336ad2021-04-28 14:54:09 +02001616void BestPractices::AddDeferredQueueOperations(CMD_BUFFER_STATE* cb) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001617 cb->queue_submit_functions.insert(cb->queue_submit_functions.end(),
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001618 cb->queue_submit_functions_after_render_pass.begin(),
1619 cb->queue_submit_functions_after_render_pass.end());
1620 cb->queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001621}
1622
1623void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1624 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzenb8336ad2021-04-28 14:54:09 +02001625 AddDeferredQueueOperations(GetCBState(commandBuffer));
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001626}
1627
1628void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1629 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Hans-Kristian Arntzenb8336ad2021-04-28 14:54:09 +02001630 AddDeferredQueueOperations(GetCBState(commandBuffer));
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001631}
1632
1633void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1634 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Hans-Kristian Arntzenb8336ad2021-04-28 14:54:09 +02001635 AddDeferredQueueOperations(GetCBState(commandBuffer));
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001636}
1637
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001638void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1639 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001640 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001641 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001642 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1643}
1644
1645void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1646 const VkRenderPassBeginInfo* pRenderPassBegin,
1647 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1648 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1649 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1650}
1651
1652void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1653 const VkRenderPassBeginInfo* pRenderPassBegin,
1654 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1655 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1656 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1657}
1658
1659void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001660
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001661 if (!pRenderPassBegin) {
1662 return;
1663 }
1664
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001665 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
1666
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001667 auto rp_state = GetRenderPassState(pRenderPassBegin->renderPass);
1668 if (rp_state) {
1669 // Check load ops
1670 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001671 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001672
1673 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1674 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1675 continue;
1676 }
1677
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001678 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001679
1680 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1681 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001682 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001683 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1684 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001685 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001686 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001687 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001688 }
1689
1690 auto framebuffer = GetFramebufferState(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001691 IMAGE_VIEW_STATE* image_view = nullptr;
1692
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001693 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001694 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1695 if (rpabi) {
1696 image_view = GetImageViewState(rpabi->pAttachments[att]);
1697 }
1698 } else {
1699 image_view = GetImageViewState(framebuffer->createInfo.pAttachments[att]);
1700 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001701
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001702 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view, usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001703 }
1704
1705 // Check store ops
1706 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001707 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001708
1709 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1710 continue;
1711 }
1712
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001713 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001714
1715 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1716 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001717 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001718 }
1719
1720 auto framebuffer = GetFramebufferState(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001721
1722 IMAGE_VIEW_STATE* image_view = nullptr;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001723 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001724 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1725 if (rpabi) {
1726 image_view = GetImageViewState(rpabi->pAttachments[att]);
1727 }
1728 } else {
1729 image_view = GetImageViewState(framebuffer->createInfo.pAttachments[att]);
1730 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001731
Hans-Kristian Arntzenf44df192021-06-14 11:42:08 +02001732 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view, usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001733 }
1734 }
1735}
1736
Attilio Provenzano02859b22020-02-27 14:17:28 +00001737bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1738 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001739 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1740 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001741 return skip;
1742}
1743
1744bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1745 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001746 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001747 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1748 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001749 return skip;
1750}
1751
1752bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001753 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001754 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1755 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001756 return skip;
1757}
1758
Sam Walls0961ec02020-03-31 16:39:15 +01001759void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1760 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001761 // Reset the renderpass state
1762 auto& render_pass_state = cbRenderPassState[commandBuffer];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02001763 render_pass_state.touchesAttachments.clear();
1764 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001765 render_pass_state.numDrawCallsDepthOnly = 0;
1766 render_pass_state.numDrawCallsDepthEqualCompare = 0;
1767 render_pass_state.colorAttachment = false;
1768 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001769 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001770 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01001771
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02001772 auto* cb = GetCBState(commandBuffer);
1773 cb->hasDrawCmd = false;
1774
Hans-Kristian Arntzena1a92cc2021-03-17 13:09:33 +01001775 const auto* rp_state = GetRenderPassState(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01001776
1777 // track depth / color attachment usage within the renderpass
1778 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
1779 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001780 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001781
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001782 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001783 }
1784}
1785
1786void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1787 VkSubpassContents contents) {
1788 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1789 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
1790}
1791
1792void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1793 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1794 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1795 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1796}
1797
1798void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1799 const VkRenderPassBeginInfo* pRenderPassBegin,
1800 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1801 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1802 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1803}
1804
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001805// Generic function to handle validation for all CmdDraw* type functions
1806bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
1807 bool skip = false;
1808 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
1809 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001810 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
1811 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001812 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001813
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001814 // Verify vertex binding
1815 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
1816 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001817 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001818 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001819 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
1820 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001821 }
1822 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06001823
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001824 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06001825 if (pipe) {
1826 const auto* rp_state = pipe->rp_state.get();
1827 if (rp_state) {
1828 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
1829 const auto& subpass = rp_state->createInfo.pSubpasses[i];
1830 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(
1831 pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
1832 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && pipe->graphicsPipelineCI.pRasterizationState &&
1833 pipe->graphicsPipelineCI.pRasterizationState->depthBiasEnable == VK_TRUE) {
1834 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
1835 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
1836 }
1837 }
1838 }
1839 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001840 }
1841 return skip;
1842}
1843
Sam Walls0961ec02020-03-31 16:39:15 +01001844void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001845 auto& render_pass_state = cbRenderPassState[cmd_buffer];
Sam Walls0961ec02020-03-31 16:39:15 +01001846 if (VendorCheckEnabled(kBPVendorArm)) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001847 RecordCmdDrawTypeArm(render_pass_state, draw_count, caller);
1848 }
1849
1850 if (render_pass_state.drawTouchAttachments) {
1851 for (auto& touch : render_pass_state.nextDrawTouchesAttachments) {
1852 RecordAttachmentAccess(render_pass_state, touch.framebufferAttachment, touch.aspects);
1853 }
1854 // No need to touch the same attachments over and over.
1855 render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001856 }
1857}
1858
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001859void BestPractices::RecordCmdDrawTypeArm(RenderPassState& render_pass_state, uint32_t draw_count, const char* caller) {
1860 if (draw_count >= kDepthPrePassMinDrawCountArm) {
1861 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
1862 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01001863 }
1864}
1865
Camden5b184be2019-08-13 07:50:19 -06001866bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001867 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06001868 bool skip = false;
1869
1870 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001871 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
1872 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06001873 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06001874 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06001875
1876 return skip;
1877}
1878
Sam Walls0961ec02020-03-31 16:39:15 +01001879void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
1880 uint32_t firstVertex, uint32_t firstInstance) {
1881 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
1882 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
1883}
1884
Camden5b184be2019-08-13 07:50:19 -06001885bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001886 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06001887 bool skip = false;
1888
1889 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001890 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
1891 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06001892 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001893 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
1894
Attilio Provenzano02859b22020-02-27 14:17:28 +00001895 // Check if we reached the limit for small indexed draw calls.
1896 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
1897 const CMD_BUFFER_STATE* cmd_state = GetCBState(commandBuffer);
1898 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001899 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
1900 VendorCheckEnabled(kBPVendorArm)) {
1901 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06001902 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00001903 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
1904 "You can try batching drawcalls or instancing when applicable.",
1905 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
1906 }
1907
Sam Walls8e77e4f2020-03-16 20:47:40 +00001908 if (VendorCheckEnabled(kBPVendorArm)) {
1909 ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
1910 }
1911
1912 return skip;
1913}
1914
1915bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
1916 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
1917 bool skip = false;
1918
1919 // check for sparse/underutilised index buffer, and post-transform cache thrashing
1920 const auto* cmd_state = GetCBState(commandBuffer);
1921 if (cmd_state == nullptr) return skip;
1922
locke-lunarg1ae57d62020-11-18 10:49:19 -07001923 const auto* ib_state = cmd_state->index_buffer_binding.buffer_state.get();
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06001924 if (ib_state == nullptr || cmd_state->index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00001925
1926 const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06001927 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00001928 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
1929 const void* ib_mem = ib_mem_state.p_driver_data;
1930 bool primitive_restart_enable = false;
1931
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001932 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
1933 const auto& pipeline_binding_iter = cmd_state->lastBound[lv_bind_point];
1934 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00001935
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001936 if (pipeline_state != nullptr && pipeline_state->graphicsPipelineCI.pInputAssemblyState != nullptr) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001937 primitive_restart_enable = pipeline_state->graphicsPipelineCI.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001938 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00001939
1940 // 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 -06001941 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00001942 uint32_t scan_stride;
1943 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
1944 scan_stride = sizeof(uint8_t);
1945 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
1946 scan_stride = sizeof(uint16_t);
1947 } else {
1948 scan_stride = sizeof(uint32_t);
1949 }
1950
1951 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
1952 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
1953
1954 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
1955 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
1956 // irrespective of whether or not they're part of the draw call.
1957
1958 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
1959 uint32_t min_index = ~0u;
1960 // start with maximum as 0 and adjust to indices in the buffer
1961 uint32_t max_index = 0u;
1962
1963 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
1964 // for the given index buffer
1965 uint32_t vertex_shade_count = 0;
1966
1967 PostTransformLRUCacheModel post_transform_cache;
1968
1969 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
1970 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
1971 // target architecture.
1972 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
1973 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
1974 post_transform_cache.resize(32);
1975
1976 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
1977 uint32_t scan_index;
1978 uint32_t primitive_restart_value;
1979 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
1980 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
1981 primitive_restart_value = 0xFF;
1982 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
1983 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
1984 primitive_restart_value = 0xFFFF;
1985 } else {
1986 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
1987 primitive_restart_value = 0xFFFFFFFF;
1988 }
1989
1990 max_index = std::max(max_index, scan_index);
1991 min_index = std::min(min_index, scan_index);
1992
1993 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
1994 bool in_cache = post_transform_cache.query_cache(scan_index);
1995 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
1996 if (!in_cache) vertex_shade_count++;
1997 }
1998 }
1999
2000 // 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 +01002001 // 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
2002 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002003
2004 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002005 skip |=
2006 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2007 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2008 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2009 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2010 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2011 VendorSpecificTag(kBPVendorArm),
2012 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002013 return skip;
2014 }
2015
2016 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2017 // 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 +01002018 const size_t refs_per_bucket = 64;
2019 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2020
2021 const uint32_t n_indices = max_index - min_index + 1;
2022 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2023 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2024
2025 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2026 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002027
2028 // To avoid using too much memory, we run over the indices again.
2029 // Knowing the size from the last scan allows us to record index usage with bitsets
2030 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2031 uint32_t scan_index;
2032 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2033 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2034 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2035 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2036 } else {
2037 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2038 }
2039 // keep track of the set of all indices used to reference vertices in the draw call
2040 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002041 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2042 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002043 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2044 }
2045
2046 uint32_t vertex_reference_count = 0;
2047 for (const auto& bitset : vertex_reference_buckets) {
2048 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2049 }
2050
2051 // 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 -07002052 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002053 // 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 -07002054 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002055
2056 if (utilization < 0.5f) {
2057 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2058 "%s The indices which were specified for the draw call only utilise approximately "
2059 "%.02f%% of the bound vertex buffer.",
2060 VendorSpecificTag(kBPVendorArm), utilization);
2061 }
2062
2063 if (cache_hit_rate <= 0.5f) {
2064 skip |=
2065 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2066 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2067 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2068 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2069 "recently shaded vertices.",
2070 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2071 }
2072 }
2073
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002074 return skip;
2075}
2076
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002077bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2078 const VkCommandBuffer* pCommandBuffers) const {
2079 bool skip = false;
2080 const CMD_BUFFER_STATE* primary = GetCBState(commandBuffer);
2081 for (uint32_t i = 0; i < commandBufferCount; i++) {
2082 auto secondary_itr = cbRenderPassState.find(pCommandBuffers[i]);
2083 if (secondary_itr == cbRenderPassState.end()) {
2084 continue;
2085 }
2086 auto& secondary = secondary_itr->second;
2087 for (auto& clear : secondary.earlyClearAttachments) {
2088 if (ClearAttachmentsIsFullClear(primary, clear.rects.size(), clear.rects.data())) {
2089 skip |= ValidateClearAttachment(commandBuffer, primary,
2090 clear.framebufferAttachment, clear.colorAttachment,
2091 clear.aspects, true);
2092 }
2093 }
2094 }
2095 return skip;
2096}
2097
2098void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2099 const VkCommandBuffer* pCommandBuffers) {
2100 CMD_BUFFER_STATE* primary = GetCBState(commandBuffer);
2101 auto& primary_state = cbRenderPassState[commandBuffer];
2102
2103 for (uint32_t i = 0; i < commandBufferCount; i++) {
2104 auto& secondary = cbRenderPassState[pCommandBuffers[i]];
2105
2106 for (auto& early_clear : secondary.earlyClearAttachments) {
2107 if (ClearAttachmentsIsFullClear(primary, early_clear.rects.size(), early_clear.rects.data())) {
2108 RecordAttachmentClearAttachments(primary, primary_state, early_clear.framebufferAttachment,
2109 early_clear.colorAttachment, early_clear.aspects,
2110 early_clear.rects.size(), early_clear.rects.data());
2111 } else {
2112 RecordAttachmentAccess(primary_state, early_clear.framebufferAttachment,
2113 early_clear.aspects);
2114 }
2115 }
2116
2117 for (auto& touch : secondary.touchesAttachments) {
2118 RecordAttachmentAccess(primary_state, touch.framebufferAttachment,
2119 touch.aspects);
2120 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002121
2122 primary_state.numDrawCallsDepthEqualCompare += secondary.numDrawCallsDepthEqualCompare;
2123 primary_state.numDrawCallsDepthOnly += secondary.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002124
2125 CMD_BUFFER_STATE* second_state = GetCBState(pCommandBuffers[i]);
2126 if (second_state->hasDrawCmd) {
2127 primary->hasDrawCmd = true;
2128 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002129 }
2130
2131 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2132}
2133
2134void BestPractices::RecordAttachmentAccess(RenderPassState& state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2135 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
2136 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002137 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002138 return info.framebufferAttachment == fb_attachment;
2139 });
2140
2141 if (itr != state.touchesAttachments.end()) {
2142 itr->aspects |= aspects;
2143 } else {
2144 state.touchesAttachments.push_back({ fb_attachment, aspects });
2145 }
2146}
2147
2148void BestPractices::RecordAttachmentClearAttachments(CMD_BUFFER_STATE* cmd_state, RenderPassState& state,
2149 uint32_t fb_attachment, uint32_t color_attachment,
2150 VkImageAspectFlags aspects, uint32_t rectCount,
2151 const VkClearRect *pRects) {
2152 // If we observe a full clear before any other access to a frame buffer attachment,
2153 // we have candidate for redundant clear attachments.
2154 auto itr = std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002155 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002156 return info.framebufferAttachment == fb_attachment;
2157 });
2158
2159 uint32_t new_aspects = aspects;
2160 if (itr != state.touchesAttachments.end()) {
2161 new_aspects = aspects & ~itr->aspects;
2162 itr->aspects |= aspects;
2163 } else {
2164 state.touchesAttachments.push_back({ fb_attachment, aspects });
2165 }
2166
2167 if (new_aspects == 0) {
2168 return;
2169 }
2170
2171 if (cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2172 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2173 // CmdExecuteCommands.
2174 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2175 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2176 }
2177}
2178
2179void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2180 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2181 uint32_t rectCount, const VkClearRect* pRects) {
2182 CMD_BUFFER_STATE* cmd_state = GetCBState(commandBuffer);
2183 RENDER_PASS_STATE* rp_state = cmd_state->activeRenderPass.get();
2184 FRAMEBUFFER_STATE* fb_state = cmd_state->activeFramebuffer.get();
2185 RenderPassState& tracking_state = cbRenderPassState[commandBuffer];
2186 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2187
2188 if (rectCount == 0 || !rp_state) {
2189 return;
2190 }
2191
2192 if (!is_secondary && !fb_state) {
2193 return;
2194 }
2195
2196 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
2197 bool full_clear = ClearAttachmentsIsFullClear(cmd_state, rectCount, pRects);
2198
2199 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2200 for (uint32_t i = 0; i < attachmentCount; i++) {
2201 auto& attachment = pClearAttachments[i];
2202 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2203 VkImageAspectFlags aspects = attachment.aspectMask;
2204
2205 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2206 if (subpass.pDepthStencilAttachment) {
2207 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2208 }
2209 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2210 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
2211 }
2212
2213 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2214 if (full_clear) {
2215 RecordAttachmentClearAttachments(cmd_state, tracking_state,
2216 fb_attachment, attachment.colorAttachment, aspects,
2217 rectCount, pRects);
2218 } else {
2219 RecordAttachmentAccess(tracking_state, fb_attachment, aspects);
2220 }
2221 }
2222 }
2223
2224 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments,
2225 rectCount, pRects);
2226}
2227
Attilio Provenzano02859b22020-02-27 14:17:28 +00002228void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2229 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2230 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2231 firstInstance);
2232
2233 CMD_BUFFER_STATE* cmd_state = GetCBState(commandBuffer);
2234 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2235 cmd_state->small_indexed_draw_call_count++;
2236 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002237
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002238 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002239}
2240
Sam Walls0961ec02020-03-31 16:39:15 +01002241void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2242 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2243 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2244 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2245}
2246
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002247bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2248 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2249 uint32_t maxDrawCount, uint32_t stride) const {
2250 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2251
2252 return skip;
2253}
2254
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002255bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2256 VkDeviceSize offset, VkBuffer countBuffer,
2257 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2258 uint32_t stride) const {
2259 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002260
2261 return skip;
2262}
2263
2264bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002265 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002266 bool skip = false;
2267
2268 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002269 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2270 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002271 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002272 }
2273
2274 return skip;
2275}
2276
Sam Walls0961ec02020-03-31 16:39:15 +01002277void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2278 uint32_t count, uint32_t stride) {
2279 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2280 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2281}
2282
Camden5b184be2019-08-13 07:50:19 -06002283bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002284 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002285 bool skip = false;
2286
2287 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002288 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2289 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002290 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002291 }
2292
2293 return skip;
2294}
2295
Sam Walls0961ec02020-03-31 16:39:15 +01002296void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2297 uint32_t count, uint32_t stride) {
2298 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2299 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2300}
2301
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002302void BestPractices::ValidateBoundDescriptorSets(VkCommandBuffer commandBuffer, const char* function_name) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002303 CMD_BUFFER_STATE* cb_state = GetCBState(commandBuffer);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002304
2305 if (cb_state) {
2306 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002307 const auto& layout = *descriptor_set->GetLayout();
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002308
2309 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2310 // For bindless scenarios, we should not attempt to track descriptor set state.
2311 // It is highly uncertain which resources are actually bound.
2312 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2313 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2314 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
2315 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2316 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2317 continue;
2318 }
2319
2320 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002321 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002322 VkImageView image_view{VK_NULL_HANDLE};
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002323
2324 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2325 switch (descriptor->GetClass()) {
2326 case cvdescriptorset::DescriptorClass::Image: {
2327 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2328 image_view = image_descriptor->GetImageView();
2329 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002330 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002331 }
2332 case cvdescriptorset::DescriptorClass::ImageSampler: {
2333 if (const auto image_sampler_descriptor =
2334 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2335 image_view = image_sampler_descriptor->GetImageView();
2336 }
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002337 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002338 }
ZandroFargnoliacf12f02020-06-18 16:50:00 +01002339 default:
Hans-Kristian Arntzenbc3a6152021-03-22 13:05:43 +01002340 break;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002341 }
2342
2343 if (image_view) {
2344 IMAGE_VIEW_STATE* image_view_state = GetImageViewState(image_view);
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002345 QueueValidateImageView(cb_state->queue_submit_functions, function_name,
2346 image_view_state, IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002347 }
2348 }
2349 }
2350 }
2351 }
2352}
2353
2354void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2355 uint32_t firstVertex, uint32_t firstInstance) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002356 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002357}
2358
2359void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2360 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002361 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002362}
2363
2364void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2365 uint32_t drawCount, uint32_t stride) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002366 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002367}
2368
Camden5b184be2019-08-13 07:50:19 -06002369bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002370 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002371 bool skip = false;
2372
2373 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002374 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2375 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2376 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2377 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002378 }
2379
2380 return skip;
2381}
Camden83a9c372019-08-14 11:41:38 -06002382
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002383bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2384 bool skip = false;
2385 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2386 skip |= ValidateCmdEndRenderPass(commandBuffer);
2387 return skip;
2388}
2389
2390bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2391 bool skip = false;
2392 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2393 skip |= ValidateCmdEndRenderPass(commandBuffer);
2394 return skip;
2395}
2396
Sam Walls0961ec02020-03-31 16:39:15 +01002397bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2398 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002399 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002400 skip |= ValidateCmdEndRenderPass(commandBuffer);
2401 return skip;
2402}
2403
2404bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2405 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002406
Hans-Kristian Arntzen09aff9d2021-06-14 15:17:02 +02002407 auto render_pass_state = cbRenderPassState.find(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002408
Hans-Kristian Arntzen09aff9d2021-06-14 15:17:02 +02002409 if (render_pass_state == cbRenderPassState.end()) return skip;
Sam Walls0961ec02020-03-31 16:39:15 +01002410
Hans-Kristian Arntzen09aff9d2021-06-14 15:17:02 +02002411 bool uses_depth = (render_pass_state->second.depthAttachment || render_pass_state->second.colorAttachment) &&
2412 render_pass_state->second.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2413 render_pass_state->second.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002414 if (uses_depth) {
2415 skip |= LogPerformanceWarning(
2416 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2417 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2418 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2419 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2420 VendorSpecificTag(kBPVendorArm));
2421 }
2422
2423 return skip;
2424}
2425
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002426void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002427 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002428}
2429
2430void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002431 ValidateBoundDescriptorSets(commandBuffer, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002432}
2433
Camden Stocker9c051442019-11-06 14:28:43 -08002434bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2435 const char* api_name) const {
2436 bool skip = false;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002437 const auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002438
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002439 if (bp_pd_state) {
2440 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2441 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2442 "Potential problem with calling %s() without first retrieving properties from "
2443 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2444 api_name);
2445 }
Camden Stocker9c051442019-11-06 14:28:43 -08002446 }
2447
2448 return skip;
2449}
2450
Camden83a9c372019-08-14 11:41:38 -06002451bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002452 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002453 bool skip = false;
2454
Camden Stocker9c051442019-11-06 14:28:43 -08002455 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002456
Camden Stocker9c051442019-11-06 14:28:43 -08002457 return skip;
2458}
2459
2460bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2461 uint32_t planeIndex,
2462 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2463 bool skip = false;
2464
2465 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2466
2467 return skip;
2468}
2469
2470bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2471 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2472 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2473 bool skip = false;
2474
2475 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002476
2477 return skip;
2478}
Camden05de2d42019-08-19 10:23:56 -06002479
2480bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002481 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002482 bool skip = false;
2483
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002484 const auto* swapchain_state = static_cast<SWAPCHAIN_STATE_BP*>(Get<SWAPCHAIN_NODE>(swapchain));
Camden05de2d42019-08-19 10:23:56 -06002485
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002486 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002487 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002488 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002489 skip |=
2490 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2491 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2492 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002493 }
Camden05de2d42019-08-19 10:23:56 -06002494
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002495 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2496 skip |= LogWarning(
2497 device, kVUID_BestPractices_Swapchain_InvalidCount,
2498 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
2499 "value (%d) that is greater than the value (%d) that was returned when pSwapchainImages was NULL.",
2500 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2501 }
2502 }
2503
Camden05de2d42019-08-19 10:23:56 -06002504 return skip;
2505}
2506
2507// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002508bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state,
2509 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002510 const CALL_STATE call_state,
2511 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002512 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002513 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2514 if (UNCALLED == call_state) {
2515 skip |= LogWarning(
2516 pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount,
2517 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2518 "recommended "
2519 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2520 caller_name, caller_name);
2521 // Then verify that pCount that is passed in on second call matches what was returned
2522 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
2523 skip |= LogWarning(pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch,
2524 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2525 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2526 ". It is recommended to instead receive all the properties by calling %s with "
2527 "pQueueFamilyPropertyCount that was "
2528 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
2529 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name,
2530 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002531 }
2532
2533 return skip;
2534}
2535
Jeff Bolz5c801d12019-10-09 10:38:45 -05002536bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2537 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002538 bool skip = false;
2539
2540 for (uint32_t i = 0; i < bindInfoCount; i++) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002541 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureStateNV(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002542 if (!as_state->memory_requirements_checked) {
2543 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2544 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2545 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002546 skip |= LogWarning(
2547 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002548 "vkBindAccelerationStructureMemoryNV(): "
2549 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2550 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2551 }
2552 }
2553
2554 return skip;
2555}
2556
Camden05de2d42019-08-19 10:23:56 -06002557bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2558 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002559 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06002560 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
2561 assert(physical_device_state);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002562 const auto* bp_pd_state = GetPhysicalDeviceStateBP(physical_device_state->phys_device);
2563 if (pQueueFamilyProperties && bp_pd_state) {
2564 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
2565 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2566 "vkGetPhysicalDeviceQueueFamilyProperties()");
2567 }
2568 return false;
Camden05de2d42019-08-19 10:23:56 -06002569}
2570
Mike Schuchardt2df08912020-12-15 16:28:09 -08002571bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2572 uint32_t* pQueueFamilyPropertyCount,
2573 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06002574 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
2575 assert(physical_device_state);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002576 const auto* bp_pd_state = GetPhysicalDeviceStateBP(physical_device_state->phys_device);
2577 if (pQueueFamilyProperties && bp_pd_state) {
2578 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
2579 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2580 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2581 }
2582 return false;
Camden05de2d42019-08-19 10:23:56 -06002583}
2584
Jeff Bolz5c801d12019-10-09 10:38:45 -05002585bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002586 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06002587 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
2588 assert(physical_device_state);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002589 const auto* bp_pd_state = GetPhysicalDeviceStateBP(physical_device_state->phys_device);
2590 if (pQueueFamilyProperties && bp_pd_state) {
2591 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
2592 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2593 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2594 }
2595 return false;
Camden05de2d42019-08-19 10:23:56 -06002596}
2597
2598bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2599 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002600 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002601 if (!pSurfaceFormats) return false;
2602 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002603 const auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
2604 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002605 bool skip = false;
2606 if (call_state == UNCALLED) {
2607 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2608 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002609 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2610 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2611 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002612 } else {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002613 auto prev_format_count = static_cast<uint32_t>(physical_device_state->surface_formats.size());
Peter Chene191bd72019-09-16 13:04:37 -04002614 if (*pSurfaceFormatCount > prev_format_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002615 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2616 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2617 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2618 "when pSurfaceFormatCount was NULL.",
2619 *pSurfaceFormatCount, prev_format_count);
Camden05de2d42019-08-19 10:23:56 -06002620 }
2621 }
2622 return skip;
2623}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002624
2625bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002626 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002627 bool skip = false;
2628
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002629 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2630 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002631 // 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 -07002632 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002633 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2634 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002635 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002636 // 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 -07002637 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2638 const auto& image_bind = bind_info.pImageBinds[i];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002639 auto image_state = GetImageState(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002640 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002641 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002642 }
Camden Stocker23cc47d2019-09-03 14:53:57 -06002643 sparse_images.insert(image_state);
2644 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2645 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2646 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002647 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002648 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2649 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002650 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002651 }
2652 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002653 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002654 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002655 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002656 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2657 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002658 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002659 }
2660 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002661 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2662 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
2663 auto image_state = GetImageState(bind_info.pImageOpaqueBinds[i].image);
2664 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002665 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002666 }
Camden Stocker23cc47d2019-09-03 14:53:57 -06002667 sparse_images.insert(image_state);
2668 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2669 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2670 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002671 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002672 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2673 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002674 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002675 }
2676 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002677 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002678 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002679 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002680 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2681 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002682 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002683 }
2684 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2685 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002686 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -06002687 }
2688 }
2689 }
2690 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002691 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2692 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002693 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002694 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002695 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2696 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002697 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002698 }
2699 }
2700 }
2701
2702 return skip;
2703}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002704
Mark Lobodzinski84101d72020-04-24 09:43:48 -06002705void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
2706 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002707 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002708 return;
2709 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002710
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002711 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2712 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
2713 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2714 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
2715 auto image_state = GetImageState(bind_info.pImageOpaqueBinds[i].image);
2716 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002717 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002718 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002719 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2720 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
2721 image_state->sparse_metadata_bound = true;
2722 }
2723 }
2724 }
2725 }
2726}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002727
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002728bool BestPractices::ClearAttachmentsIsFullClear(const CMD_BUFFER_STATE* cmd,
2729 uint32_t rectCount, const VkClearRect* pRects) const {
2730 if (cmd->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2731 // We don't know the accurate render area in a secondary,
2732 // so assume we clear the entire frame buffer.
2733 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
2734 return true;
2735 }
2736
2737 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
2738 for (uint32_t i = 0; i < rectCount; i++) {
2739 auto& rect = pRects[i];
2740 auto& render_area = cmd->activeRenderPassBeginInfo.renderArea;
2741 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
2742 return true;
2743 }
2744 }
2745
2746 return false;
2747}
2748
2749bool BestPractices::ValidateClearAttachment(VkCommandBuffer commandBuffer, const CMD_BUFFER_STATE* cmd,
2750 uint32_t fb_attachment, uint32_t color_attachment,
2751 VkImageAspectFlags aspects, bool secondary) const {
2752 const RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2753 bool skip = false;
2754
2755 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
2756 return skip;
2757 }
2758
2759 auto rp_itr = cbRenderPassState.find(commandBuffer);
2760 if (rp_itr == cbRenderPassState.end()) {
2761 return skip;
2762 }
2763
2764 auto attachment_itr = std::find_if(rp_itr->second.touchesAttachments.begin(), rp_itr->second.touchesAttachments.end(),
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002765 [&](const AttachmentInfo& info) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002766 return info.framebufferAttachment == fb_attachment;
2767 });
2768
2769 // Only report aspects which haven't been touched yet.
2770 VkImageAspectFlags new_aspects = aspects;
2771 if (attachment_itr != rp_itr->second.touchesAttachments.end()) {
2772 new_aspects &= ~attachment_itr->aspects;
2773 }
2774
2775 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
2776 if (!cmd->hasDrawCmd) {
2777 skip |= LogPerformanceWarning(
2778 commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02002779 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
2780 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002781 report_data->FormatHandle(commandBuffer).c_str());
2782 }
2783
2784 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
2785 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
2786 skip |= LogPerformanceWarning(
2787 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
2788 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
2789 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
2790 "it is more efficient.",
2791 secondary ? "vkCmdExecuteCommands(): " : "",
2792 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
2793 }
2794
2795 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
2796 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
2797 skip |= LogPerformanceWarning(
2798 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
2799 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
2800 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
2801 "it is more efficient.",
2802 secondary ? "vkCmdExecuteCommands(): " : "",
2803 report_data->FormatHandle(commandBuffer).c_str());
2804 }
2805
2806 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
2807 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
2808 skip |= LogPerformanceWarning(
2809 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
2810 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
2811 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
2812 "it is more efficient.",
2813 secondary ? "vkCmdExecuteCommands(): " : "",
2814 report_data->FormatHandle(commandBuffer).c_str());
2815 }
2816
2817 return skip;
2818}
2819
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002820bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06002821 const VkClearAttachment* pAttachments, uint32_t rectCount,
2822 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002823 bool skip = false;
2824 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
2825 if (!cb_node) return skip;
2826
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002827 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
2828 // Defer checks to ExecuteCommands.
2829 return skip;
2830 }
2831
2832 // Only care about full clears, partial clears might have legitimate uses.
2833 if (!ClearAttachmentsIsFullClear(cb_node, rectCount, pRects)) {
2834 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002835 }
2836
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002837 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
2838 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06002839 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002840 if (rp) {
2841 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
2842
2843 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002844 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002845
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002846 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
2847 uint32_t color_attachment = attachment.colorAttachment;
2848 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002849 skip |= ValidateClearAttachment(commandBuffer, cb_node,
2850 fb_attachment, color_attachment,
2851 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002852 }
2853
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002854 if (subpass.pDepthStencilAttachment &&
2855 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002856 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002857 skip |= ValidateClearAttachment(commandBuffer, cb_node,
2858 fb_attachment, VK_ATTACHMENT_UNUSED,
2859 attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00002860 }
2861 }
2862 }
2863
Camden Stockerf55721f2019-09-09 11:04:49 -06002864 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002865}
Attilio Provenzano02859b22020-02-27 14:17:28 +00002866
2867bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2868 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2869 const VkImageResolve* pRegions) const {
2870 bool skip = false;
2871
2872 skip |= VendorCheckEnabled(kBPVendorArm) &&
2873 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
2874 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
2875 "This is a very slow and extremely bandwidth intensive path. "
2876 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
2877 VendorSpecificTag(kBPVendorArm));
2878
2879 return skip;
2880}
2881
Jeff Leger178b1e52020-10-05 12:22:23 -04002882bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
2883 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
2884 bool skip = false;
2885
2886 skip |= VendorCheckEnabled(kBPVendorArm) &&
2887 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
2888 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
2889 "This is a very slow and extremely bandwidth intensive path. "
2890 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
2891 VendorSpecificTag(kBPVendorArm));
2892
2893 return skip;
2894}
2895
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002896void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2897 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2898 const VkImageResolve* pRegions) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002899 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002900 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002901 auto* src = GetImageUsageState(srcImage);
2902 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002903
2904 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002905 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
2906 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002907 }
2908}
2909
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01002910void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
2911 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002912 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002913 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002914 auto* src = GetImageUsageState(pResolveImageInfo->srcImage);
2915 auto* dst = GetImageUsageState(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01002916 uint32_t regionCount = pResolveImageInfo->regionCount;
2917
2918 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002919 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
2920 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01002921 }
2922}
2923
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002924void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
2925 const VkClearColorValue* pColor, uint32_t rangeCount,
2926 const VkImageSubresourceRange* pRanges) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002927 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002928 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002929 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002930
2931 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002932 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002933 }
2934}
2935
2936void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
2937 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
2938 const VkImageSubresourceRange* pRanges) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002939 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002940 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002941 auto* dst = GetImageUsageState(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002942
2943 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002944 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002945 }
2946}
2947
2948void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2949 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2950 const VkImageCopy* pRegions) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002951 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002952 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002953 auto* src = GetImageUsageState(srcImage);
2954 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002955
2956 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002957 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
2958 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002959 }
2960}
2961
2962void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2963 VkImageLayout dstImageLayout, uint32_t regionCount,
2964 const VkBufferImageCopy* pRegions) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002965 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002966 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002967 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002968
2969 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002970 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002971 }
2972}
2973
2974void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2975 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002976 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002977 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002978 auto* src = GetImageUsageState(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002979
2980 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002981 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002982 }
2983}
2984
2985void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2986 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2987 const VkImageBlit* pRegions, VkFilter filter) {
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002988 CMD_BUFFER_STATE* cb = GetCBState(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002989 auto &funcs = cb->queue_submit_functions;
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002990 auto* src = GetImageUsageState(srcImage);
2991 auto* dst = GetImageUsageState(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002992
2993 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002994 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
2995 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002996 }
2997}
2998
Attilio Provenzano02859b22020-02-27 14:17:28 +00002999bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3000 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3001 bool skip = false;
3002
3003 if (VendorCheckEnabled(kBPVendorArm)) {
3004 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3005 skip |= LogPerformanceWarning(
3006 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3007 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3008 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3009 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003010 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003011 }
3012
3013 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3014 skip |= LogPerformanceWarning(
3015 device, kVUID_BestPractices_CreateSampler_LodClamping,
3016 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3017 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3018 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3019 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3020 }
3021
3022 if (pCreateInfo->mipLodBias != 0.0f) {
3023 skip |=
3024 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3025 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3026 "descriptors being created and may cause reduced performance.",
3027 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3028 }
3029
3030 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3031 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3032 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3033 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3034 skip |= LogPerformanceWarning(
3035 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3036 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3037 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3038 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3039 VendorSpecificTag(kBPVendorArm));
3040 }
3041
3042 if (pCreateInfo->unnormalizedCoordinates) {
3043 skip |= LogPerformanceWarning(
3044 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3045 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3046 "descriptors being created and may cause reduced performance.",
3047 VendorSpecificTag(kBPVendorArm));
3048 }
3049
3050 if (pCreateInfo->anisotropyEnable) {
3051 skip |= LogPerformanceWarning(
3052 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3053 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3054 "and may cause reduced performance.",
3055 VendorSpecificTag(kBPVendorArm));
3056 }
3057 }
3058
3059 return skip;
3060}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003061
3062void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3063
3064bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3065 // look for a cache hit
3066 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3067 if (hit != _entries.end()) {
3068 // mark the cache hit as being most recently used
3069 hit->age = iteration++;
3070 return true;
3071 }
3072
3073 // if there's no cache hit, we need to model the entry being inserted into the cache
3074 CacheEntry new_entry = {value, iteration};
3075 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3076 // if there is still space left in the cache, use the next available slot
3077 *(_entries.begin() + iteration) = new_entry;
3078 } else {
3079 // otherwise replace the least recently used cache entry
3080 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3081 *lru = new_entry;
3082 }
3083 iteration++;
3084 return false;
3085}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003086
3087bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3088 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
3089 const auto swapchain_data = GetSwapchainState(swapchain);
3090 bool skip = false;
3091 if (swapchain_data && swapchain_data->images.size() == 0) {
3092 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3093 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3094 "vkGetSwapchainImagesKHR after swapchain creation.");
3095 }
3096 return skip;
3097}
3098
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003099void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3100 if (no_pointer) {
3101 if (UNCALLED == call_state) {
3102 call_state = QUERY_COUNT;
3103 }
3104 } else { // Save queue family properties
3105 call_state = QUERY_DETAILS;
3106 }
3107}
3108
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003109void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3110 uint32_t* pQueueFamilyPropertyCount,
3111 VkQueueFamilyProperties* pQueueFamilyProperties) {
3112 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3113 pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003114 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003115 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003116 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3117 nullptr == pQueueFamilyProperties);
3118 }
3119}
3120
3121void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3122 uint32_t* pQueueFamilyPropertyCount,
3123 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3124 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3125 pQueueFamilyProperties);
3126 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3127 if (bp_pd_state) {
3128 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3129 nullptr == pQueueFamilyProperties);
3130 }
3131}
3132
3133void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3134 uint32_t* pQueueFamilyPropertyCount,
3135 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3136 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3137 pQueueFamilyProperties);
3138 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3139 if (bp_pd_state) {
3140 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3141 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003142 }
3143}
3144
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003145void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3146 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003147 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3148 if (bp_pd_state) {
3149 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3150 }
3151}
3152
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003153void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3154 VkPhysicalDeviceFeatures2* pFeatures) {
3155 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003156 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3157 if (bp_pd_state) {
3158 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3159 }
3160}
3161
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003162void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3163 VkPhysicalDeviceFeatures2* pFeatures) {
3164 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003165 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3166 if (bp_pd_state) {
3167 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3168 }
3169}
3170
3171void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3172 VkSurfaceKHR surface,
3173 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3174 VkResult result) {
3175 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3176 if (bp_pd_state) {
3177 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3178 }
3179}
3180
3181void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3182 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3183 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
3184 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3185 if (bp_pd_state) {
3186 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3187 }
3188}
3189
3190void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3191 VkSurfaceKHR surface,
3192 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3193 VkResult result) {
3194 auto* bp_pd_state = GetPhysicalDeviceStateBP(physicalDevice);
3195 if (bp_pd_state) {
3196 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3197 }
3198}
3199
3200void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3201 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3202 VkPresentModeKHR* pPresentModes, VkResult result) {
3203 auto* bp_pd_data = GetPhysicalDeviceStateBP(physicalDevice);
3204 if (bp_pd_data) {
3205 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3206
3207 if (*pPresentModeCount) {
3208 if (call_state < QUERY_COUNT) {
3209 call_state = QUERY_COUNT;
3210 }
3211 }
3212 if (pPresentModes) {
3213 if (call_state < QUERY_DETAILS) {
3214 call_state = QUERY_DETAILS;
3215 }
3216 }
3217 }
3218}
3219
3220void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3221 uint32_t* pSurfaceFormatCount,
3222 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
3223 auto* bp_pd_data = GetPhysicalDeviceStateBP(physicalDevice);
3224 if (bp_pd_data) {
3225 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3226
3227 if (*pSurfaceFormatCount) {
3228 if (call_state < QUERY_COUNT) {
3229 call_state = QUERY_COUNT;
3230 }
3231 }
3232 if (pSurfaceFormats) {
3233 if (call_state < QUERY_DETAILS) {
3234 call_state = QUERY_DETAILS;
3235 }
3236 }
3237 }
3238}
3239
3240void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3241 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3242 uint32_t* pSurfaceFormatCount,
3243 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
3244 auto* bp_pd_data = GetPhysicalDeviceStateBP(physicalDevice);
3245 if (bp_pd_data) {
3246 if (*pSurfaceFormatCount) {
3247 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3248 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3249 }
3250 }
3251 if (pSurfaceFormats) {
3252 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3253 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3254 }
3255 }
3256 }
3257}
3258
3259void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3260 uint32_t* pPropertyCount,
3261 VkDisplayPlanePropertiesKHR* pProperties,
3262 VkResult result) {
3263 auto* bp_pd_data = GetPhysicalDeviceStateBP(physicalDevice);
3264 if (bp_pd_data) {
3265 if (*pPropertyCount) {
3266 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3267 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3268 }
3269 }
3270 if (pProperties) {
3271 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3272 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3273 }
3274 }
3275 }
3276}
3277
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003278void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3279 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3280 VkResult result) {
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003281 auto* swapchain_state = static_cast<SWAPCHAIN_STATE_BP*>(Get<SWAPCHAIN_NODE>(swapchain));
3282 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3283 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3284 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003285 }
3286 }
3287}
3288
3289void BestPractices::ManualPostCallRecordEnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount,
3290 VkPhysicalDevice* pPhysicalDevices, VkResult result) {
3291 if ((nullptr != pPhysicalDevices) && ((result == VK_SUCCESS || result == VK_INCOMPLETE))) {
3292 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
3293 phys_device_bp_state_map.emplace(pPhysicalDevices[i], PHYSICAL_DEVICE_STATE_BP{});
3294 }
3295 }
3296}
3297
3298void BestPractices::ManualPostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo*, const VkAllocationCallbacks*,
3299 VkDevice*, VkResult result) {
3300 if (VK_SUCCESS == result) {
3301 instance_device_bp_state = &phys_device_bp_state_map[gpu];
3302 }
3303}
3304
3305PHYSICAL_DEVICE_STATE_BP* BestPractices::GetPhysicalDeviceStateBP(const VkPhysicalDevice& phys_device) {
3306 if (phys_device_bp_state_map.count(phys_device) > 0) {
3307 return &phys_device_bp_state_map.at(phys_device);
3308 } else {
3309 return nullptr;
3310 }
3311}
3312
3313const PHYSICAL_DEVICE_STATE_BP* BestPractices::GetPhysicalDeviceStateBP(const VkPhysicalDevice& phys_device) const {
3314 if (phys_device_bp_state_map.count(phys_device) > 0) {
3315 return &phys_device_bp_state_map.at(phys_device);
3316 } else {
3317 return nullptr;
3318 }
3319}
3320
3321PHYSICAL_DEVICE_STATE_BP* BestPractices::GetPhysicalDeviceStateBP() {
3322 auto bp_state = (reinterpret_cast<BestPractices*>(instance_state))->instance_device_bp_state;
3323 if (bp_state) {
3324 return bp_state;
3325 } else if (!bp_state && phys_device_bp_state_map.count(physical_device_state->phys_device) > 0) {
3326 return &phys_device_bp_state_map.at(physical_device_state->phys_device);
3327 } else {
3328 return nullptr;
3329 }
3330}
3331
3332const PHYSICAL_DEVICE_STATE_BP* BestPractices::GetPhysicalDeviceStateBP() const {
3333 auto bp_state = (reinterpret_cast<BestPractices*>(instance_state))->instance_device_bp_state;
3334 if (bp_state) {
3335 return bp_state;
3336 } else if (!bp_state && phys_device_bp_state_map.count(physical_device_state->phys_device) > 0) {
3337 return &phys_device_bp_state_map.at(physical_device_state->phys_device);
3338 } else {
3339 return nullptr;
3340 }
3341}
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003342
3343void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3344 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3345
3346 QUEUE_STATE* queue_state = GetQueueState(queue);
3347 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003348 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003349 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
3350 CMD_BUFFER_STATE* cb = GetCBState(submit_info.pCommandBuffers[cb_index]);
3351 for (auto &func : cb->queue_submit_functions) {
3352 func(this, queue_state);
3353 }
3354 }
3355 }
3356}