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