Mark Lobodzinski | 91e50bf | 2020-01-14 09:55:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2020 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2020 Valve Corporation |
| 3 | * Copyright (c) 2015-2020 LunarG, Inc. |
Camden | eaa86ea | 2019-07-26 11:00:09 -0600 | [diff] [blame] | 4 | * |
| 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 Lobodzinski | 57b8ae8 | 2020-02-20 16:37:14 -0700 | [diff] [blame] | 20 | #include "best_practices_validation.h" |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 21 | #include "layer_chassis_dispatch.h" |
Camden Stocker | 0a660ce | 2019-08-27 15:30:40 -0600 | [diff] [blame] | 22 | #include "best_practices_error_enums.h" |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | #include <iomanip> |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 26 | #include <bitset> |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 27 | |
| 28 | // get the API name is proper format |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 29 | std::string BestPractices::GetAPIVersionName(uint32_t version) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 30 | std::stringstream version_name; |
| 31 | uint32_t major = VK_VERSION_MAJOR(version); |
| 32 | uint32_t minor = VK_VERSION_MINOR(version); |
| 33 | uint32_t patch = VK_VERSION_PATCH(version); |
| 34 | |
| 35 | version_name << major << "." << minor << "." << patch << " (0x" << std::setfill('0') << std::setw(8) << std::hex << version |
| 36 | << ")"; |
| 37 | |
| 38 | return version_name.str(); |
| 39 | } |
| 40 | |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 41 | struct VendorSpecificInfo { |
| 42 | bool CHECK_ENABLED::*check; |
| 43 | std::string name; |
| 44 | }; |
| 45 | |
| 46 | const std::map<BPVendorFlagBits, VendorSpecificInfo> vendor_info = { |
| 47 | {kBPVendorArm, {&CHECK_ENABLED::vendor_specific_arm, "Arm"}}, |
| 48 | }; |
| 49 | |
| 50 | bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const { |
| 51 | for (const auto& vendor : vendor_info) { |
| 52 | if (vendors & vendor.first && enabled.*(vendor.second.check)) { |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | const char* VendorSpecificTag(BPVendorFlags vendors) { |
| 60 | // Cache built vendor tags in a map |
| 61 | static std::unordered_map<BPVendorFlags, std::string> tag_map; |
| 62 | |
| 63 | auto res = tag_map.find(vendors); |
| 64 | if (res == tag_map.end()) { |
| 65 | // Build the vendor tag string |
| 66 | std::stringstream vendor_tag; |
| 67 | |
| 68 | vendor_tag << "["; |
| 69 | bool first_vendor = true; |
| 70 | for (const auto& vendor : vendor_info) { |
| 71 | if (vendors & vendor.first) { |
| 72 | if (!first_vendor) { |
| 73 | vendor_tag << ", "; |
| 74 | } |
| 75 | vendor_tag << vendor.second.name; |
| 76 | first_vendor = false; |
| 77 | } |
| 78 | } |
| 79 | vendor_tag << "]"; |
| 80 | |
| 81 | tag_map[vendors] = vendor_tag.str(); |
| 82 | res = tag_map.find(vendors); |
| 83 | } |
| 84 | |
| 85 | return res->second.c_str(); |
| 86 | } |
| 87 | |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 88 | const char* DepReasonToString(ExtDeprecationReason reason) { |
| 89 | switch (reason) { |
| 90 | case kExtPromoted: |
| 91 | return "promoted to"; |
| 92 | break; |
| 93 | case kExtObsoleted: |
| 94 | return "obsoleted by"; |
| 95 | break; |
| 96 | case kExtDeprecated: |
| 97 | return "deprecated by"; |
| 98 | break; |
| 99 | default: |
| 100 | return ""; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version, |
| 106 | const char* vuid) const { |
| 107 | bool skip = false; |
| 108 | auto dep_info_it = deprecated_extensions.find(extension_name); |
| 109 | if (dep_info_it != deprecated_extensions.end()) { |
| 110 | auto dep_info = dep_info_it->second; |
| 111 | if ((dep_info.target.compare("VK_VERSION_1_1") && (version >= VK_VERSION_1_1)) || |
| 112 | (dep_info.target.compare("VK_VERSION_1_2") && (version >= VK_VERSION_1_2))) { |
| 113 | skip |= |
| 114 | LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.", |
| 115 | api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str()); |
| 116 | } else if (!dep_info.target.find("VK_VERSION")) { |
| 117 | if (dep_info.target.length() == 0) { |
| 118 | skip |= LogWarning(instance, vuid, |
| 119 | "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated " |
| 120 | "without replacement.", |
| 121 | api_name, extension_name); |
| 122 | } else { |
| 123 | skip |= LogWarning(instance, vuid, |
| 124 | "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.", |
| 125 | api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str()); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return skip; |
| 130 | } |
| 131 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 132 | bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 133 | VkInstance* pInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 134 | bool skip = false; |
| 135 | |
| 136 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
| 137 | if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) { |
Camden Stocker | 11ecf51 | 2020-01-21 16:06:49 -0800 | [diff] [blame] | 138 | skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 139 | "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.", |
| 140 | pCreateInfo->ppEnabledExtensionNames[i]); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 141 | } |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 142 | skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], |
| 143 | pCreateInfo->pApplicationInfo->apiVersion, |
| 144 | kVUID_BestPractices_CreateInstance_DeprecatedExtension); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return skip; |
| 148 | } |
| 149 | |
| 150 | void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, |
| 151 | VkInstance* pInstance) { |
Mark Lobodzinski | 97484d6 | 2020-03-03 11:57:41 -0700 | [diff] [blame] | 152 | ValidationStateTracker::PreCallRecordCreateInstance(pCreateInfo, pAllocator, pInstance); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 153 | instance_api_version = pCreateInfo->pApplicationInfo->apiVersion; |
| 154 | } |
| 155 | |
| 156 | bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 157 | const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 158 | bool skip = false; |
| 159 | |
| 160 | // get API version of physical device passed when creating device. |
| 161 | VkPhysicalDeviceProperties physical_device_properties{}; |
| 162 | DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties); |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 163 | auto device_api_version = physical_device_properties.apiVersion; |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 164 | |
| 165 | // check api versions and warn if instance api Version is higher than version on device. |
| 166 | if (instance_api_version > device_api_version) { |
| 167 | std::string inst_api_name = GetAPIVersionName(instance_api_version); |
| 168 | std::string dev_api_name = GetAPIVersionName(device_api_version); |
| 169 | |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 170 | skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch, |
| 171 | "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s", |
| 172 | inst_api_name.c_str(), dev_api_name.c_str()); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
| 176 | if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) { |
Camden Stocker | 11ecf51 | 2020-01-21 16:06:49 -0800 | [diff] [blame] | 177 | skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 178 | "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.", |
| 179 | pCreateInfo->ppEnabledExtensionNames[i]); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 180 | } |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 181 | skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], instance_api_version, |
| 182 | kVUID_BestPractices_CreateDevice_DeprecatedExtension); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 185 | auto pd_state = GetPhysicalDeviceState(physicalDevice); |
Cort | a48da1d | 2019-09-20 18:59:07 +0200 | [diff] [blame] | 186 | if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 187 | skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled, |
| 188 | "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures()."); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 189 | } |
| 190 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 191 | return skip; |
| 192 | } |
| 193 | |
| 194 | bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 195 | const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 196 | bool skip = false; |
| 197 | |
| 198 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
| 199 | std::stringstream bufferHex; |
| 200 | bufferHex << "0x" << std::hex << HandleToUint64(pBuffer); |
| 201 | |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 202 | skip |= LogWarning( |
| 203 | device, kVUID_BestPractices_SharingModeExclusive, |
| 204 | "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues " |
| 205 | "(queueFamilyIndexCount of %" PRIu32 ").", |
| 206 | bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return skip; |
| 210 | } |
| 211 | |
| 212 | bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 213 | const VkAllocationCallbacks* pAllocator, VkImage* pImage) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 214 | bool skip = false; |
| 215 | |
| 216 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
| 217 | std::stringstream imageHex; |
| 218 | imageHex << "0x" << std::hex << HandleToUint64(pImage); |
| 219 | |
| 220 | skip |= |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 221 | LogWarning(device, kVUID_BestPractices_SharingModeExclusive, |
| 222 | "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues " |
| 223 | "(queueFamilyIndexCount of %" PRIu32 ").", |
| 224 | imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 225 | } |
| 226 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 227 | if (VendorCheckEnabled(kBPVendorArm)) { |
| 228 | if (pCreateInfo->samples > kMaxEfficientSamplesArm) { |
| 229 | skip |= LogPerformanceWarning( |
| 230 | device, kVUID_BestPractices_CreateImage_TooLargeSampleCount, |
| 231 | "%s vkCreateImage(): Trying to create an image with %u samples. " |
| 232 | "The hardware revision may not have full throughput for framebuffers with more than %u samples.", |
| 233 | VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm); |
| 234 | } |
| 235 | |
| 236 | if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) { |
| 237 | skip |= LogPerformanceWarning( |
| 238 | device, kVUID_BestPractices_CreateImage_NonTransientMSImage, |
| 239 | "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have " |
| 240 | "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, " |
| 241 | "and do not need to be backed by physical storage. " |
| 242 | "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.", |
| 243 | VendorSpecificTag(kBPVendorArm)); |
| 244 | } |
| 245 | } |
| 246 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 247 | return skip; |
| 248 | } |
| 249 | |
| 250 | bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 251 | const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 252 | bool skip = false; |
| 253 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 254 | auto physical_device_state = GetPhysicalDeviceState(); |
| 255 | |
| 256 | if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 257 | skip |= LogWarning( |
| 258 | device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 259 | "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR()."); |
| 260 | } |
| 261 | |
| 262 | if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 263 | skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
| 264 | "vkCreateSwapchainKHR() called before getting surface present mode(s) from " |
| 265 | "vkGetPhysicalDeviceSurfacePresentModesKHR()."); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 269 | skip |= LogWarning( |
| 270 | device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
| 271 | "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR()."); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 272 | } |
| 273 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 274 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 275 | skip |= |
| 276 | LogWarning(device, kVUID_BestPractices_SharingModeExclusive, |
| 277 | "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while " |
| 278 | "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").", |
| 279 | pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return skip; |
| 283 | } |
| 284 | |
| 285 | bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount, |
| 286 | const VkSwapchainCreateInfoKHR* pCreateInfos, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 287 | const VkAllocationCallbacks* pAllocator, |
| 288 | VkSwapchainKHR* pSwapchains) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 289 | bool skip = false; |
| 290 | |
| 291 | for (uint32_t i = 0; i < swapchainCount; i++) { |
| 292 | if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 293 | skip |= LogWarning( |
| 294 | device, kVUID_BestPractices_SharingModeExclusive, |
| 295 | "Warning: A shared swapchain (index %" PRIu32 |
| 296 | ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple " |
| 297 | "queues (queueFamilyIndexCount of %" PRIu32 ").", |
| 298 | i, pCreateInfos[i].queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | return skip; |
| 303 | } |
| 304 | |
| 305 | bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 306 | const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 307 | bool skip = false; |
| 308 | |
| 309 | for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) { |
| 310 | VkFormat format = pCreateInfo->pAttachments[i].format; |
| 311 | if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) { |
| 312 | if ((FormatIsColor(format) || FormatHasDepth(format)) && |
| 313 | pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 314 | skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment, |
| 315 | "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and " |
| 316 | "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you " |
| 317 | "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the " |
| 318 | "image truely is undefined at the start of the render pass."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 319 | } |
| 320 | if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 321 | skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment, |
| 322 | "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD " |
| 323 | "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you " |
| 324 | "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the " |
| 325 | "image truely is undefined at the start of the render pass."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 326 | } |
| 327 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 328 | |
| 329 | const auto& attachment = pCreateInfo->pAttachments[i]; |
| 330 | if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) { |
| 331 | bool access_requires_memory = |
| 332 | attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE; |
| 333 | |
| 334 | if (FormatHasStencil(format)) { |
| 335 | access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD || |
| 336 | attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE; |
| 337 | } |
| 338 | |
| 339 | if (access_requires_memory) { |
| 340 | skip |= LogPerformanceWarning( |
| 341 | device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory, |
| 342 | "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp " |
| 343 | "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, " |
| 344 | "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.", |
| 345 | i, static_cast<uint32_t>(attachment.samples)); |
| 346 | } |
| 347 | } |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) { |
| 351 | skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask); |
| 352 | skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask); |
| 353 | } |
| 354 | |
| 355 | return skip; |
| 356 | } |
| 357 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 358 | bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, |
| 359 | const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const { |
| 360 | bool skip = false; |
| 361 | |
| 362 | // Check for non-transient attachments that should be transient and vice versa |
| 363 | auto rp_state = GetRenderPassState(pCreateInfo->renderPass); |
| 364 | if (rp_state) { |
| 365 | const VkRenderPassCreateInfo2* rpci = rp_state->createInfo.ptr(); |
| 366 | const VkImageView* image_views = pCreateInfo->pAttachments; |
| 367 | |
| 368 | for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) { |
| 369 | auto& attachment = rpci->pAttachments[i]; |
| 370 | bool attachment_should_be_transient = |
| 371 | (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE); |
| 372 | |
| 373 | if (FormatHasStencil(attachment.format)) { |
| 374 | attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD && |
| 375 | attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE); |
| 376 | } |
| 377 | |
| 378 | auto view_state = GetImageViewState(image_views[i]); |
| 379 | if (view_state) { |
| 380 | auto& ivci = view_state->create_info; |
| 381 | auto& ici = GetImageState(ivci.image)->createInfo; |
| 382 | |
| 383 | bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0; |
| 384 | |
| 385 | // The check for an image that should not be transient applies to all GPUs |
| 386 | if (!attachment_should_be_transient && image_is_transient) { |
| 387 | skip |= LogPerformanceWarning( |
| 388 | device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient, |
| 389 | "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, " |
| 390 | "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. " |
| 391 | "Physical memory will need to be backed lazily to this image, potentially causing stalls.", |
| 392 | i); |
| 393 | } |
| 394 | |
| 395 | bool supports_lazy = false; |
| 396 | for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) { |
| 397 | if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { |
| 398 | supports_lazy = true; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // The check for an image that should be transient only applies to GPUs supporting |
| 403 | // lazily allocated memory |
| 404 | if (supports_lazy && attachment_should_be_transient && !image_is_transient) { |
| 405 | skip |= LogPerformanceWarning( |
| 406 | device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient, |
| 407 | "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, " |
| 408 | "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. " |
| 409 | "You can save physical memory by using transient attachment backed by lazily allocated memory here.", |
| 410 | i); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return skip; |
| 417 | } |
| 418 | |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 419 | bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, |
| 420 | VkDescriptorSet* pDescriptorSets, void* ads_state_data) const { |
| 421 | bool skip = false; |
| 422 | skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data); |
| 423 | |
| 424 | if (!skip) { |
| 425 | const auto& pool_handle = pAllocateInfo->descriptorPool; |
| 426 | auto iter = descriptor_pool_freed_count.find(pool_handle); |
| 427 | // if the number of freed sets > 0, it implies they could be recycled instead if desirable |
| 428 | // this warning is specific to Arm |
| 429 | if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) { |
| 430 | skip |= LogPerformanceWarning( |
| 431 | device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse, |
| 432 | "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the " |
| 433 | "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.", |
| 434 | VendorSpecificTag(kBPVendorArm)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return skip; |
| 439 | } |
| 440 | |
| 441 | void BestPractices::PostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, |
| 442 | VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) { |
| 443 | ValidationStateTracker::PostCallRecordAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, result, ads_state); |
| 444 | |
| 445 | if (result == VK_SUCCESS) { |
| 446 | // find the free count for the pool we allocated into |
| 447 | auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool); |
| 448 | if (iter != descriptor_pool_freed_count.end()) { |
| 449 | // we record successful allocations by subtracting the allocation count from the last recorded free count |
| 450 | const auto alloc_count = pAllocateInfo->descriptorSetCount; |
| 451 | // clamp the unsigned subtraction to the range [0, last_free_count] |
| 452 | if (iter->second > alloc_count) |
| 453 | iter->second -= alloc_count; |
| 454 | else |
| 455 | iter->second = 0; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, |
| 461 | const VkDescriptorSet* pDescriptorSets, VkResult result) { |
| 462 | ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result); |
| 463 | if (result == VK_SUCCESS) { |
| 464 | // we want to track frees because we're interested in suggesting re-use |
| 465 | auto iter = descriptor_pool_freed_count.find(descriptorPool); |
| 466 | if (iter == descriptor_pool_freed_count.end()) { |
| 467 | descriptor_pool_freed_count.insert(std::make_pair(descriptorPool, descriptorSetCount)); |
| 468 | } else { |
| 469 | iter->second += descriptorSetCount; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 474 | bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 475 | const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 476 | bool skip = false; |
| 477 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 478 | if (num_mem_objects + 1 > kMemoryObjectWarningLimit) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 479 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects, |
| 480 | "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 481 | } |
| 482 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 483 | if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) { |
| 484 | skip |= LogPerformanceWarning( |
| 485 | device, kVUID_BestPractices_AllocateMemory_SmallAllocation, |
| 486 | "vkAllocateMemory(): Allocating a VkDeviceMemory of size %llu. This is a very small allocation (current " |
| 487 | "threshold is %llu bytes). " |
| 488 | "You should make large allocations and sub-allocate from one large VkDeviceMemory.", |
| 489 | pAllocateInfo->allocationSize, kMinDeviceAllocationSize); |
| 490 | } |
| 491 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 492 | // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker |
| 493 | |
| 494 | return skip; |
| 495 | } |
| 496 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 497 | void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, |
| 498 | const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory, |
| 499 | VkResult result) { |
Camden Stocker | 9738af9 | 2019-10-16 13:54:03 -0700 | [diff] [blame] | 500 | ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result); |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 501 | if (result != VK_SUCCESS) { |
| 502 | static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 503 | VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE, |
| 504 | VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR}; |
| 505 | static std::vector<VkResult> success_codes = {}; |
| 506 | ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes); |
| 507 | return; |
| 508 | } |
| 509 | num_mem_objects++; |
| 510 | } |
Camden Stocker | 9738af9 | 2019-10-16 13:54:03 -0700 | [diff] [blame] | 511 | |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 512 | void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& success_codes, |
| 513 | const std::vector<VkResult>& error_codes) const { |
| 514 | auto error = std::find(error_codes.begin(), error_codes.end(), result); |
| 515 | if (error != error_codes.end()) { |
| 516 | LogWarning(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned error %s.", api_name, string_VkResult(result)); |
| 517 | return; |
| 518 | } |
| 519 | auto success = std::find(success_codes.begin(), success_codes.end(), result); |
| 520 | if (success != success_codes.end()) { |
| 521 | LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned non-success return code %s.", api_name, |
| 522 | string_VkResult(result)); |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 526 | bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory, |
| 527 | const VkAllocationCallbacks* pAllocator) const { |
Mark Lobodzinski | 91e50bf | 2020-01-14 09:55:11 -0700 | [diff] [blame] | 528 | if (memory == VK_NULL_HANDLE) return false; |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 529 | bool skip = false; |
| 530 | |
Camden Stocker | 9738af9 | 2019-10-16 13:54:03 -0700 | [diff] [blame] | 531 | const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 532 | |
| 533 | for (auto& obj : mem_info->obj_bindings) { |
Mark Lobodzinski | 818425a | 2020-03-16 18:19:03 -0600 | [diff] [blame] | 534 | LogObjectList objlist(device); |
| 535 | objlist.add(obj); |
| 536 | objlist.add(mem_info->mem); |
| 537 | skip |= LogWarning(objlist, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.", |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 538 | report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem).c_str()); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 539 | } |
| 540 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 541 | return skip; |
| 542 | } |
| 543 | |
| 544 | void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) { |
Mark Lobodzinski | 97484d6 | 2020-03-03 11:57:41 -0700 | [diff] [blame] | 545 | ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 546 | if (memory != VK_NULL_HANDLE) { |
| 547 | num_mem_objects--; |
| 548 | } |
| 549 | } |
| 550 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 551 | bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const { |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 552 | bool skip = false; |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 553 | const BUFFER_STATE* buffer_state = GetBufferState(buffer); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 554 | |
sfricke-samsung | e244119 | 2019-11-06 14:07:57 -0800 | [diff] [blame] | 555 | if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 556 | skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled, |
| 557 | "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.", |
| 558 | api_name, report_data->FormatHandle(buffer).c_str()); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 559 | } |
| 560 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 561 | const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory); |
| 562 | |
| 563 | if (mem_state->alloc_info.allocationSize == buffer_state->createInfo.size && |
| 564 | mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) { |
| 565 | skip |= LogPerformanceWarning( |
| 566 | device, kVUID_BestPractices_SmallDedicatedAllocation, |
| 567 | "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. " |
| 568 | "The required size of the allocation is %llu, but smaller buffers like this should be sub-allocated from " |
| 569 | "larger memory blocks. (Current threshold is %llu bytes.)", |
| 570 | api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize); |
| 571 | } |
| 572 | |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 573 | return skip; |
| 574 | } |
| 575 | |
| 576 | bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 577 | VkDeviceSize memoryOffset) const { |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 578 | bool skip = false; |
| 579 | const char* api_name = "BindBufferMemory()"; |
| 580 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 581 | skip |= ValidateBindBufferMemory(buffer, memory, api_name); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 582 | |
| 583 | return skip; |
| 584 | } |
| 585 | |
| 586 | bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 587 | const VkBindBufferMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 588 | char api_name[64]; |
| 589 | bool skip = false; |
| 590 | |
| 591 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 592 | sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 593 | skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return skip; |
| 597 | } |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 598 | |
| 599 | bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 600 | const VkBindBufferMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 601 | char api_name[64]; |
| 602 | bool skip = false; |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 603 | |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 604 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 605 | sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 606 | skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | return skip; |
| 610 | } |
| 611 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 612 | bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 613 | bool skip = false; |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 614 | const IMAGE_STATE* image_state = GetImageState(image); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 615 | |
sfricke-samsung | d7ea5de | 2020-04-08 09:19:18 -0700 | [diff] [blame^] | 616 | if ((image_state->createInfo.flags & VK_IMAGE_CREATE_DISJOINT_BIT) == 0) { |
| 617 | if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) { |
| 618 | skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled, |
| 619 | "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.", |
| 620 | api_name, report_data->FormatHandle(image).c_str()); |
| 621 | } |
| 622 | } else { |
| 623 | // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each |
| 624 | // plane. |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 625 | } |
| 626 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 627 | const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory); |
| 628 | |
| 629 | if (mem_state->alloc_info.allocationSize == image_state->requirements.size && |
| 630 | mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) { |
| 631 | skip |= LogPerformanceWarning( |
| 632 | device, kVUID_BestPractices_SmallDedicatedAllocation, |
| 633 | "%s: Trying to bind %s to a memory block which is fully consumed by the image. " |
| 634 | "The required size of the allocation is %llu, but smaller images like this should be sub-allocated from " |
| 635 | "larger memory blocks. (Current threshold is %llu bytes.)", |
| 636 | api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize); |
| 637 | } |
| 638 | |
| 639 | // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation, |
| 640 | // make sure this type is actually used. |
| 641 | // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT |
| 642 | // (i.e.most tile - based renderers) |
| 643 | if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) { |
| 644 | bool supports_lazy = false; |
| 645 | uint32_t suggested_type = 0; |
| 646 | |
| 647 | for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) { |
| 648 | if ((1u << i) & image_state->requirements.memoryTypeBits) { |
| 649 | if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { |
| 650 | supports_lazy = true; |
| 651 | suggested_type = i; |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags; |
| 658 | |
| 659 | if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) { |
| 660 | skip |= LogPerformanceWarning( |
| 661 | device, kVUID_BestPractices_NonLazyTransientImage, |
| 662 | "%s: Attempting to bind memory type % u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT," |
| 663 | "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save " |
| 664 | "%llu bytes of physical memory.", |
| 665 | api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements.size); |
| 666 | } |
| 667 | } |
| 668 | |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 669 | return skip; |
| 670 | } |
| 671 | |
| 672 | bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 673 | VkDeviceSize memoryOffset) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 674 | bool skip = false; |
| 675 | const char* api_name = "vkBindImageMemory()"; |
| 676 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 677 | skip |= ValidateBindImageMemory(image, memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 678 | |
| 679 | return skip; |
| 680 | } |
| 681 | |
| 682 | bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 683 | const VkBindImageMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 684 | char api_name[64]; |
| 685 | bool skip = false; |
| 686 | |
| 687 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 688 | sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 689 | skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | return skip; |
| 693 | } |
| 694 | |
| 695 | bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 696 | const VkBindImageMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 697 | char api_name[64]; |
| 698 | bool skip = false; |
| 699 | |
| 700 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 701 | sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 702 | skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return skip; |
| 706 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 707 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 708 | static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) { |
| 709 | switch (format) { |
| 710 | case VK_FORMAT_B10G11R11_UFLOAT_PACK32: |
| 711 | case VK_FORMAT_R16_SFLOAT: |
| 712 | case VK_FORMAT_R16G16_SFLOAT: |
| 713 | case VK_FORMAT_R16G16B16_SFLOAT: |
| 714 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 715 | case VK_FORMAT_R32_SFLOAT: |
| 716 | case VK_FORMAT_R32G32_SFLOAT: |
| 717 | case VK_FORMAT_R32G32B32_SFLOAT: |
| 718 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 719 | return false; |
| 720 | |
| 721 | default: |
| 722 | return true; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount, |
| 727 | const VkGraphicsPipelineCreateInfo* pCreateInfos) const { |
| 728 | bool skip = false; |
| 729 | |
| 730 | for (uint32_t i = 0; i < createInfoCount; i++) { |
| 731 | auto pCreateInfo = &pCreateInfos[i]; |
| 732 | |
| 733 | if (!pCreateInfo->pColorBlendState || !pCreateInfo->pMultisampleState || |
| 734 | pCreateInfo->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT || |
| 735 | pCreateInfo->pMultisampleState->sampleShadingEnable) { |
| 736 | return skip; |
| 737 | } |
| 738 | |
| 739 | auto rp_state = GetRenderPassState(pCreateInfo->renderPass); |
| 740 | auto& subpass = rp_state->createInfo.pSubpasses[pCreateInfo->subpass]; |
| 741 | |
| 742 | for (uint32_t j = 0; j < pCreateInfo->pColorBlendState->attachmentCount; j++) { |
| 743 | auto& blend_att = pCreateInfo->pColorBlendState->pAttachments[j]; |
| 744 | uint32_t att = subpass.pColorAttachments[j].attachment; |
| 745 | |
| 746 | if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) { |
| 747 | if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) { |
| 748 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending, |
| 749 | "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and " |
| 750 | "color attachment #%u makes use " |
| 751 | "of a format which cannot be blended at full throughput when using MSAA.", |
| 752 | VendorSpecificTag(kBPVendorArm), i, j); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return skip; |
| 759 | } |
| 760 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 761 | bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 762 | const VkGraphicsPipelineCreateInfo* pCreateInfos, |
Mark Lobodzinski | 2a162a0 | 2019-09-06 11:02:12 -0600 | [diff] [blame] | 763 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 764 | void* cgpl_state_data) const { |
Mark Lobodzinski | 8317a3e | 2019-09-20 10:07:08 -0600 | [diff] [blame] | 765 | bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, |
| 766 | pAllocator, pPipelines, cgpl_state_data); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 767 | |
| 768 | if ((createInfoCount > 1) && (!pipelineCache)) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 769 | skip |= LogPerformanceWarning( |
| 770 | device, kVUID_BestPractices_CreatePipelines_MultiplePipelines, |
| 771 | "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a " |
| 772 | "pipeline cache, which may help with performance"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 773 | } |
| 774 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 775 | for (uint32_t i = 0; i < createInfoCount; i++) { |
| 776 | auto& createInfo = pCreateInfos[i]; |
| 777 | |
| 778 | auto& vertexInput = *createInfo.pVertexInputState; |
| 779 | uint32_t count = 0; |
| 780 | for (uint32_t j = 0; j < vertexInput.vertexBindingDescriptionCount; j++) { |
| 781 | if (vertexInput.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) { |
| 782 | count++; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | if (count > kMaxInstancedVertexBuffers) { |
| 787 | skip |= LogPerformanceWarning( |
| 788 | device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers, |
| 789 | "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the " |
| 790 | "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.", |
| 791 | count, kMaxInstancedVertexBuffers); |
| 792 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 793 | |
| 794 | skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos); |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 797 | return skip; |
| 798 | } |
| 799 | |
| 800 | bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 801 | const VkComputePipelineCreateInfo* pCreateInfos, |
Mark Lobodzinski | 2a162a0 | 2019-09-06 11:02:12 -0600 | [diff] [blame] | 802 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 803 | void* ccpl_state_data) const { |
Mark Lobodzinski | 8317a3e | 2019-09-20 10:07:08 -0600 | [diff] [blame] | 804 | bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, |
| 805 | pAllocator, pPipelines, ccpl_state_data); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 806 | |
| 807 | if ((createInfoCount > 1) && (!pipelineCache)) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 808 | skip |= LogPerformanceWarning( |
| 809 | device, kVUID_BestPractices_CreatePipelines_MultiplePipelines, |
| 810 | "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a " |
| 811 | "pipeline cache, which may help with performance"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return skip; |
| 815 | } |
| 816 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 817 | bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 818 | bool skip = false; |
| 819 | |
| 820 | if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 821 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 822 | "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str()); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 823 | } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 824 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 825 | "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str()); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | return skip; |
| 829 | } |
| 830 | |
Mark Lobodzinski | 9b133c1 | 2020-03-10 10:42:56 -0600 | [diff] [blame] | 831 | void BestPractices::PostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) { |
| 832 | ValidationStateTracker::PostCallRecordQueuePresentKHR(queue, pPresentInfo, result); |
| 833 | for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) { |
| 834 | auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result; |
| 835 | if (swapchains_result == VK_SUBOPTIMAL_KHR) { |
| 836 | LogPerformanceWarning( |
| 837 | pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain, |
| 838 | "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, " |
| 839 | "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it " |
| 840 | "targets. Applications should query updated surface information and recreate their swapchain at the next " |
| 841 | "convenient opportunity.", |
| 842 | report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str()); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 847 | bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, |
| 848 | VkFence fence) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 849 | bool skip = false; |
| 850 | |
| 851 | for (uint32_t submit = 0; submit < submitCount; submit++) { |
| 852 | for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) { |
| 853 | skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return skip; |
| 858 | } |
| 859 | |
Attilio Provenzano | 746e43e | 2020-02-27 11:23:50 +0000 | [diff] [blame] | 860 | bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo, |
| 861 | const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const { |
| 862 | bool skip = false; |
| 863 | |
| 864 | if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) { |
| 865 | skip |= LogPerformanceWarning( |
| 866 | device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset, |
| 867 | "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire " |
| 868 | "pool instead."); |
| 869 | } |
| 870 | |
| 871 | return skip; |
| 872 | } |
| 873 | |
| 874 | bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer, |
| 875 | const VkCommandBufferBeginInfo* pBeginInfo) const { |
| 876 | bool skip = false; |
| 877 | |
| 878 | if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) { |
| 879 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse, |
| 880 | "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set."); |
| 881 | } |
| 882 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 883 | if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) { |
| 884 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 885 | LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit, |
| 886 | "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. " |
| 887 | "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.", |
| 888 | VendorSpecificTag(kBPVendorArm)); |
| 889 | } |
| 890 | |
Attilio Provenzano | 746e43e | 2020-02-27 11:23:50 +0000 | [diff] [blame] | 891 | return skip; |
| 892 | } |
| 893 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 894 | bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 895 | bool skip = false; |
| 896 | |
| 897 | skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask); |
| 898 | |
| 899 | return skip; |
| 900 | } |
| 901 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 902 | bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, |
| 903 | VkPipelineStageFlags stageMask) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 904 | bool skip = false; |
| 905 | |
| 906 | skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask); |
| 907 | |
| 908 | return skip; |
| 909 | } |
| 910 | |
| 911 | bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents, |
| 912 | VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, |
| 913 | uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, |
| 914 | uint32_t bufferMemoryBarrierCount, |
| 915 | const VkBufferMemoryBarrier* pBufferMemoryBarriers, |
| 916 | uint32_t imageMemoryBarrierCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 917 | const VkImageMemoryBarrier* pImageMemoryBarriers) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 918 | bool skip = false; |
| 919 | |
| 920 | skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask); |
| 921 | skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask); |
| 922 | |
| 923 | return skip; |
| 924 | } |
| 925 | |
| 926 | bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 927 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 928 | uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, |
| 929 | uint32_t bufferMemoryBarrierCount, |
| 930 | const VkBufferMemoryBarrier* pBufferMemoryBarriers, |
| 931 | uint32_t imageMemoryBarrierCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 932 | const VkImageMemoryBarrier* pImageMemoryBarriers) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 933 | bool skip = false; |
| 934 | |
| 935 | skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask); |
| 936 | skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask); |
| 937 | |
| 938 | return skip; |
| 939 | } |
| 940 | |
| 941 | bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 942 | VkQueryPool queryPool, uint32_t query) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 943 | bool skip = false; |
| 944 | |
| 945 | skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage); |
| 946 | |
| 947 | return skip; |
| 948 | } |
| 949 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 950 | static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) { |
| 951 | for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) { |
| 952 | auto& subpassInfo = createInfo.pSubpasses[subpass]; |
| 953 | |
| 954 | // If an attachment is ever used as a color attachment, |
| 955 | // resolve attachment or depth stencil attachment, |
| 956 | // it needs to exist on tile at some point. |
| 957 | |
| 958 | for (uint32_t i = 0; i < subpassInfo.colorAttachmentCount; i++) |
| 959 | if (subpassInfo.pColorAttachments[i].attachment == attachment) return true; |
| 960 | |
| 961 | if (subpassInfo.pResolveAttachments) { |
| 962 | for (uint32_t i = 0; i < subpassInfo.colorAttachmentCount; i++) |
| 963 | if (subpassInfo.pResolveAttachments[i].attachment == attachment) return true; |
| 964 | } |
| 965 | |
| 966 | if (subpassInfo.pDepthStencilAttachment && subpassInfo.pDepthStencilAttachment->attachment == attachment) return true; |
| 967 | } |
| 968 | |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version, |
| 973 | const VkRenderPassBeginInfo* pRenderPassBegin) const { |
| 974 | bool skip = false; |
| 975 | |
| 976 | if (!pRenderPassBegin) { |
| 977 | return skip; |
| 978 | } |
| 979 | |
| 980 | auto rp_state = GetRenderPassState(pRenderPassBegin->renderPass); |
| 981 | if (rp_state) { |
| 982 | // Check if any attachments have LOAD operation on them |
| 983 | for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) { |
| 984 | auto& attachment = rp_state->createInfo.pAttachments[att]; |
| 985 | |
| 986 | bool attachmentHasReadback = false; |
| 987 | if (!FormatHasStencil(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 988 | attachmentHasReadback = true; |
| 989 | } |
| 990 | |
| 991 | if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 992 | attachmentHasReadback = true; |
| 993 | } |
| 994 | |
| 995 | bool attachmentNeedsReadback = false; |
| 996 | |
| 997 | // Check if the attachment is actually used in any subpass on-tile |
| 998 | if (attachmentHasReadback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) { |
| 999 | attachmentNeedsReadback = true; |
| 1000 | } |
| 1001 | |
| 1002 | // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement |
| 1003 | if (attachmentNeedsReadback) { |
| 1004 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 1005 | LogPerformanceWarning( |
| 1006 | device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback, |
| 1007 | "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n" |
| 1008 | "Submitting this renderpass will cause the driver to inject a readback of the attachment " |
| 1009 | "which will copy in total %u pixels (renderArea = { %d, %d, %u, %u }) to the tile buffer.", |
| 1010 | VendorSpecificTag(kBPVendorArm), att, |
| 1011 | pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height, |
| 1012 | pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y, |
| 1013 | pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | return skip; |
| 1019 | } |
| 1020 | |
| 1021 | bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
| 1022 | VkSubpassContents contents) const { |
| 1023 | bool skip = ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin); |
| 1024 | return skip; |
| 1025 | } |
| 1026 | |
| 1027 | bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 1028 | const VkRenderPassBeginInfo* pRenderPassBegin, |
| 1029 | const VkSubpassBeginInfoKHR* pSubpassBeginInfo) const { |
| 1030 | bool skip = ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
| 1031 | return skip; |
| 1032 | } |
| 1033 | |
| 1034 | bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
| 1035 | const VkSubpassBeginInfoKHR* pSubpassBeginInfo) const { |
| 1036 | bool skip = ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
| 1037 | return skip; |
| 1038 | } |
| 1039 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1040 | // Generic function to handle validation for all CmdDraw* type functions |
| 1041 | bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const { |
| 1042 | bool skip = false; |
| 1043 | const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer); |
| 1044 | if (cb_state) { |
| 1045 | const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1046 | const PIPELINE_STATE* pipeline_state = nullptr; |
| 1047 | if (last_bound_it != cb_state->lastBound.cend()) { |
| 1048 | pipeline_state = last_bound_it->second.pipeline_state; |
| 1049 | } |
| 1050 | const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings; |
| 1051 | // Verify vertex binding |
| 1052 | if (pipeline_state->vertex_binding_descriptions_.size() <= 0) { |
| 1053 | if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 1054 | skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds, |
| 1055 | "Vertex buffers are bound to %s but no vertex buffers are attached to %s.", |
| 1056 | report_data->FormatHandle(cb_state->commandBuffer).c_str(), |
| 1057 | report_data->FormatHandle(pipeline_state->pipeline).c_str()); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | return skip; |
| 1062 | } |
| 1063 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1064 | bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1065 | uint32_t firstVertex, uint32_t firstInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1066 | bool skip = false; |
| 1067 | |
| 1068 | if (instanceCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1069 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero, |
| 1070 | "Warning: You are calling vkCmdDraw() with an instanceCount of Zero."); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1071 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | return skip; |
| 1075 | } |
| 1076 | |
| 1077 | bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1078 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1079 | bool skip = false; |
| 1080 | |
| 1081 | if (instanceCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1082 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero, |
| 1083 | "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1084 | } |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1085 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()"); |
| 1086 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1087 | // Check if we reached the limit for small indexed draw calls. |
| 1088 | // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed. |
| 1089 | const CMD_BUFFER_STATE* cmd_state = GetCBState(commandBuffer); |
| 1090 | if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices && |
| 1091 | (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1)) { |
| 1092 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 1093 | LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls, |
| 1094 | "The command buffer contains many small indexed drawcalls " |
| 1095 | "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. " |
| 1096 | "You can try batching drawcalls or instancing when applicable.", |
| 1097 | VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices); |
| 1098 | } |
| 1099 | |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 1100 | if (VendorCheckEnabled(kBPVendorArm)) { |
| 1101 | ValidateIndexBufferArm(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); |
| 1102 | } |
| 1103 | |
| 1104 | return skip; |
| 1105 | } |
| 1106 | |
| 1107 | bool BestPractices::ValidateIndexBufferArm(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 1108 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const { |
| 1109 | bool skip = false; |
| 1110 | |
| 1111 | // check for sparse/underutilised index buffer, and post-transform cache thrashing |
| 1112 | const auto* cmd_state = GetCBState(commandBuffer); |
| 1113 | if (cmd_state == nullptr) return skip; |
| 1114 | |
| 1115 | const auto* ib_state = GetBufferState(cmd_state->index_buffer_binding.buffer); |
| 1116 | if (ib_state == nullptr) return skip; |
| 1117 | |
| 1118 | const VkIndexType ib_type = cmd_state->index_buffer_binding.index_type; |
| 1119 | const auto& ib_mem_state = *ib_state->binding.mem_state; |
| 1120 | const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset; |
| 1121 | const void* ib_mem = ib_mem_state.p_driver_data; |
| 1122 | bool primitive_restart_enable = false; |
| 1123 | |
| 1124 | const auto& pipeline_binding_iter = cmd_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1125 | |
| 1126 | if (pipeline_binding_iter != cmd_state->lastBound.end()) { |
| 1127 | const auto* pipeline_state = pipeline_binding_iter->second.pipeline_state; |
| 1128 | if (pipeline_state != nullptr && pipeline_state->graphicsPipelineCI.pInputAssemblyState != nullptr) |
| 1129 | primitive_restart_enable = pipeline_state->graphicsPipelineCI.pInputAssemblyState->primitiveRestartEnable == VK_TRUE; |
| 1130 | } |
| 1131 | |
| 1132 | // no point checking index buffer if the memory is nonexistant/unmapped, or if there is no graphics pipeline bound to this CB |
| 1133 | if (ib_mem && pipeline_binding_iter != cmd_state->lastBound.end()) { |
| 1134 | uint32_t scan_stride; |
| 1135 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 1136 | scan_stride = sizeof(uint8_t); |
| 1137 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 1138 | scan_stride = sizeof(uint16_t); |
| 1139 | } else { |
| 1140 | scan_stride = sizeof(uint32_t); |
| 1141 | } |
| 1142 | |
| 1143 | const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride; |
| 1144 | const uint8_t* scan_end = scan_begin + indexCount * scan_stride; |
| 1145 | |
| 1146 | // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all |
| 1147 | // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded, |
| 1148 | // irrespective of whether or not they're part of the draw call. |
| 1149 | |
| 1150 | // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer |
| 1151 | uint32_t min_index = ~0u; |
| 1152 | // start with maximum as 0 and adjust to indices in the buffer |
| 1153 | uint32_t max_index = 0u; |
| 1154 | |
| 1155 | // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded |
| 1156 | // for the given index buffer |
| 1157 | uint32_t vertex_shade_count = 0; |
| 1158 | |
| 1159 | PostTransformLRUCacheModel post_transform_cache; |
| 1160 | |
| 1161 | // The size of the cache being modelled positively correlates with how much behaviour it can capture about |
| 1162 | // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the |
| 1163 | // target architecture. |
| 1164 | // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice. |
| 1165 | // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html |
| 1166 | post_transform_cache.resize(32); |
| 1167 | |
| 1168 | for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) { |
| 1169 | uint32_t scan_index; |
| 1170 | uint32_t primitive_restart_value; |
| 1171 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 1172 | scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr); |
| 1173 | primitive_restart_value = 0xFF; |
| 1174 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 1175 | scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr); |
| 1176 | primitive_restart_value = 0xFFFF; |
| 1177 | } else { |
| 1178 | scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr); |
| 1179 | primitive_restart_value = 0xFFFFFFFF; |
| 1180 | } |
| 1181 | |
| 1182 | max_index = std::max(max_index, scan_index); |
| 1183 | min_index = std::min(min_index, scan_index); |
| 1184 | |
| 1185 | if (!primitive_restart_enable || scan_index != primitive_restart_value) { |
| 1186 | bool in_cache = post_transform_cache.query_cache(scan_index); |
| 1187 | // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again |
| 1188 | if (!in_cache) vertex_shade_count++; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // if the max and min values were not set, then we either have no indices, or all primitive restarts, exit... |
| 1193 | if (max_index < min_index) return skip; |
| 1194 | |
| 1195 | if (max_index - min_index >= indexCount) { |
| 1196 | skip |= LogPerformanceWarning( |
| 1197 | device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer, |
| 1198 | "%s The indices which were specified for the draw call only utilise approximately %.02f%% of " |
| 1199 | "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven " |
| 1200 | "Vertex Shading), meaning all vertices corresponding to indices between the minimum and " |
| 1201 | "maximum would be loaded, and possibly shaded, whether or not they are used.", |
| 1202 | VendorSpecificTag(kBPVendorArm), (static_cast<float>(indexCount) / (max_index - min_index)) * 100.0f); |
| 1203 | return skip; |
| 1204 | } |
| 1205 | |
| 1206 | // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call |
| 1207 | // each bit of the n-th bucket contains the inclusion information for indices (n*n_buckets) to ((n+1)*n_buckets) |
| 1208 | const size_t n_buckets = 64; |
| 1209 | std::vector<std::bitset<n_buckets>> vertex_reference_buckets; |
| 1210 | vertex_reference_buckets.resize((max_index - min_index + 1) / n_buckets); |
| 1211 | |
| 1212 | // To avoid using too much memory, we run over the indices again. |
| 1213 | // Knowing the size from the last scan allows us to record index usage with bitsets |
| 1214 | for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) { |
| 1215 | uint32_t scan_index; |
| 1216 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 1217 | scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr); |
| 1218 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 1219 | scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr); |
| 1220 | } else { |
| 1221 | scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr); |
| 1222 | } |
| 1223 | // keep track of the set of all indices used to reference vertices in the draw call |
| 1224 | size_t index_offset = scan_index - min_index; |
| 1225 | size_t bitset_bucket_index = index_offset / n_buckets; |
| 1226 | uint64_t used_indices = 1ull << ((index_offset % n_buckets) & 0xFFFFFFFFu); |
| 1227 | vertex_reference_buckets[bitset_bucket_index] |= used_indices; |
| 1228 | } |
| 1229 | |
| 1230 | uint32_t vertex_reference_count = 0; |
| 1231 | for (const auto& bitset : vertex_reference_buckets) { |
| 1232 | vertex_reference_count += static_cast<uint32_t>(bitset.count()); |
| 1233 | } |
| 1234 | |
| 1235 | // low index buffer utilization implies that: of the vertices available to the draw call, not all are utilized |
| 1236 | float utilization = static_cast<float>(vertex_reference_count) / (max_index - min_index + 1); |
| 1237 | // low hit rate (high miss rate) implies the order of indices in the draw call may be possible to improve |
| 1238 | float cache_hit_rate = static_cast<float>(vertex_reference_count) / vertex_shade_count; |
| 1239 | |
| 1240 | if (utilization < 0.5f) { |
| 1241 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer, |
| 1242 | "%s The indices which were specified for the draw call only utilise approximately " |
| 1243 | "%.02f%% of the bound vertex buffer.", |
| 1244 | VendorSpecificTag(kBPVendorArm), utilization); |
| 1245 | } |
| 1246 | |
| 1247 | if (cache_hit_rate <= 0.5f) { |
| 1248 | skip |= |
| 1249 | LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing, |
| 1250 | "%s The indices which were specified for the draw call are estimated to cause thrashing of " |
| 1251 | "the post-transform vertex cache, with a hit-rate of %.02f%%. " |
| 1252 | "I.e. the ordering of the index buffer may not make optimal use of indices associated with " |
| 1253 | "recently shaded vertices.", |
| 1254 | VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f); |
| 1255 | } |
| 1256 | } |
| 1257 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1258 | return skip; |
| 1259 | } |
| 1260 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1261 | void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 1262 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
| 1263 | ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, |
| 1264 | firstInstance); |
| 1265 | |
| 1266 | CMD_BUFFER_STATE* cmd_state = GetCBState(commandBuffer); |
| 1267 | if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) { |
| 1268 | cmd_state->small_indexed_draw_call_count++; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1272 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 1273 | VkDeviceSize offset, VkBuffer countBuffer, |
| 1274 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 1275 | uint32_t stride) const { |
| 1276 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1277 | |
| 1278 | return skip; |
| 1279 | } |
| 1280 | |
| 1281 | bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1282 | uint32_t drawCount, uint32_t stride) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1283 | bool skip = false; |
| 1284 | |
| 1285 | if (drawCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1286 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero, |
| 1287 | "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero."); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1288 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | return skip; |
| 1292 | } |
| 1293 | |
| 1294 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1295 | uint32_t drawCount, uint32_t stride) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1296 | bool skip = false; |
| 1297 | |
| 1298 | if (drawCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1299 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero, |
| 1300 | "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero."); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 1301 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | return skip; |
| 1305 | } |
| 1306 | |
| 1307 | bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1308 | uint32_t groupCountZ) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1309 | bool skip = false; |
| 1310 | |
| 1311 | if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1312 | skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero, |
| 1313 | "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32 |
| 1314 | ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").", |
| 1315 | groupCountX, groupCountY, groupCountZ); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | return skip; |
| 1319 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1320 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 1321 | bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice, |
| 1322 | const char* api_name) const { |
| 1323 | bool skip = false; |
| 1324 | const auto physical_device_state = GetPhysicalDeviceState(physicalDevice); |
| 1325 | |
| 1326 | if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1327 | skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled, |
| 1328 | "Potential problem with calling %s() without first retrieving properties from " |
| 1329 | "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.", |
| 1330 | api_name); |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | return skip; |
| 1334 | } |
| 1335 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1336 | bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1337 | uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const { |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1338 | bool skip = false; |
| 1339 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 1340 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR"); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1341 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 1342 | return skip; |
| 1343 | } |
| 1344 | |
| 1345 | bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode, |
| 1346 | uint32_t planeIndex, |
| 1347 | VkDisplayPlaneCapabilitiesKHR* pCapabilities) const { |
| 1348 | bool skip = false; |
| 1349 | |
| 1350 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR"); |
| 1351 | |
| 1352 | return skip; |
| 1353 | } |
| 1354 | |
| 1355 | bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice, |
| 1356 | const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo, |
| 1357 | VkDisplayPlaneCapabilities2KHR* pCapabilities) const { |
| 1358 | bool skip = false; |
| 1359 | |
| 1360 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR"); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1361 | |
| 1362 | return skip; |
| 1363 | } |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1364 | |
| 1365 | bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1366 | VkImage* pSwapchainImages) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1367 | bool skip = false; |
| 1368 | |
| 1369 | auto swapchain_state = GetSwapchainState(swapchain); |
| 1370 | |
| 1371 | if (swapchain_state && pSwapchainImages) { |
| 1372 | // Compare the preliminary value of *pSwapchainImageCount with the value this time: |
| 1373 | if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1374 | skip |= |
| 1375 | LogWarning(device, kVUID_Core_Swapchain_PriorCount, |
| 1376 | "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has " |
| 1377 | "been seen for pSwapchainImages."); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | return skip; |
| 1382 | } |
| 1383 | |
| 1384 | // Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1385 | bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state, |
| 1386 | uint32_t requested_queue_family_property_count, |
| 1387 | bool qfp_null, const char* caller_name) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1388 | bool skip = false; |
| 1389 | if (!qfp_null) { |
| 1390 | // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count |
| 1391 | if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1392 | skip |= LogWarning( |
| 1393 | pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount, |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1394 | "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended " |
| 1395 | "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.", |
| 1396 | caller_name, caller_name); |
| 1397 | // Then verify that pCount that is passed in on second call matches what was returned |
| 1398 | } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1399 | skip |= LogWarning( |
| 1400 | pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch, |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1401 | "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32 |
| 1402 | ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32 |
| 1403 | ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was " |
| 1404 | "previously obtained by calling %s with NULL pQueueFamilyProperties.", |
| 1405 | caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | return skip; |
| 1410 | } |
| 1411 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1412 | bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV( |
| 1413 | VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const { |
Camden Stocker | 8251058 | 2019-09-03 14:00:16 -0600 | [diff] [blame] | 1414 | bool skip = false; |
| 1415 | |
| 1416 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 1417 | const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure); |
| 1418 | if (!as_state->memory_requirements_checked) { |
| 1419 | // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling |
| 1420 | // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with |
| 1421 | // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1422 | skip |= LogWarning( |
| 1423 | device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery, |
Camden Stocker | 8251058 | 2019-09-03 14:00:16 -0600 | [diff] [blame] | 1424 | "vkBindAccelerationStructureMemoryNV(): " |
| 1425 | "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.", |
| 1426 | report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str()); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | return skip; |
| 1431 | } |
| 1432 | |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1433 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, |
| 1434 | uint32_t* pQueueFamilyPropertyCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1435 | VkQueueFamilyProperties* pQueueFamilyProperties) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1436 | const auto physical_device_state = GetPhysicalDeviceState(physicalDevice); |
| 1437 | assert(physical_device_state); |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1438 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount, |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1439 | (nullptr == pQueueFamilyProperties), |
| 1440 | "vkGetPhysicalDeviceQueueFamilyProperties()"); |
| 1441 | } |
| 1442 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1443 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2( |
| 1444 | VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, |
| 1445 | VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1446 | const auto physical_device_state = GetPhysicalDeviceState(physicalDevice); |
| 1447 | assert(physical_device_state); |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1448 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount, |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1449 | (nullptr == pQueueFamilyProperties), |
| 1450 | "vkGetPhysicalDeviceQueueFamilyProperties2()"); |
| 1451 | } |
| 1452 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1453 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR( |
| 1454 | VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, |
| 1455 | VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1456 | auto physical_device_state = GetPhysicalDeviceState(physicalDevice); |
| 1457 | assert(physical_device_state); |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1458 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount, |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1459 | (nullptr == pQueueFamilyProperties), |
| 1460 | "vkGetPhysicalDeviceQueueFamilyProperties2KHR()"); |
| 1461 | } |
| 1462 | |
| 1463 | bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, |
| 1464 | uint32_t* pSurfaceFormatCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1465 | VkSurfaceFormatKHR* pSurfaceFormats) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1466 | if (!pSurfaceFormats) return false; |
| 1467 | const auto physical_device_state = GetPhysicalDeviceState(physicalDevice); |
| 1468 | const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState; |
| 1469 | bool skip = false; |
| 1470 | if (call_state == UNCALLED) { |
| 1471 | // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't |
| 1472 | // previously call this function with a NULL value of pSurfaceFormats: |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1473 | skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount, |
| 1474 | "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior " |
| 1475 | "positive value has been seen for pSurfaceFormats."); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1476 | } else { |
| 1477 | auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size(); |
Peter Chen | e191bd7 | 2019-09-16 13:04:37 -0400 | [diff] [blame] | 1478 | if (*pSurfaceFormatCount > prev_format_count) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1479 | skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch, |
| 1480 | "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with " |
| 1481 | "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned " |
| 1482 | "when pSurfaceFormatCount was NULL.", |
| 1483 | *pSurfaceFormatCount, prev_format_count); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | return skip; |
| 1487 | } |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1488 | |
| 1489 | bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1490 | VkFence fence) const { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1491 | bool skip = false; |
| 1492 | |
| 1493 | for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) { |
| 1494 | const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx]; |
| 1495 | // Store sparse binding image_state and after binding is complete make sure that any requiring metadata have it bound |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 1496 | std::unordered_set<const IMAGE_STATE*> sparse_images; |
| 1497 | // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state |
| 1498 | // in RecordQueueBindSparse. |
| 1499 | std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata; |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1500 | // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound |
| 1501 | for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) { |
| 1502 | const auto& image_bind = bindInfo.pImageBinds[i]; |
| 1503 | auto image_state = GetImageState(image_bind.image); |
| 1504 | if (!image_state) |
| 1505 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
| 1506 | sparse_images.insert(image_state); |
| 1507 | if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) { |
| 1508 | if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) { |
| 1509 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1510 | skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState, |
| 1511 | "vkQueueBindSparse(): Binding sparse memory to %s without first calling " |
| 1512 | "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.", |
| 1513 | report_data->FormatHandle(image_state->image).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | if (!image_state->memory_requirements_checked) { |
| 1517 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1518 | skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState, |
| 1519 | "vkQueueBindSparse(): Binding sparse memory to %s without first calling " |
| 1520 | "vkGetImageMemoryRequirements() to retrieve requirements.", |
| 1521 | report_data->FormatHandle(image_state->image).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) { |
| 1525 | const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i]; |
| 1526 | auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image); |
| 1527 | if (!image_state) |
| 1528 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
| 1529 | sparse_images.insert(image_state); |
| 1530 | if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) { |
| 1531 | if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) { |
| 1532 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1533 | skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState, |
| 1534 | "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling " |
| 1535 | "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.", |
| 1536 | report_data->FormatHandle(image_state->image).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | if (!image_state->memory_requirements_checked) { |
| 1540 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1541 | skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState, |
| 1542 | "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling " |
| 1543 | "vkGetImageMemoryRequirements() to retrieve requirements.", |
| 1544 | report_data->FormatHandle(image_state->image).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1545 | } |
| 1546 | for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) { |
| 1547 | if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) { |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 1548 | sparse_images_with_metadata.insert(image_state); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | for (const auto& sparse_image_state : sparse_images) { |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 1553 | if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound && |
| 1554 | sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1555 | // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1556 | skip |= LogWarning(sparse_image_state->image, kVUID_Core_MemTrack_InvalidState, |
| 1557 | "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no " |
| 1558 | "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.", |
| 1559 | report_data->FormatHandle(sparse_image_state->image).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | return skip; |
| 1565 | } |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 1566 | |
| 1567 | void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo, |
| 1568 | VkFence fence, VkResult result) { |
Mark Lobodzinski | 97484d6 | 2020-03-03 11:57:41 -0700 | [diff] [blame] | 1569 | ValidationStateTracker::PostCallRecordQueueBindSparse(queue, bindInfoCount, pBindInfo, fence, result); |
| 1570 | |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 1571 | if (result != VK_SUCCESS) { |
| 1572 | static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 1573 | VK_ERROR_DEVICE_LOST}; |
| 1574 | static std::vector<VkResult> success_codes = {}; |
| 1575 | ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes); |
| 1576 | return; |
| 1577 | } |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 1578 | |
| 1579 | for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) { |
| 1580 | const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx]; |
| 1581 | for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) { |
| 1582 | const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i]; |
| 1583 | auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image); |
| 1584 | if (!image_state) |
| 1585 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
| 1586 | for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) { |
| 1587 | if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) { |
| 1588 | image_state->sparse_metadata_bound = true; |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | } |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 1594 | |
| 1595 | bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount, |
Camden Stocker | f55721f | 2019-09-09 11:04:49 -0600 | [diff] [blame] | 1596 | const VkClearAttachment* pAttachments, uint32_t rectCount, |
| 1597 | const VkClearRect* pRects) const { |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 1598 | bool skip = false; |
| 1599 | const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer); |
| 1600 | if (!cb_node) return skip; |
| 1601 | |
Camden Stocker | f55721f | 2019-09-09 11:04:49 -0600 | [diff] [blame] | 1602 | // Warn if this is issued prior to Draw Cmd and clearing the entire attachment |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 1603 | if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) && |
| 1604 | (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) { |
| 1605 | // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass) |
| 1606 | // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call |
| 1607 | // CmdClearAttachments. |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 1608 | skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw, |
| 1609 | "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you " |
| 1610 | "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.", |
| 1611 | report_data->FormatHandle(commandBuffer).c_str()); |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1614 | // Check for uses of ClearAttachments along with LOAD_OP_LOAD, |
| 1615 | // as it can be more efficient to just use LOAD_OP_CLEAR |
| 1616 | const RENDER_PASS_STATE* rp = cb_node->activeRenderPass; |
| 1617 | if (rp) { |
| 1618 | const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass]; |
| 1619 | |
| 1620 | for (uint32_t i = 0; i < attachmentCount; i++) { |
| 1621 | auto& attachment = pAttachments[i]; |
| 1622 | if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| 1623 | uint32_t color_attachment = attachment.colorAttachment; |
| 1624 | uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment; |
| 1625 | |
| 1626 | if (fb_attachment != VK_ATTACHMENT_UNUSED) { |
| 1627 | if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 1628 | skip |= LogPerformanceWarning( |
| 1629 | device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 1630 | "vkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, " |
| 1631 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 1632 | "it is more efficient.", |
| 1633 | report_data->FormatHandle(commandBuffer).c_str(), color_attachment); |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) { |
| 1639 | uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment; |
| 1640 | |
| 1641 | if (fb_attachment != VK_ATTACHMENT_UNUSED) { |
| 1642 | if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 1643 | skip |= LogPerformanceWarning( |
| 1644 | device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 1645 | "vkCmdClearAttachments() issued on %s for the depth attachment in this subpass, " |
| 1646 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 1647 | "it is more efficient.", |
| 1648 | report_data->FormatHandle(commandBuffer).c_str()); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) { |
| 1654 | uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment; |
| 1655 | |
| 1656 | if (fb_attachment != VK_ATTACHMENT_UNUSED) { |
| 1657 | if (rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 1658 | skip |= LogPerformanceWarning( |
| 1659 | device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 1660 | "vkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, " |
| 1661 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 1662 | "it is more efficient.", |
| 1663 | report_data->FormatHandle(commandBuffer).c_str()); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
Camden Stocker | f55721f | 2019-09-09 11:04:49 -0600 | [diff] [blame] | 1670 | return skip; |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 1671 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1672 | |
| 1673 | bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1674 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1675 | const VkImageResolve* pRegions) const { |
| 1676 | bool skip = false; |
| 1677 | |
| 1678 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 1679 | LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage, |
| 1680 | "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. " |
| 1681 | "This is a very slow and extremely bandwidth intensive path. " |
| 1682 | "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.", |
| 1683 | VendorSpecificTag(kBPVendorArm)); |
| 1684 | |
| 1685 | return skip; |
| 1686 | } |
| 1687 | |
| 1688 | bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo, |
| 1689 | const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const { |
| 1690 | bool skip = false; |
| 1691 | |
| 1692 | if (VendorCheckEnabled(kBPVendorArm)) { |
| 1693 | if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) { |
| 1694 | skip |= LogPerformanceWarning( |
| 1695 | device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes, |
| 1696 | "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). " |
| 1697 | "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D " |
| 1698 | "image) are actually used. If you need different wrapping modes, disregard this warning.", |
| 1699 | VendorSpecificTag(kBPVendorArm)); |
| 1700 | } |
| 1701 | |
| 1702 | if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) { |
| 1703 | skip |= LogPerformanceWarning( |
| 1704 | device, kVUID_BestPractices_CreateSampler_LodClamping, |
| 1705 | "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. " |
| 1706 | "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod " |
| 1707 | "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.", |
| 1708 | VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod); |
| 1709 | } |
| 1710 | |
| 1711 | if (pCreateInfo->mipLodBias != 0.0f) { |
| 1712 | skip |= |
| 1713 | LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias, |
| 1714 | "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient " |
| 1715 | "descriptors being created and may cause reduced performance.", |
| 1716 | VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias); |
| 1717 | } |
| 1718 | |
| 1719 | if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER || |
| 1720 | pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER || |
| 1721 | pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) && |
| 1722 | (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) { |
| 1723 | skip |= LogPerformanceWarning( |
| 1724 | device, kVUID_BestPractices_CreateSampler_BorderClampColor, |
| 1725 | "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. " |
| 1726 | "This will lead to less efficient descriptors being created and may cause reduced performance. " |
| 1727 | "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.", |
| 1728 | VendorSpecificTag(kBPVendorArm)); |
| 1729 | } |
| 1730 | |
| 1731 | if (pCreateInfo->unnormalizedCoordinates) { |
| 1732 | skip |= LogPerformanceWarning( |
| 1733 | device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates, |
| 1734 | "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient " |
| 1735 | "descriptors being created and may cause reduced performance.", |
| 1736 | VendorSpecificTag(kBPVendorArm)); |
| 1737 | } |
| 1738 | |
| 1739 | if (pCreateInfo->anisotropyEnable) { |
| 1740 | skip |= LogPerformanceWarning( |
| 1741 | device, kVUID_BestPractices_CreateSampler_Anisotropy, |
| 1742 | "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created " |
| 1743 | "and may cause reduced performance.", |
| 1744 | VendorSpecificTag(kBPVendorArm)); |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | return skip; |
| 1749 | } |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 1750 | |
| 1751 | void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); } |
| 1752 | |
| 1753 | bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) { |
| 1754 | // look for a cache hit |
| 1755 | auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; }); |
| 1756 | if (hit != _entries.end()) { |
| 1757 | // mark the cache hit as being most recently used |
| 1758 | hit->age = iteration++; |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
| 1762 | // if there's no cache hit, we need to model the entry being inserted into the cache |
| 1763 | CacheEntry new_entry = {value, iteration}; |
| 1764 | if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) { |
| 1765 | // if there is still space left in the cache, use the next available slot |
| 1766 | *(_entries.begin() + iteration) = new_entry; |
| 1767 | } else { |
| 1768 | // otherwise replace the least recently used cache entry |
| 1769 | auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; }); |
| 1770 | *lru = new_entry; |
| 1771 | } |
| 1772 | iteration++; |
| 1773 | return false; |
| 1774 | } |