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