Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2022 Valve Corporation |
| 3 | * Copyright (c) 2015-2022 LunarG, Inc. |
Nadav Geva | 41c12a2 | 2021-05-21 13:14:05 -0400 | [diff] [blame] | 4 | * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
Camden | eaa86ea | 2019-07-26 11:00:09 -0600 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Camden Stocker <camden@lunarg.com> |
Nadav Geva | 41c12a2 | 2021-05-21 13:14:05 -0400 | [diff] [blame] | 19 | * Author: Nadav Geva <nadav.geva@amd.com> |
Camden | eaa86ea | 2019-07-26 11:00:09 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
Mark Lobodzinski | 57b8ae8 | 2020-02-20 16:37:14 -0700 | [diff] [blame] | 22 | #include "best_practices_validation.h" |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 23 | #include "layer_chassis_dispatch.h" |
Camden Stocker | 0a660ce | 2019-08-27 15:30:40 -0600 | [diff] [blame] | 24 | #include "best_practices_error_enums.h" |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 25 | #include "shader_validation.h" |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 26 | #include "sync_utils.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 27 | #include "cmd_buffer_state.h" |
| 28 | #include "device_state.h" |
| 29 | #include "render_pass_state.h" |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 30 | |
| 31 | #include <string> |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 32 | #include <bitset> |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 33 | #include <memory> |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 34 | |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 35 | struct VendorSpecificInfo { |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 36 | EnableFlags vendor_id; |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 37 | std::string name; |
| 38 | }; |
| 39 | |
LawG4 | 7546309 | 2022-02-22 10:45:54 +0000 | [diff] [blame] | 40 | const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {{kBPVendorArm, {vendor_specific_arm, "Arm"}}, |
| 41 | {kBPVendorAMD, {vendor_specific_amd, "AMD"}}, |
Rodrigo Locatti | c779cb3 | 2022-02-25 19:26:31 -0300 | [diff] [blame] | 42 | {kBPVendorIMG, {vendor_specific_img, "IMG"}}, |
| 43 | {kBPVendorNVIDIA, {vendor_specific_nvidia, "NVIDIA"}}}; |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 44 | |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 45 | const SpecialUseVUIDs kSpecialUseInstanceVUIDs { |
| 46 | kVUID_BestPractices_CreateInstance_SpecialUseExtension_CADSupport, |
| 47 | kVUID_BestPractices_CreateInstance_SpecialUseExtension_D3DEmulation, |
| 48 | kVUID_BestPractices_CreateInstance_SpecialUseExtension_DevTools, |
| 49 | kVUID_BestPractices_CreateInstance_SpecialUseExtension_Debugging, |
| 50 | kVUID_BestPractices_CreateInstance_SpecialUseExtension_GLEmulation, |
| 51 | }; |
| 52 | |
| 53 | const SpecialUseVUIDs kSpecialUseDeviceVUIDs { |
| 54 | kVUID_BestPractices_CreateDevice_SpecialUseExtension_CADSupport, |
| 55 | kVUID_BestPractices_CreateDevice_SpecialUseExtension_D3DEmulation, |
| 56 | kVUID_BestPractices_CreateDevice_SpecialUseExtension_DevTools, |
| 57 | kVUID_BestPractices_CreateDevice_SpecialUseExtension_Debugging, |
| 58 | kVUID_BestPractices_CreateDevice_SpecialUseExtension_GLEmulation, |
| 59 | }; |
| 60 | |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 61 | static constexpr std::array<VkFormat, 12> kCustomClearColorCompressedFormatsNVIDIA = { |
| 62 | VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_A8B8G8R8_UNORM_PACK32, |
| 63 | VK_FORMAT_A2R10G10B10_UNORM_PACK32, VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_R16G16B16A16_UNORM, |
| 64 | VK_FORMAT_R16G16B16A16_SNORM, VK_FORMAT_R16G16B16A16_UINT, VK_FORMAT_R16G16B16A16_SINT, |
| 65 | VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_B10G11R11_UFLOAT_PACK32, |
| 66 | }; |
| 67 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 68 | ReadLockGuard BestPractices::ReadLock() { |
| 69 | if (fine_grained_locking) { |
| 70 | return ReadLockGuard(validation_object_mutex, std::defer_lock); |
| 71 | } else { |
| 72 | return ReadLockGuard(validation_object_mutex); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | WriteLockGuard BestPractices::WriteLock() { |
| 77 | if (fine_grained_locking) { |
| 78 | return WriteLockGuard(validation_object_mutex, std::defer_lock); |
| 79 | } else { |
| 80 | return WriteLockGuard(validation_object_mutex); |
| 81 | } |
| 82 | } |
| 83 | |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 84 | std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb, |
| 85 | const VkCommandBufferAllocateInfo* pCreateInfo, |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 86 | const COMMAND_POOL_STATE* pool) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 87 | return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<bp_state::CommandBuffer>(this, cb, pCreateInfo, pool)); |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 90 | bp_state::CommandBuffer::CommandBuffer(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo, |
| 91 | const COMMAND_POOL_STATE* pool) |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 92 | : CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {} |
| 93 | |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 94 | bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 95 | for (const auto& vendor : kVendorInfo) { |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 96 | if (vendors & vendor.first && enabled[vendor.second.vendor_id]) { |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | const char* VendorSpecificTag(BPVendorFlags vendors) { |
| 104 | // Cache built vendor tags in a map |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 105 | static layer_data::unordered_map<BPVendorFlags, std::string> tag_map; |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 106 | |
| 107 | auto res = tag_map.find(vendors); |
| 108 | if (res == tag_map.end()) { |
| 109 | // Build the vendor tag string |
| 110 | std::stringstream vendor_tag; |
| 111 | |
| 112 | vendor_tag << "["; |
| 113 | bool first_vendor = true; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 114 | for (const auto& vendor : kVendorInfo) { |
Attilio Provenzano | 19d6a98 | 2020-02-27 12:41:41 +0000 | [diff] [blame] | 115 | if (vendors & vendor.first) { |
| 116 | if (!first_vendor) { |
| 117 | vendor_tag << ", "; |
| 118 | } |
| 119 | vendor_tag << vendor.second.name; |
| 120 | first_vendor = false; |
| 121 | } |
| 122 | } |
| 123 | vendor_tag << "]"; |
| 124 | |
| 125 | tag_map[vendors] = vendor_tag.str(); |
| 126 | res = tag_map.find(vendors); |
| 127 | } |
| 128 | |
| 129 | return res->second.c_str(); |
| 130 | } |
| 131 | |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 132 | const char* DepReasonToString(ExtDeprecationReason reason) { |
| 133 | switch (reason) { |
| 134 | case kExtPromoted: |
| 135 | return "promoted to"; |
| 136 | break; |
| 137 | case kExtObsoleted: |
| 138 | return "obsoleted by"; |
| 139 | break; |
| 140 | case kExtDeprecated: |
| 141 | return "deprecated by"; |
| 142 | break; |
| 143 | default: |
| 144 | return ""; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version, |
| 150 | const char* vuid) const { |
| 151 | bool skip = false; |
| 152 | auto dep_info_it = deprecated_extensions.find(extension_name); |
| 153 | if (dep_info_it != deprecated_extensions.end()) { |
| 154 | auto dep_info = dep_info_it->second; |
Mark Lobodzinski | 6a14970 | 2020-05-14 12:21:34 -0600 | [diff] [blame] | 155 | if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) || |
Tony-LunarG | c30b59f | 2022-02-15 11:02:36 -0700 | [diff] [blame] | 156 | ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2)) || |
| 157 | ((dep_info.target.compare("VK_VERSION_1_3") == 0) && (version >= VK_API_VERSION_1_3))) { |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 158 | skip |= |
| 159 | LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.", |
| 160 | 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] | 161 | } else if (dep_info.target.find("VK_VERSION") == std::string::npos) { |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 162 | if (dep_info.target.length() == 0) { |
| 163 | skip |= LogWarning(instance, vuid, |
| 164 | "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated " |
| 165 | "without replacement.", |
| 166 | api_name, extension_name); |
| 167 | } else { |
| 168 | skip |= LogWarning(instance, vuid, |
| 169 | "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.", |
| 170 | api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str()); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return skip; |
| 175 | } |
| 176 | |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 177 | bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const |
| 178 | { |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 179 | bool skip = false; |
| 180 | auto dep_info_it = special_use_extensions.find(extension_name); |
| 181 | |
| 182 | if (dep_info_it != special_use_extensions.end()) { |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 183 | const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s " |
| 184 | "and it is strongly recommended that it be otherwise avoided."; |
| 185 | auto& special_uses = dep_info_it->second; |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 186 | |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 187 | if (special_uses.find("cadsupport") != std::string::npos) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 188 | skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name, |
| 189 | "specialized functionality used by CAD/CAM applications"); |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 190 | } |
| 191 | if (special_uses.find("d3demulation") != std::string::npos) { |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 192 | skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name, |
| 193 | "D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D"); |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 194 | } |
| 195 | if (special_uses.find("devtools") != std::string::npos) { |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 196 | skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name, |
| 197 | "developer tools such as capture-replay libraries"); |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 198 | } |
| 199 | if (special_uses.find("debugging") != std::string::npos) { |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 200 | skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name, |
| 201 | "use by applications when debugging"); |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 202 | } |
| 203 | if (special_uses.find("glemulation") != std::string::npos) { |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 204 | skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name, |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 205 | "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality " |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 206 | "specific to those APIs"); |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 207 | } |
Mark Lobodzinski | 057724a | 2020-11-09 17:13:18 -0700 | [diff] [blame] | 208 | } |
| 209 | return skip; |
| 210 | } |
| 211 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 212 | bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 213 | VkInstance* pInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 214 | bool skip = false; |
| 215 | |
| 216 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
| 217 | if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) { |
Camden Stocker | 11ecf51 | 2020-01-21 16:06:49 -0800 | [diff] [blame] | 218 | skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 219 | "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.", |
| 220 | pCreateInfo->ppEnabledExtensionNames[i]); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 221 | } |
Mark Lobodzinski | 17d8dc6 | 2020-06-03 08:48:58 -0600 | [diff] [blame] | 222 | uint32_t specified_version = |
| 223 | (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0); |
| 224 | skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version, |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 225 | kVUID_BestPractices_CreateInstance_DeprecatedExtension); |
Hannes Harnisch | 607d1d9 | 2021-07-10 18:44:56 +0200 | [diff] [blame] | 226 | skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return skip; |
| 230 | } |
| 231 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 232 | bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 233 | const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 234 | bool skip = false; |
| 235 | |
| 236 | // get API version of physical device passed when creating device. |
| 237 | VkPhysicalDeviceProperties physical_device_properties{}; |
| 238 | DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties); |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 239 | auto device_api_version = physical_device_properties.apiVersion; |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 240 | |
| 241 | // check api versions and warn if instance api Version is higher than version on device. |
Jeremy Gebben | 404f6ac | 2021-10-28 12:33:28 -0600 | [diff] [blame] | 242 | if (api_version > device_api_version) { |
| 243 | std::string inst_api_name = StringAPIVersion(api_version); |
Mark Lobodzinski | 6088078 | 2020-08-11 08:02:07 -0600 | [diff] [blame] | 244 | std::string dev_api_name = StringAPIVersion(device_api_version); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 245 | |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 246 | skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch, |
| 247 | "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s", |
| 248 | inst_api_name.c_str(), dev_api_name.c_str()); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Rodrigo Locatti | c2d5cf4 | 2022-03-01 18:05:26 -0300 | [diff] [blame] | 251 | std::vector<std::string> extensions; |
| 252 | { |
| 253 | uint32_t property_count = 0; |
| 254 | if (DispatchEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &property_count, nullptr) == VK_SUCCESS) { |
| 255 | std::vector<VkExtensionProperties> property_list(property_count); |
| 256 | if (DispatchEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &property_count, property_list.data()) == VK_SUCCESS) { |
| 257 | extensions.reserve(property_list.size()); |
| 258 | for (const VkExtensionProperties& properties : property_list) { |
| 259 | extensions.push_back(properties.extensionName); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 265 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 266 | const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i]; |
| 267 | |
paul-lunarg | d8c7808 | 2022-08-31 20:00:02 +0200 | [diff] [blame] | 268 | uint32_t extension_api_version = std::min(api_version, device_api_version); |
paul-lunarg | 4e0e1df | 2022-08-31 18:46:21 +0200 | [diff] [blame] | 269 | |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 270 | if (white_list(extension_name, kInstanceExtensionNames)) { |
Camden Stocker | 11ecf51 | 2020-01-21 16:06:49 -0800 | [diff] [blame] | 271 | skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 272 | "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.", |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 273 | extension_name); |
paul-lunarg | 4e0e1df | 2022-08-31 18:46:21 +0200 | [diff] [blame] | 274 | extension_api_version = api_version; |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 275 | } |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 276 | |
paul-lunarg | 4e0e1df | 2022-08-31 18:46:21 +0200 | [diff] [blame] | 277 | skip |= ValidateDeprecatedExtensions("CreateDevice", extension_name, extension_api_version, |
Mark Lobodzinski | 6167e10 | 2020-02-24 17:03:55 -0700 | [diff] [blame] | 278 | kVUID_BestPractices_CreateDevice_DeprecatedExtension); |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 279 | skip |= ValidateSpecialUseExtensions("CreateDevice", extension_name, kSpecialUseDeviceVUIDs); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 280 | } |
| 281 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 282 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 283 | if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 284 | skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled, |
| 285 | "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures()."); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 286 | } |
| 287 | |
LawG4 | 3f848c7 | 2022-02-23 09:35:21 +0000 | [diff] [blame] | 288 | if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorIMG)) && |
| 289 | (pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) { |
Szilard Papp | 7d2c795 | 2020-06-22 14:38:13 +0100 | [diff] [blame] | 290 | skip |= LogPerformanceWarning( |
| 291 | device, kVUID_BestPractices_CreateDevice_RobustBufferAccess, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 292 | "%s %s %s: vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during " |
Szilard Papp | 7d2c795 | 2020-06-22 14:38:13 +0100 | [diff] [blame] | 293 | "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage " |
| 294 | "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case " |
| 295 | "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.", |
LawG4 | 3f848c7 | 2022-02-23 09:35:21 +0000 | [diff] [blame] | 296 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorIMG)); |
Szilard Papp | 7d2c795 | 2020-06-22 14:38:13 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Rodrigo Locatti | 8dde2ff | 2022-03-01 18:06:08 -0300 | [diff] [blame] | 299 | const bool enabled_pageable_device_local_memory = IsExtEnabled(device_extensions.vk_ext_pageable_device_local_memory); |
| 300 | if (VendorCheckEnabled(kBPVendorNVIDIA) && !enabled_pageable_device_local_memory && |
| 301 | std::find(extensions.begin(), extensions.end(), VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME) != extensions.end()) { |
| 302 | skip |= LogPerformanceWarning( |
| 303 | device, kVUID_BestPractices_CreateDevice_PageableDeviceLocalMemory, |
| 304 | "%s vkCreateDevice() called without pageable device local memory. " |
| 305 | "Use pageableDeviceLocalMemory from VK_EXT_pageable_device_local_memory when it is available.", |
| 306 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 307 | } |
| 308 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 309 | return skip; |
| 310 | } |
| 311 | |
| 312 | bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 313 | const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 314 | bool skip = false; |
| 315 | |
| 316 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 317 | std::stringstream buffer_hex; |
| 318 | buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 319 | |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 320 | skip |= LogWarning( |
| 321 | device, kVUID_BestPractices_SharingModeExclusive, |
| 322 | "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues " |
| 323 | "(queueFamilyIndexCount of %" PRIu32 ").", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 324 | buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return skip; |
| 328 | } |
| 329 | |
| 330 | bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 331 | const VkAllocationCallbacks* pAllocator, VkImage* pImage) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 332 | bool skip = false; |
| 333 | |
| 334 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 335 | std::stringstream image_hex; |
| 336 | image_hex << "0x" << std::hex << HandleToUint64(pImage); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 337 | |
| 338 | skip |= |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 339 | LogWarning(device, kVUID_BestPractices_SharingModeExclusive, |
| 340 | "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues " |
| 341 | "(queueFamilyIndexCount of %" PRIu32 ").", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 342 | image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 343 | } |
| 344 | |
ziga-lunarg | 6df3d10 | 2022-03-18 17:02:14 +0100 | [diff] [blame] | 345 | if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) { |
| 346 | skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags, |
| 347 | "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not " |
| 348 | "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the " |
| 349 | "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect."); |
| 350 | } |
| 351 | |
LawG4 | 655f59c | 2022-02-23 13:55:55 +0000 | [diff] [blame] | 352 | if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) { |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 353 | if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) { |
| 354 | skip |= LogPerformanceWarning( |
| 355 | device, kVUID_BestPractices_CreateImage_NonTransientMSImage, |
LawG4 | 655f59c | 2022-02-23 13:55:55 +0000 | [diff] [blame] | 356 | "%s %s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have " |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 357 | "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, " |
| 358 | "and do not need to be backed by physical storage. " |
| 359 | "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.", |
LawG4 | 655f59c | 2022-02-23 13:55:55 +0000 | [diff] [blame] | 360 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG)); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
LawG4 | ba11389 | 2022-02-23 14:39:02 +0000 | [diff] [blame] | 364 | if (VendorCheckEnabled(kBPVendorArm) && pCreateInfo->samples > kMaxEfficientSamplesArm) { |
| 365 | skip |= LogPerformanceWarning( |
| 366 | device, kVUID_BestPractices_CreateImage_TooLargeSampleCount, |
| 367 | "%s vkCreateImage(): Trying to create an image with %u samples. " |
| 368 | "The hardware revision may not have full throughput for framebuffers with more than %u samples.", |
| 369 | VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm); |
| 370 | } |
| 371 | |
| 372 | if (VendorCheckEnabled(kBPVendorIMG) && pCreateInfo->samples > kMaxEfficientSamplesImg) { |
| 373 | skip |= LogPerformanceWarning( |
| 374 | device, kVUID_BestPractices_CreateImage_TooLargeSampleCount, |
| 375 | "%s vkCreateImage(): Trying to create an image with %u samples. " |
| 376 | "The device may not have full support for true multisampling for images with more than %u samples. " |
| 377 | "XT devices support up to 8 samples, XE up to 4 samples.", |
| 378 | VendorSpecificTag(kBPVendorIMG), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesImg); |
| 379 | } |
| 380 | |
LawG4 | db16f80 | 2022-03-21 17:33:39 +0000 | [diff] [blame] | 381 | if (VendorCheckEnabled(kBPVendorIMG) && (pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG || |
| 382 | pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG || |
| 383 | pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG || |
| 384 | pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG || |
| 385 | pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG || |
| 386 | pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG || |
| 387 | pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG || |
| 388 | pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG)) { |
| 389 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_Texture_Format_PVRTC_Outdated, |
| 390 | "%s vkCreateImage(): Trying to create an image with a PVRTC format. Both PVRTC1 and PVRTC2 " |
| 391 | "are slower than standard image formats on PowerVR GPUs, prefer ETC, BC, ASTC, etc.", |
| 392 | VendorSpecificTag(kBPVendorIMG)); |
| 393 | } |
| 394 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 395 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 396 | std::stringstream image_hex; |
| 397 | image_hex << "0x" << std::hex << HandleToUint64(pImage); |
| 398 | |
| 399 | if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) && |
| 400 | (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) { |
| 401 | skip |= LogPerformanceWarning(device, |
| 402 | kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets, |
| 403 | "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. " |
| 404 | "Using a SHARING_MODE_CONCURRENT " |
| 405 | "is not recommended with color and depth targets", |
| 406 | VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str()); |
| 407 | } |
| 408 | |
| 409 | if ((pCreateInfo->usage & |
| 410 | (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) && |
| 411 | (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) { |
| 412 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets, |
| 413 | "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. " |
| 414 | "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets", |
| 415 | VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str()); |
| 416 | } |
| 417 | |
| 418 | if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) && |
| 419 | (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) { |
| 420 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets, |
| 421 | "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a " |
| 422 | "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets", |
| 423 | VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str()); |
| 424 | } |
| 425 | } |
| 426 | |
Rodrigo Locatti | 5466f9d | 2022-03-09 18:20:38 -0300 | [diff] [blame] | 427 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 428 | std::stringstream image_hex; |
| 429 | image_hex << "0x" << std::hex << HandleToUint64(pImage); |
| 430 | |
| 431 | if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) { |
| 432 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateImage_TilingLinear, |
| 433 | "%s Performance warning: image (%s) is created with tiling VK_IMAGE_TILING_LINEAR. " |
| 434 | "Use VK_IMAGE_TILING_OPTIMAL instead.", |
| 435 | VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str()); |
| 436 | } |
Rodrigo Locatti | 3290c2b | 2022-03-09 18:25:56 -0300 | [diff] [blame] | 437 | |
| 438 | if (pCreateInfo->format == VK_FORMAT_D32_SFLOAT || pCreateInfo->format == VK_FORMAT_D32_SFLOAT_S8_UINT) { |
| 439 | skip |= LogPerformanceWarning( |
| 440 | device, kVUID_BestPractices_CreateImage_Depth32Format, |
| 441 | "%s Performance warning: image (%s) is created with a 32-bit depth format. Use VK_FORMAT_D24_UNORM_S8_UINT or " |
| 442 | "VK_FORMAT_D16_UNORM instead, unless the extra precision is needed.", |
| 443 | VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str()); |
| 444 | } |
Rodrigo Locatti | 5466f9d | 2022-03-09 18:20:38 -0300 | [diff] [blame] | 445 | } |
| 446 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 447 | return skip; |
| 448 | } |
| 449 | |
| 450 | bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 451 | const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 452 | bool skip = false; |
| 453 | |
Jeremy Gebben | 383b9a3 | 2021-09-08 16:31:33 -0600 | [diff] [blame] | 454 | const auto* bp_pd_state = GetPhysicalDeviceState(); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 455 | if (bp_pd_state) { |
| 456 | if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) { |
| 457 | skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
| 458 | "vkCreateSwapchainKHR() called before getting surface capabilities from " |
| 459 | "vkGetPhysicalDeviceSurfaceCapabilitiesKHR()."); |
| 460 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 461 | |
Shannon McPherson | 73e58c8 | 2021-03-05 17:14:26 -0700 | [diff] [blame] | 462 | if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) && |
| 463 | (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) { |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 464 | skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
| 465 | "vkCreateSwapchainKHR() called before getting surface present mode(s) from " |
| 466 | "vkGetPhysicalDeviceSurfacePresentModesKHR()."); |
| 467 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 468 | |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 469 | if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) { |
| 470 | skip |= LogWarning( |
| 471 | device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled, |
| 472 | "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR()."); |
| 473 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 474 | } |
| 475 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 476 | if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 477 | skip |= |
| 478 | LogWarning(device, kVUID_BestPractices_SharingModeExclusive, |
Mark Lobodzinski | 019f4e3 | 2020-04-13 11:01:35 -0600 | [diff] [blame] | 479 | "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] | 480 | "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").", |
| 481 | pCreateInfo->queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 482 | } |
| 483 | |
ziga-lunarg | 79beba6 | 2022-03-30 01:17:30 +0200 | [diff] [blame] | 484 | const auto present_mode = pCreateInfo->presentMode; |
| 485 | if (((present_mode == VK_PRESENT_MODE_MAILBOX_KHR) || (present_mode == VK_PRESENT_MODE_FIFO_KHR)) && |
| 486 | (pCreateInfo->minImageCount == 2)) { |
Szilard Papp | 48a6da3 | 2020-06-10 14:41:59 +0100 | [diff] [blame] | 487 | skip |= LogPerformanceWarning( |
| 488 | device, kVUID_BestPractices_SuboptimalSwapchainImageCount, |
| 489 | "Warning: A Swapchain is being created with minImageCount set to %" PRIu32 |
| 490 | ", which means double buffering is going " |
| 491 | "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, " |
| 492 | "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to " |
| 493 | "3 to use triple buffering to maximize performance in such cases.", |
| 494 | pCreateInfo->minImageCount); |
| 495 | } |
| 496 | |
Szilard Papp | d5f0f81 | 2020-06-22 09:01:29 +0100 | [diff] [blame] | 497 | if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) { |
| 498 | skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode, |
| 499 | "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". " |
| 500 | "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. " |
| 501 | "Presentation modes which are not FIFO will present the latest available frame and discard other " |
| 502 | "frame(s) if any.", |
| 503 | VendorSpecificTag(kBPVendorArm)); |
| 504 | } |
| 505 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 506 | return skip; |
| 507 | } |
| 508 | |
| 509 | bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount, |
| 510 | const VkSwapchainCreateInfoKHR* pCreateInfos, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 511 | const VkAllocationCallbacks* pAllocator, |
| 512 | VkSwapchainKHR* pSwapchains) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 513 | bool skip = false; |
| 514 | |
| 515 | for (uint32_t i = 0; i < swapchainCount; i++) { |
| 516 | if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 517 | skip |= LogWarning( |
| 518 | device, kVUID_BestPractices_SharingModeExclusive, |
| 519 | "Warning: A shared swapchain (index %" PRIu32 |
| 520 | ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple " |
| 521 | "queues (queueFamilyIndexCount of %" PRIu32 ").", |
| 522 | i, pCreateInfos[i].queueFamilyIndexCount); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | return skip; |
| 527 | } |
| 528 | |
| 529 | bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 530 | const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 531 | bool skip = false; |
| 532 | |
| 533 | for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) { |
| 534 | VkFormat format = pCreateInfo->pAttachments[i].format; |
| 535 | if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) { |
| 536 | if ((FormatIsColor(format) || FormatHasDepth(format)) && |
| 537 | pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 538 | skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment, |
| 539 | "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and " |
| 540 | "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you " |
| 541 | "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the " |
| 542 | "image truely is undefined at the start of the render pass."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 543 | } |
| 544 | if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 545 | skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment, |
| 546 | "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD " |
| 547 | "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you " |
| 548 | "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the " |
| 549 | "image truely is undefined at the start of the render pass."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 550 | } |
| 551 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 552 | |
| 553 | const auto& attachment = pCreateInfo->pAttachments[i]; |
| 554 | if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) { |
| 555 | bool access_requires_memory = |
| 556 | attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE; |
| 557 | |
| 558 | if (FormatHasStencil(format)) { |
| 559 | access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD || |
| 560 | attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE; |
| 561 | } |
| 562 | |
| 563 | if (access_requires_memory) { |
| 564 | skip |= LogPerformanceWarning( |
| 565 | device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory, |
| 566 | "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp " |
| 567 | "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, " |
| 568 | "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.", |
| 569 | i, static_cast<uint32_t>(attachment.samples)); |
| 570 | } |
| 571 | } |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) { |
| 575 | skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask); |
| 576 | skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask); |
| 577 | } |
| 578 | |
| 579 | return skip; |
| 580 | } |
| 581 | |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 582 | bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount, |
| 583 | const VkImageView* image_views) const { |
| 584 | bool skip = false; |
| 585 | |
| 586 | // Check for non-transient attachments that should be transient and vice versa |
| 587 | for (uint32_t i = 0; i < attachmentCount; ++i) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 588 | const auto& attachment = rpci->pAttachments[i]; |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 589 | bool attachment_should_be_transient = |
| 590 | (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE); |
| 591 | |
| 592 | if (FormatHasStencil(attachment.format)) { |
| 593 | attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD && |
| 594 | attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE); |
| 595 | } |
| 596 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 597 | auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]); |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 598 | if (view_state) { |
Jeremy Gebben | 057f9d5 | 2021-11-05 14:12:31 -0600 | [diff] [blame] | 599 | const auto& ici = view_state->image_state->createInfo; |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 600 | |
| 601 | bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0; |
| 602 | |
| 603 | // The check for an image that should not be transient applies to all GPUs |
| 604 | if (!attachment_should_be_transient && image_is_transient) { |
| 605 | skip |= LogPerformanceWarning( |
| 606 | device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient, |
| 607 | "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, " |
| 608 | "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. " |
| 609 | "Physical memory will need to be backed lazily to this image, potentially causing stalls.", |
| 610 | i); |
| 611 | } |
| 612 | |
| 613 | bool supports_lazy = false; |
| 614 | for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) { |
| 615 | if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { |
| 616 | supports_lazy = true; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // The check for an image that should be transient only applies to GPUs supporting |
| 621 | // lazily allocated memory |
| 622 | if (supports_lazy && attachment_should_be_transient && !image_is_transient) { |
| 623 | skip |= LogPerformanceWarning( |
| 624 | device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient, |
| 625 | "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, " |
| 626 | "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. " |
| 627 | "You can save physical memory by using transient attachment backed by lazily allocated memory here.", |
| 628 | i); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | return skip; |
| 633 | } |
| 634 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 635 | bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, |
| 636 | const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const { |
| 637 | bool skip = false; |
| 638 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 639 | auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass); |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 640 | if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) { |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 641 | skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments); |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | return skip; |
| 645 | } |
| 646 | |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 647 | bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, |
| 648 | VkDescriptorSet* pDescriptorSets, void* ads_state_data) const { |
| 649 | bool skip = false; |
| 650 | skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data); |
| 651 | |
| 652 | if (!skip) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 653 | const auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool); |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 654 | // if the number of freed sets > 0, it implies they could be recycled instead if desirable |
| 655 | // this warning is specific to Arm |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 656 | if (VendorCheckEnabled(kBPVendorArm) && pool_state && (pool_state->freed_count > 0)) { |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 657 | skip |= LogPerformanceWarning( |
| 658 | device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse, |
| 659 | "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the " |
| 660 | "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.", |
| 661 | VendorSpecificTag(kBPVendorArm)); |
| 662 | } |
ziga-lunarg | 5a76c44 | 2022-04-17 18:04:08 +0200 | [diff] [blame] | 663 | |
| 664 | if (IsExtEnabled(device_extensions.vk_khr_maintenance1)) { |
| 665 | // Track number of descriptorSets allowable in this pool |
| 666 | if (pool_state->GetAvailableSets() < pAllocateInfo->descriptorSetCount) { |
| 667 | skip |= LogWarning(pool_state->Handle(), kVUID_BestPractices_EmptyDescriptorPool, |
| 668 | "vkAllocateDescriptorSets(): Unable to allocate %" PRIu32 " descriptorSets from %s" |
| 669 | ". This pool only has %" PRIu32 " descriptorSets remaining.", |
| 670 | pAllocateInfo->descriptorSetCount, report_data->FormatHandle(pool_state->Handle()).c_str(), |
| 671 | pool_state->GetAvailableSets()); |
| 672 | } |
| 673 | } |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | return skip; |
| 677 | } |
| 678 | |
Mark Lobodzinski | 84101d7 | 2020-04-24 09:43:48 -0600 | [diff] [blame] | 679 | void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, |
| 680 | VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) { |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 681 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 682 | auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool); |
| 683 | if (pool_state) { |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 684 | // we record successful allocations by subtracting the allocation count from the last recorded free count |
| 685 | const auto alloc_count = pAllocateInfo->descriptorSetCount; |
| 686 | // clamp the unsigned subtraction to the range [0, last_free_count] |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 687 | if (pool_state->freed_count > alloc_count) { |
| 688 | pool_state->freed_count -= alloc_count; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 689 | } else { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 690 | pool_state->freed_count = 0; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 691 | } |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, |
| 697 | const VkDescriptorSet* pDescriptorSets, VkResult result) { |
| 698 | ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result); |
| 699 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 700 | auto pool_state = Get<bp_state::DescriptorPool>(descriptorPool); |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 701 | // we want to track frees because we're interested in suggesting re-use |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 702 | if (pool_state) { |
| 703 | pool_state->freed_count += descriptorSetCount; |
Sam Walls | e746d52 | 2020-03-16 21:20:23 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Rodrigo Locatti | d5b54f5 | 2022-03-16 19:12:45 -0300 | [diff] [blame] | 708 | void BestPractices::PreCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, |
| 709 | const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) { |
| 710 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 711 | WriteLockGuard guard{memory_free_events_lock_}; |
| 712 | |
| 713 | // Release old allocations to avoid overpopulating the container |
| 714 | const auto now = std::chrono::high_resolution_clock::now(); |
| 715 | const auto last_old = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [now](const MemoryFreeEvent& event) { |
| 716 | return now - event.time > kAllocateMemoryReuseTimeThresholdNVIDIA; |
| 717 | }); |
| 718 | memory_free_events_.erase(memory_free_events_.begin(), last_old.base()); |
| 719 | } |
| 720 | } |
| 721 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 722 | bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 723 | const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 724 | bool skip = false; |
| 725 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 726 | if ((Count<DEVICE_MEMORY_STATE>() + 1) > kMemoryObjectWarningLimit) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 727 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects, |
| 728 | "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 729 | } |
| 730 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 731 | if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) { |
| 732 | skip |= LogPerformanceWarning( |
| 733 | device, kVUID_BestPractices_AllocateMemory_SmallAllocation, |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 734 | "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current " |
| 735 | "threshold is %" PRIu64 " bytes). " |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 736 | "You should make large allocations and sub-allocate from one large VkDeviceMemory.", |
| 737 | pAllocateInfo->allocationSize, kMinDeviceAllocationSize); |
| 738 | } |
| 739 | |
Rodrigo Locatti | d5b54f5 | 2022-03-16 19:12:45 -0300 | [diff] [blame] | 740 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 741 | if (!device_extensions.vk_ext_pageable_device_local_memory && |
| 742 | !LvlFindInChain<VkMemoryPriorityAllocateInfoEXT>(pAllocateInfo->pNext)) { |
| 743 | skip |= LogPerformanceWarning( |
| 744 | device, kVUID_BestPractices_AllocateMemory_SetPriority, |
| 745 | "%s Use VkMemoryPriorityAllocateInfoEXT to provide the operating system information on the allocations that " |
| 746 | "should stay in video memory and which should be demoted first when video memory is limited. " |
| 747 | "The highest priority should be given to GPU-written resources like color attachments, depth attachments, " |
| 748 | "storage images, and buffers written from the GPU.", |
| 749 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 750 | } |
| 751 | |
| 752 | { |
| 753 | // Size in bytes for an allocation to be considered "compatible" |
| 754 | static constexpr VkDeviceSize size_threshold = VkDeviceSize{1} << 20; |
| 755 | |
| 756 | ReadLockGuard guard{memory_free_events_lock_}; |
| 757 | |
| 758 | const auto now = std::chrono::high_resolution_clock::now(); |
| 759 | const VkDeviceSize alloc_size = pAllocateInfo->allocationSize; |
| 760 | const uint32_t memory_type_index = pAllocateInfo->memoryTypeIndex; |
| 761 | const auto latest_event = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [&](const MemoryFreeEvent& event) { |
| 762 | return (memory_type_index == event.memory_type_index) && (alloc_size <= event.allocation_size) && |
| 763 | (alloc_size - event.allocation_size <= size_threshold) && (now - event.time < kAllocateMemoryReuseTimeThresholdNVIDIA); |
| 764 | }); |
| 765 | |
| 766 | if (latest_event != memory_free_events_.rend()) { |
| 767 | const auto time_delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - latest_event->time); |
| 768 | if (time_delta < std::chrono::milliseconds{5}) { |
| 769 | skip |= |
| 770 | LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_ReuseAllocations, |
| 771 | "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation " |
| 772 | "has just been released, and it could have been reused in place of this allocation.", |
| 773 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 774 | } else { |
| 775 | const uint32_t seconds = static_cast<uint32_t>(time_delta.count() / 1000); |
| 776 | const uint32_t milliseconds = static_cast<uint32_t>(time_delta.count() % 1000); |
| 777 | |
| 778 | skip |= LogPerformanceWarning( |
| 779 | device, kVUID_BestPractices_AllocateMemory_ReuseAllocations, |
| 780 | "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation has been released " |
| 781 | "%" PRIu32 ".%03" PRIu32 " seconds ago, and it could have been reused in place of this allocation.", |
| 782 | VendorSpecificTag(kBPVendorNVIDIA), seconds, milliseconds); |
| 783 | } |
| 784 | } |
| 785 | } |
Rodrigo Locatti | e4f8d52 | 2022-03-15 16:30:49 -0300 | [diff] [blame] | 786 | } |
| 787 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 788 | // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker |
| 789 | |
| 790 | return skip; |
| 791 | } |
| 792 | |
Mark Lobodzinski | 84101d7 | 2020-04-24 09:43:48 -0600 | [diff] [blame] | 793 | void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, |
| 794 | const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory, |
| 795 | VkResult result) { |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 796 | if (result != VK_SUCCESS) { |
| 797 | static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 798 | VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE, |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 799 | VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS}; |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 800 | static std::vector<VkResult> success_codes = {}; |
Nathaniel Cesario | db3f43f | 2021-05-12 09:08:23 -0600 | [diff] [blame] | 801 | ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes); |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 802 | return; |
| 803 | } |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 804 | } |
Camden Stocker | 9738af9 | 2019-10-16 13:54:03 -0700 | [diff] [blame] | 805 | |
Mark Lobodzinski | de15e58 | 2020-04-29 08:06:00 -0600 | [diff] [blame] | 806 | void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes, |
| 807 | const std::vector<VkResult>& success_codes) const { |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 808 | auto error = std::find(error_codes.begin(), error_codes.end(), result); |
| 809 | if (error != error_codes.end()) { |
Gareth Webb | 586c46b | 2021-01-13 11:17:22 +0000 | [diff] [blame] | 810 | static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR, |
| 811 | VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT}; |
| 812 | |
| 813 | auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result); |
| 814 | if (common_failure != common_failure_codes.end()) { |
| 815 | LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result)); |
| 816 | } else { |
| 817 | LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result)); |
| 818 | } |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | auto success = std::find(success_codes.begin(), success_codes.end(), result); |
| 822 | if (success != success_codes.end()) { |
Mark Lobodzinski | e721515 | 2020-05-11 08:21:23 -0600 | [diff] [blame] | 823 | LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name, |
| 824 | string_VkResult(result)); |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
Rodrigo Locatti | d5b54f5 | 2022-03-16 19:12:45 -0300 | [diff] [blame] | 828 | void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) { |
| 829 | if (memory != VK_NULL_HANDLE && VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 830 | auto mem_info = Get<DEVICE_MEMORY_STATE>(memory); |
| 831 | |
| 832 | // Exclude memory free events on dedicated allocations, or imported/exported allocations. |
| 833 | if (!mem_info->IsDedicatedBuffer() && !mem_info->IsDedicatedImage() && !mem_info->IsExport() && !mem_info->IsImport()) { |
| 834 | MemoryFreeEvent event; |
| 835 | event.time = std::chrono::high_resolution_clock::now(); |
| 836 | event.memory_type_index = mem_info->alloc_info.memoryTypeIndex; |
| 837 | event.allocation_size = mem_info->alloc_info.allocationSize; |
| 838 | |
| 839 | WriteLockGuard guard{memory_free_events_lock_}; |
| 840 | memory_free_events_.push_back(event); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator); |
| 845 | } |
| 846 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 847 | bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory, |
| 848 | const VkAllocationCallbacks* pAllocator) const { |
Mark Lobodzinski | 91e50bf | 2020-01-14 09:55:11 -0700 | [diff] [blame] | 849 | if (memory == VK_NULL_HANDLE) return false; |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 850 | bool skip = false; |
| 851 | |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 852 | auto mem_info = Get<DEVICE_MEMORY_STATE>(memory); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 853 | |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 854 | for (const auto& item : mem_info->ObjectBindings()) { |
| 855 | const auto& obj = item.first; |
Mark Lobodzinski | 818425a | 2020-03-16 18:19:03 -0600 | [diff] [blame] | 856 | LogObjectList objlist(device); |
| 857 | objlist.add(obj); |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 858 | objlist.add(mem_info->mem()); |
Mark Lobodzinski | 818425a | 2020-03-16 18:19:03 -0600 | [diff] [blame] | 859 | skip |= LogWarning(objlist, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 860 | 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] | 861 | } |
| 862 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 863 | return skip; |
| 864 | } |
| 865 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 866 | bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const { |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 867 | bool skip = false; |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 868 | auto buffer_state = Get<BUFFER_STATE>(buffer); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 869 | |
sfricke-samsung | e244119 | 2019-11-06 14:07:57 -0800 | [diff] [blame] | 870 | if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 871 | skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled, |
| 872 | "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.", |
| 873 | api_name, report_data->FormatHandle(buffer).c_str()); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 874 | } |
| 875 | |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 876 | auto mem_state = Get<DEVICE_MEMORY_STATE>(memory); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 877 | |
AndreyVK_D3D | 0416a33 | 2021-11-02 23:22:28 +0300 | [diff] [blame] | 878 | if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size && |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 879 | mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) { |
| 880 | skip |= LogPerformanceWarning( |
| 881 | device, kVUID_BestPractices_SmallDedicatedAllocation, |
| 882 | "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. " |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 883 | "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from " |
| 884 | "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)", |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 885 | api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize); |
| 886 | } |
| 887 | |
Rodrigo Locatti | 66b2335 | 2022-03-15 17:28:32 -0300 | [diff] [blame] | 888 | skip |= ValidateBindMemory(device, memory); |
| 889 | |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 890 | return skip; |
| 891 | } |
| 892 | |
| 893 | bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 894 | VkDeviceSize memoryOffset) const { |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 895 | bool skip = false; |
| 896 | const char* api_name = "BindBufferMemory()"; |
| 897 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 898 | skip |= ValidateBindBufferMemory(buffer, memory, api_name); |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 899 | |
| 900 | return skip; |
| 901 | } |
| 902 | |
| 903 | bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 904 | const VkBindBufferMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 905 | char api_name[64]; |
| 906 | bool skip = false; |
| 907 | |
| 908 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
Frédéric Wang | afb8986 | 2022-06-21 16:15:29 +0200 | [diff] [blame] | 909 | snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 910 | skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return skip; |
| 914 | } |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 915 | |
| 916 | bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 917 | const VkBindBufferMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 918 | char api_name[64]; |
| 919 | bool skip = false; |
Camden Stocker | b603cc8 | 2019-09-03 10:09:02 -0600 | [diff] [blame] | 920 | |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 921 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
Frédéric Wang | afb8986 | 2022-06-21 16:15:29 +0200 | [diff] [blame] | 922 | snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2KHR() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 923 | skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | return skip; |
| 927 | } |
| 928 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 929 | bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 930 | bool skip = false; |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 931 | auto image_state = Get<IMAGE_STATE>(image); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 932 | |
sfricke-samsung | 71bc657 | 2020-04-29 15:49:43 -0700 | [diff] [blame] | 933 | if (image_state->disjoint == false) { |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 934 | if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) { |
sfricke-samsung | d7ea5de | 2020-04-08 09:19:18 -0700 | [diff] [blame] | 935 | skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled, |
| 936 | "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.", |
| 937 | api_name, report_data->FormatHandle(image).c_str()); |
| 938 | } |
| 939 | } else { |
| 940 | // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each |
| 941 | // plane. |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 942 | } |
| 943 | |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 944 | auto mem_state = Get<DEVICE_MEMORY_STATE>(memory); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 945 | |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 946 | if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size && |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 947 | mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) { |
| 948 | skip |= LogPerformanceWarning( |
| 949 | device, kVUID_BestPractices_SmallDedicatedAllocation, |
| 950 | "%s: Trying to bind %s to a memory block which is fully consumed by the image. " |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 951 | "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from " |
| 952 | "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)", |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 953 | api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize); |
| 954 | } |
| 955 | |
| 956 | // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation, |
| 957 | // make sure this type is actually used. |
| 958 | // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT |
| 959 | // (i.e.most tile - based renderers) |
| 960 | if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) { |
| 961 | bool supports_lazy = false; |
| 962 | uint32_t suggested_type = 0; |
| 963 | |
| 964 | for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) { |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 965 | if ((1u << i) & image_state->requirements[0].memoryTypeBits) { |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 966 | if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { |
| 967 | supports_lazy = true; |
| 968 | suggested_type = i; |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags; |
| 975 | |
| 976 | if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) { |
| 977 | skip |= LogPerformanceWarning( |
| 978 | device, kVUID_BestPractices_NonLazyTransientImage, |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 979 | "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT," |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 980 | "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save " |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 981 | "%" PRIu64 " bytes of physical memory.", |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 982 | api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
Rodrigo Locatti | 66b2335 | 2022-03-15 17:28:32 -0300 | [diff] [blame] | 986 | skip |= ValidateBindMemory(device, memory); |
| 987 | |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 988 | return skip; |
| 989 | } |
| 990 | |
| 991 | bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 992 | VkDeviceSize memoryOffset) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 993 | bool skip = false; |
| 994 | const char* api_name = "vkBindImageMemory()"; |
| 995 | |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 996 | skip |= ValidateBindImageMemory(image, memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 997 | |
| 998 | return skip; |
| 999 | } |
| 1000 | |
| 1001 | bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1002 | const VkBindImageMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 1003 | char api_name[64]; |
| 1004 | bool skip = false; |
| 1005 | |
| 1006 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
Frédéric Wang | afb8986 | 2022-06-21 16:15:29 +0200 | [diff] [blame] | 1007 | snprintf(api_name, sizeof(api_name), "vkBindImageMemory2() pBindInfos[%u]", i); |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 1008 | if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) { |
Tony-LunarG | 5e60b85 | 2020-04-27 11:27:54 -0600 | [diff] [blame] | 1009 | skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name); |
| 1010 | } |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | return skip; |
| 1014 | } |
| 1015 | |
| 1016 | bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1017 | const VkBindImageMemoryInfo* pBindInfos) const { |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 1018 | char api_name[64]; |
| 1019 | bool skip = false; |
| 1020 | |
| 1021 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
Frédéric Wang | afb8986 | 2022-06-21 16:15:29 +0200 | [diff] [blame] | 1022 | snprintf(api_name, sizeof(api_name), "vkBindImageMemory2KHR() pBindInfos[%u]", i); |
Attilio Provenzano | f31788e | 2020-02-27 12:00:36 +0000 | [diff] [blame] | 1023 | skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name); |
Camden Stocker | 8b798ab | 2019-09-03 10:33:28 -0600 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | return skip; |
| 1027 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 1028 | |
Rodrigo Locatti | 66b2335 | 2022-03-15 17:28:32 -0300 | [diff] [blame] | 1029 | void BestPractices::PreCallRecordSetDeviceMemoryPriorityEXT(VkDevice device, VkDeviceMemory memory, float priority) { |
| 1030 | auto mem_info = std::static_pointer_cast<bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory)); |
| 1031 | mem_info->dynamic_priority.emplace(priority); |
| 1032 | } |
| 1033 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1034 | static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) { |
| 1035 | switch (format) { |
| 1036 | case VK_FORMAT_B10G11R11_UFLOAT_PACK32: |
| 1037 | case VK_FORMAT_R16_SFLOAT: |
| 1038 | case VK_FORMAT_R16G16_SFLOAT: |
| 1039 | case VK_FORMAT_R16G16B16_SFLOAT: |
| 1040 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 1041 | case VK_FORMAT_R32_SFLOAT: |
| 1042 | case VK_FORMAT_R32G32_SFLOAT: |
| 1043 | case VK_FORMAT_R32G32B32_SFLOAT: |
| 1044 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 1045 | return false; |
| 1046 | |
| 1047 | default: |
| 1048 | return true; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount, |
| 1053 | const VkGraphicsPipelineCreateInfo* pCreateInfos) const { |
| 1054 | bool skip = false; |
| 1055 | |
| 1056 | for (uint32_t i = 0; i < createInfoCount; i++) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1057 | auto create_info = &pCreateInfos[i]; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1058 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1059 | if (!create_info->pColorBlendState || !create_info->pMultisampleState || |
| 1060 | create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT || |
| 1061 | create_info->pMultisampleState->sampleShadingEnable) { |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1062 | return skip; |
| 1063 | } |
| 1064 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1065 | auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass); |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 1066 | const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass]; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1067 | |
Hans-Kristian Arntzen | c2742e7 | 2021-07-01 14:31:06 +0200 | [diff] [blame] | 1068 | // According to spec, pColorBlendState must be ignored if subpass does not have color attachments. |
| 1069 | uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount); |
| 1070 | |
| 1071 | for (uint32_t j = 0; j < num_color_attachments; j++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 1072 | const auto& blend_att = create_info->pColorBlendState->pAttachments[j]; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1073 | uint32_t att = subpass.pColorAttachments[j].attachment; |
| 1074 | |
| 1075 | if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) { |
| 1076 | if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) { |
| 1077 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending, |
| 1078 | "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and " |
| 1079 | "color attachment #%u makes use " |
| 1080 | "of a format which cannot be blended at full throughput when using MSAA.", |
| 1081 | VendorSpecificTag(kBPVendorArm), i, j); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | return skip; |
| 1088 | } |
| 1089 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1090 | void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 1091 | const VkComputePipelineCreateInfo* pCreateInfos, |
| 1092 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
| 1093 | VkResult result, void* pipe_state) { |
| 1094 | // AMD best practice |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1095 | pipeline_cache_ = pipelineCache; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1098 | bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 1099 | const VkGraphicsPipelineCreateInfo* pCreateInfos, |
Mark Lobodzinski | 2a162a0 | 2019-09-06 11:02:12 -0600 | [diff] [blame] | 1100 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1101 | void* cgpl_state_data) const { |
Mark Lobodzinski | 8317a3e | 2019-09-20 10:07:08 -0600 | [diff] [blame] | 1102 | bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, |
| 1103 | pAllocator, pPipelines, cgpl_state_data); |
ziga-lunarg | 08c8158 | 2022-03-08 17:33:45 +0100 | [diff] [blame] | 1104 | if (skip) { |
| 1105 | return skip; |
| 1106 | } |
Mark Lobodzinski | 8dd14d8 | 2020-04-10 14:16:33 -0600 | [diff] [blame] | 1107 | 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] | 1108 | |
| 1109 | if ((createInfoCount > 1) && (!pipelineCache)) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 1110 | skip |= LogPerformanceWarning( |
| 1111 | device, kVUID_BestPractices_CreatePipelines_MultiplePipelines, |
| 1112 | "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a " |
| 1113 | "pipeline cache, which may help with performance"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1114 | } |
| 1115 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1116 | for (uint32_t i = 0; i < createInfoCount; i++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 1117 | const auto& create_info = pCreateInfos[i]; |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1118 | |
Tony-LunarG | b6a2daf | 2022-07-29 11:30:55 -0600 | [diff] [blame] | 1119 | if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV) && create_info.pVertexInputState) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 1120 | const auto& vertex_input = *create_info.pVertexInputState; |
Mark Lobodzinski | 8dd14d8 | 2020-04-10 14:16:33 -0600 | [diff] [blame] | 1121 | uint32_t count = 0; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1122 | for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) { |
| 1123 | if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) { |
Mark Lobodzinski | 8dd14d8 | 2020-04-10 14:16:33 -0600 | [diff] [blame] | 1124 | count++; |
| 1125 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1126 | } |
Mark Lobodzinski | 8dd14d8 | 2020-04-10 14:16:33 -0600 | [diff] [blame] | 1127 | if (count > kMaxInstancedVertexBuffers) { |
| 1128 | skip |= LogPerformanceWarning( |
| 1129 | device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers, |
| 1130 | "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the " |
| 1131 | "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.", |
| 1132 | count, kMaxInstancedVertexBuffers); |
| 1133 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1134 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1135 | |
Szilard Papp | aaf2da3 | 2020-06-22 10:37:35 +0100 | [diff] [blame] | 1136 | if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) && |
| 1137 | (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) && |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 1138 | (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) && |
| 1139 | VendorCheckEnabled(kBPVendorArm)) { |
| 1140 | skip |= LogPerformanceWarning( |
| 1141 | device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero, |
| 1142 | "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true " |
| 1143 | "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced " |
| 1144 | "efficiency during rasterization. Consider disabling depthBias or increasing either " |
| 1145 | "depthBiasConstantFactor or depthBiasSlopeFactor.", |
| 1146 | VendorSpecificTag(kBPVendorArm)); |
Szilard Papp | aaf2da3 | 2020-06-22 10:37:35 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1149 | skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos); |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1150 | } |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1151 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1152 | auto prev_pipeline = pipeline_cache_.load(); |
| 1153 | if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1154 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1155 | "%s %s Performance Warning: A second pipeline cache is in use. " |
| 1156 | "Consider using only one pipeline cache to improve cache hit rate.", |
| 1157 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA)); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1158 | } |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1159 | } |
| 1160 | if (VendorCheckEnabled(kBPVendorAMD)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1161 | if (num_pso_ > kMaxRecommendedNumberOfPSOAMD) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1162 | skip |= |
| 1163 | LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines, |
| 1164 | "%s Performance warning: Too many pipelines created, consider consolidation", |
| 1165 | VendorSpecificTag(kBPVendorAMD)); |
| 1166 | } |
| 1167 | |
Nathaniel Cesario | 1a7e1a9 | 2021-08-30 14:34:20 -0600 | [diff] [blame] | 1168 | if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1169 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart, |
| 1170 | "%s Performance warning: Use of primitive restart is not recommended", |
| 1171 | VendorSpecificTag(kBPVendorAMD)); |
| 1172 | } |
| 1173 | |
| 1174 | // TODO: this might be too aggressive of a check |
| 1175 | if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) { |
| 1176 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates, |
| 1177 | "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed", |
| 1178 | VendorSpecificTag(kBPVendorAMD)); |
| 1179 | } |
| 1180 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 1181 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1182 | return skip; |
| 1183 | } |
| 1184 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1185 | static std::vector<bp_state::AttachmentInfo> GetAttachmentAccess(const safe_VkGraphicsPipelineCreateInfo& create_info, |
| 1186 | std::shared_ptr<const RENDER_PASS_STATE>& rp) { |
| 1187 | std::vector<bp_state::AttachmentInfo> result; |
Jeremy Gebben | b5dda54 | 2022-08-02 14:26:20 -0600 | [diff] [blame] | 1188 | if (!rp || rp->UsesDynamicRendering()) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1189 | return result; |
Hans-Kristian Arntzen | b033ab1 | 2021-06-16 11:16:59 +0200 | [diff] [blame] | 1190 | } |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1191 | |
| 1192 | const auto& subpass = rp->createInfo.pSubpasses[create_info.subpass]; |
| 1193 | |
| 1194 | // NOTE: see PIPELINE_LAYOUT and safe_VkGraphicsPipelineCreateInfo constructors. pColorBlendState and pDepthStencilState |
| 1195 | // are only non-null if they are enabled. |
| 1196 | if (create_info.pColorBlendState) { |
| 1197 | // According to spec, pColorBlendState must be ignored if subpass does not have color attachments. |
| 1198 | uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info.pColorBlendState->attachmentCount); |
| 1199 | for (uint32_t j = 0; j < num_color_attachments; j++) { |
| 1200 | if (create_info.pColorBlendState->pAttachments[j].colorWriteMask != 0) { |
| 1201 | uint32_t attachment = subpass.pColorAttachments[j].attachment; |
| 1202 | if (attachment != VK_ATTACHMENT_UNUSED) { |
| 1203 | result.push_back({attachment, VK_IMAGE_ASPECT_COLOR_BIT}); |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | if (create_info.pDepthStencilState && |
| 1210 | (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable || |
| 1211 | create_info.pDepthStencilState->stencilTestEnable)) { |
| 1212 | uint32_t attachment = subpass.pDepthStencilAttachment ? subpass.pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED; |
| 1213 | if (attachment != VK_ATTACHMENT_UNUSED) { |
| 1214 | VkImageAspectFlags aspects = 0; |
| 1215 | if (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable) { |
| 1216 | aspects |= VK_IMAGE_ASPECT_DEPTH_BIT; |
| 1217 | } |
| 1218 | if (create_info.pDepthStencilState->stencilTestEnable) { |
| 1219 | aspects |= VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1220 | } |
| 1221 | result.push_back({attachment, aspects}); |
| 1222 | } |
| 1223 | } |
| 1224 | return result; |
| 1225 | } |
| 1226 | |
| 1227 | bp_state::Pipeline::Pipeline(const ValidationStateTracker* state_data, const VkGraphicsPipelineCreateInfo* pCreateInfo, |
| 1228 | std::shared_ptr<const RENDER_PASS_STATE>&& rpstate, |
| 1229 | std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) |
| 1230 | : PIPELINE_STATE(state_data, pCreateInfo, std::move(rpstate), std::move(layout)), |
| 1231 | access_framebuffer_attachments(GetAttachmentAccess(create_info.graphics, rp_state)) {} |
| 1232 | |
| 1233 | std::shared_ptr<PIPELINE_STATE> BestPractices::CreateGraphicsPipelineState( |
| 1234 | const VkGraphicsPipelineCreateInfo* pCreateInfo, std::shared_ptr<const RENDER_PASS_STATE>&& render_pass, |
| 1235 | std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) const { |
| 1236 | return std::static_pointer_cast<PIPELINE_STATE>( |
| 1237 | std::make_shared<bp_state::Pipeline>(this, pCreateInfo, std::move(render_pass), std::move(layout))); |
Hans-Kristian Arntzen | b033ab1 | 2021-06-16 11:16:59 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1240 | void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1241 | const VkGraphicsPipelineCreateInfo* pCreateInfos, |
| 1242 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
| 1243 | VkResult result, void* cgpl_state_data) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1244 | // AMD best practice |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1245 | pipeline_cache_ = pipelineCache; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1248 | bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 1249 | const VkComputePipelineCreateInfo* pCreateInfos, |
Mark Lobodzinski | 2a162a0 | 2019-09-06 11:02:12 -0600 | [diff] [blame] | 1250 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1251 | void* ccpl_state_data) const { |
Mark Lobodzinski | 8317a3e | 2019-09-20 10:07:08 -0600 | [diff] [blame] | 1252 | bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, |
| 1253 | pAllocator, pPipelines, ccpl_state_data); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1254 | |
| 1255 | if ((createInfoCount > 1) && (!pipelineCache)) { |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 1256 | skip |= LogPerformanceWarning( |
| 1257 | device, kVUID_BestPractices_CreatePipelines_MultiplePipelines, |
| 1258 | "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a " |
| 1259 | "pipeline cache, which may help with performance"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1260 | } |
| 1261 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1262 | if (VendorCheckEnabled(kBPVendorAMD)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1263 | auto prev_pipeline = pipeline_cache_.load(); |
| 1264 | if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) { |
| 1265 | skip |= LogPerformanceWarning( |
| 1266 | device, kVUID_BestPractices_CreatePipelines_MultiplePipelines, |
| 1267 | "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to " |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1268 | "improve cache hit rate", |
| 1269 | VendorSpecificTag(kBPVendorAMD)); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1272 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1273 | for (uint32_t i = 0; i < createInfoCount; i++) { |
| 1274 | const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i]; |
| 1275 | if (VendorCheckEnabled(kBPVendorArm)) { |
| 1276 | skip |= ValidateCreateComputePipelineArm(createInfo); |
| 1277 | } |
sfricke-samsung | 86d055a | 2022-02-11 14:43:50 -0800 | [diff] [blame] | 1278 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1279 | if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) { |
| 1280 | auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module); |
| 1281 | for (const auto& builtin : module_state->static_data_.builtin_decoration_list) { |
| 1282 | if (builtin.builtin == spv::BuiltInWorkgroupSize) { |
| 1283 | skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize, |
| 1284 | "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32 |
| 1285 | "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 " |
| 1286 | "extension exposes a new LocalSizeId execution mode that should be used instead.", |
| 1287 | i); |
sfricke-samsung | 86d055a | 2022-02-11 14:43:50 -0800 | [diff] [blame] | 1288 | } |
| 1289 | } |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1290 | } |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1291 | } |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1292 | |
| 1293 | return skip; |
| 1294 | } |
| 1295 | |
| 1296 | bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const { |
| 1297 | bool skip = false; |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1298 | auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module); |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 1299 | // Generate warnings about work group sizes based on active resources. |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1300 | auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage); |
| 1301 | if (entrypoint == module_state->end()) return false; |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1302 | |
| 1303 | uint32_t x = 1, y = 1, z = 1; |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1304 | module_state->FindLocalSize(entrypoint, x, y, z); |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1305 | |
| 1306 | uint32_t thread_count = x * y * z; |
| 1307 | |
| 1308 | // Generate a priori warnings about work group sizes. |
| 1309 | if (thread_count > kMaxEfficientWorkGroupThreadCountArm) { |
| 1310 | skip |= LogPerformanceWarning( |
| 1311 | device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize, |
| 1312 | "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, " |
| 1313 | "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work " |
| 1314 | "groups with less than %u threads, especially when using barrier() or shared memory.", |
| 1315 | VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm); |
| 1316 | } |
| 1317 | |
| 1318 | if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) || |
| 1319 | ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) || |
| 1320 | ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) { |
| 1321 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment, |
| 1322 | "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, " |
| 1323 | "%u, %u) is not aligned to %u " |
| 1324 | "threads. On Arm Mali architectures, not aligning work group sizes to %u may " |
| 1325 | "leave threads idle on the shader " |
| 1326 | "core.", |
| 1327 | VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm, |
| 1328 | kThreadGroupDispatchCountAlignmentArm); |
| 1329 | } |
| 1330 | |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1331 | auto accessible_ids = module_state->MarkAccessibleIds(entrypoint); |
| 1332 | auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids); |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1333 | |
| 1334 | unsigned dimensions = 0; |
| 1335 | if (x > 1) dimensions++; |
| 1336 | if (y > 1) dimensions++; |
| 1337 | if (z > 1) dimensions++; |
| 1338 | // Here the dimension will really depend on the dispatch grid, but assume it's 1D. |
| 1339 | dimensions = std::max(dimensions, 1u); |
| 1340 | |
| 1341 | // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons. |
| 1342 | // There are some false positives here. We could simply have a shader that does this within a 1D grid, |
| 1343 | // or we may have a linearly tiled image, but these cases are quite unlikely in practice. |
| 1344 | bool accesses_2d = false; |
| 1345 | for (const auto& usage : descriptor_uses) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1346 | auto dim = module_state->GetShaderResourceDimensionality(usage.second); |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 1347 | if (dim < 0) continue; |
| 1348 | auto spvdim = spv::Dim(dim); |
| 1349 | if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true; |
| 1350 | } |
| 1351 | |
| 1352 | if (accesses_2d && dimensions < 2) { |
| 1353 | LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality, |
| 1354 | "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which " |
| 1355 | "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be " |
| 1356 | "exhibiting poor spatial locality with respect to one or more shader resources.", |
| 1357 | VendorSpecificTag(kBPVendorArm), x, y, z); |
| 1358 | } |
| 1359 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1360 | return skip; |
| 1361 | } |
| 1362 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1363 | bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1364 | bool skip = false; |
| 1365 | |
| 1366 | if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1367 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 1368 | "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] | 1369 | } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 1370 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 1371 | "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] | 1372 | } |
| 1373 | |
| 1374 | return skip; |
| 1375 | } |
| 1376 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1377 | bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const { |
| 1378 | bool skip = false; |
| 1379 | |
| 1380 | if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) { |
| 1381 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 1382 | "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str()); |
| 1383 | } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) { |
| 1384 | skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags, |
| 1385 | "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str()); |
| 1386 | } |
| 1387 | |
| 1388 | return skip; |
| 1389 | } |
| 1390 | |
| 1391 | bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const { |
| 1392 | bool skip = false; |
| 1393 | auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info); |
| 1394 | |
| 1395 | skip |= CheckPipelineStageFlags(api_name, stage_masks.src); |
| 1396 | skip |= CheckPipelineStageFlags(api_name, stage_masks.dst); |
| 1397 | |
| 1398 | return skip; |
| 1399 | } |
| 1400 | |
Mark Lobodzinski | 84101d7 | 2020-04-24 09:43:48 -0600 | [diff] [blame] | 1401 | void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) { |
Mark Lobodzinski | 9b133c1 | 2020-03-10 10:42:56 -0600 | [diff] [blame] | 1402 | for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) { |
| 1403 | auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result; |
| 1404 | if (swapchains_result == VK_SUBOPTIMAL_KHR) { |
| 1405 | LogPerformanceWarning( |
| 1406 | pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain, |
| 1407 | "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, " |
| 1408 | "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it " |
| 1409 | "targets. Applications should query updated surface information and recreate their swapchain at the next " |
| 1410 | "convenient opportunity.", |
| 1411 | report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str()); |
| 1412 | } |
| 1413 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1414 | |
| 1415 | // AMD best practice |
| 1416 | // end-of-frame cleanup |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1417 | num_queue_submissions_ = 0; |
| 1418 | num_barriers_objects_ = 0; |
| 1419 | ClearPipelinesUsedInFrame(); |
Mark Lobodzinski | 9b133c1 | 2020-03-10 10:42:56 -0600 | [diff] [blame] | 1420 | } |
| 1421 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1422 | bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, |
| 1423 | VkFence fence) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1424 | bool skip = false; |
| 1425 | |
| 1426 | for (uint32_t submit = 0; submit < submitCount; submit++) { |
| 1427 | for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) { |
| 1428 | skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]); |
| 1429 | } |
ziga-lunarg | c77f0c0 | 2022-04-18 00:15:16 +0200 | [diff] [blame] | 1430 | if (pSubmits[submit].signalSemaphoreCount == 0 && pSubmits[submit].pSignalSemaphores != nullptr) { |
| 1431 | skip |= |
| 1432 | LogWarning(device, kVUID_BestPractices_SemaphoreCount, |
| 1433 | "pSubmits[%" PRIu32 "].pSignalSemaphores is set, but pSubmits[%" PRIu32 "].signalSemaphoreCount is 0.", |
| 1434 | submit, submit); |
| 1435 | } |
| 1436 | if (pSubmits[submit].waitSemaphoreCount == 0 && pSubmits[submit].pWaitSemaphores != nullptr) { |
| 1437 | skip |= LogWarning(device, kVUID_BestPractices_SemaphoreCount, |
| 1438 | "pSubmits[%" PRIu32 "].pWaitSemaphores is set, but pSubmits[%" PRIu32 "].waitSemaphoreCount is 0.", |
| 1439 | submit, submit); |
| 1440 | } |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | return skip; |
| 1444 | } |
| 1445 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1446 | bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits, |
| 1447 | VkFence fence) const { |
| 1448 | bool skip = false; |
| 1449 | |
| 1450 | for (uint32_t submit = 0; submit < submitCount; submit++) { |
| 1451 | for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) { |
| 1452 | skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | return skip; |
| 1457 | } |
| 1458 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1459 | bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits, |
| 1460 | VkFence fence) const { |
| 1461 | bool skip = false; |
| 1462 | |
| 1463 | for (uint32_t submit = 0; submit < submitCount; submit++) { |
| 1464 | for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) { |
| 1465 | skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | return skip; |
| 1470 | } |
| 1471 | |
Attilio Provenzano | 746e43e | 2020-02-27 11:23:50 +0000 | [diff] [blame] | 1472 | bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo, |
| 1473 | const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const { |
| 1474 | bool skip = false; |
| 1475 | |
| 1476 | if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) { |
| 1477 | skip |= LogPerformanceWarning( |
| 1478 | device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset, |
| 1479 | "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire " |
| 1480 | "pool instead."); |
| 1481 | } |
| 1482 | |
| 1483 | return skip; |
| 1484 | } |
| 1485 | |
Rodrigo Locatti | c789fe8 | 2022-07-06 17:42:19 -0300 | [diff] [blame] | 1486 | void BestPractices::PreCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, |
| 1487 | const VkCommandBufferBeginInfo* pBeginInfo) { |
| 1488 | StateTracker::PreCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo); |
| 1489 | |
| 1490 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
paul-lunarg | 093c176 | 2022-08-23 18:52:10 +0200 | [diff] [blame] | 1491 | if (!cb) return; |
Rodrigo Locatti | c789fe8 | 2022-07-06 17:42:19 -0300 | [diff] [blame] | 1492 | |
| 1493 | cb->num_submits = 0; |
| 1494 | cb->is_one_time_submit = (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) != 0; |
| 1495 | } |
| 1496 | |
Attilio Provenzano | 746e43e | 2020-02-27 11:23:50 +0000 | [diff] [blame] | 1497 | bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer, |
| 1498 | const VkCommandBufferBeginInfo* pBeginInfo) const { |
| 1499 | bool skip = false; |
| 1500 | |
| 1501 | if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) { |
| 1502 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse, |
| 1503 | "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set."); |
| 1504 | } |
| 1505 | |
Rodrigo Locatti | c789fe8 | 2022-07-06 17:42:19 -0300 | [diff] [blame] | 1506 | if (VendorCheckEnabled(kBPVendorArm)) { |
Rodrigo Locatti | fe5172b | 2022-03-22 18:49:29 -0300 | [diff] [blame] | 1507 | if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) { |
| 1508 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit, |
Rodrigo Locatti | c789fe8 | 2022-07-06 17:42:19 -0300 | [diff] [blame] | 1509 | "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. " |
| 1510 | "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.", |
| 1511 | VendorSpecificTag(kBPVendorArm)); |
| 1512 | } |
| 1513 | } |
| 1514 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1515 | auto cb = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 1516 | if (cb->num_submits == 1 && !cb->is_one_time_submit) { |
| 1517 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit, |
| 1518 | "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT was not set " |
| 1519 | "and the command buffer has only been submitted once. " |
| 1520 | "For best performance on NVIDIA GPUs, use ONE_TIME_SUBMIT.", |
| 1521 | VendorSpecificTag(kBPVendorNVIDIA)); |
Rodrigo Locatti | fe5172b | 2022-03-22 18:49:29 -0300 | [diff] [blame] | 1522 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Attilio Provenzano | 746e43e | 2020-02-27 11:23:50 +0000 | [diff] [blame] | 1525 | return skip; |
| 1526 | } |
| 1527 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1528 | bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1529 | bool skip = false; |
| 1530 | |
| 1531 | skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask); |
| 1532 | |
| 1533 | return skip; |
| 1534 | } |
| 1535 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1536 | bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event, |
| 1537 | const VkDependencyInfoKHR* pDependencyInfo) const { |
| 1538 | return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo); |
| 1539 | } |
| 1540 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1541 | bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event, |
| 1542 | const VkDependencyInfo* pDependencyInfo) const { |
| 1543 | return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo); |
| 1544 | } |
| 1545 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1546 | bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, |
| 1547 | VkPipelineStageFlags stageMask) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1548 | bool skip = false; |
| 1549 | |
| 1550 | skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask); |
| 1551 | |
| 1552 | return skip; |
| 1553 | } |
| 1554 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1555 | bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event, |
| 1556 | VkPipelineStageFlags2KHR stageMask) const { |
| 1557 | bool skip = false; |
| 1558 | |
| 1559 | skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask); |
| 1560 | |
| 1561 | return skip; |
| 1562 | } |
| 1563 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1564 | bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, |
| 1565 | VkPipelineStageFlags2 stageMask) const { |
| 1566 | bool skip = false; |
| 1567 | |
| 1568 | skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask); |
| 1569 | |
| 1570 | return skip; |
| 1571 | } |
| 1572 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1573 | bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents, |
| 1574 | VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, |
| 1575 | uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, |
| 1576 | uint32_t bufferMemoryBarrierCount, |
| 1577 | const VkBufferMemoryBarrier* pBufferMemoryBarriers, |
| 1578 | uint32_t imageMemoryBarrierCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1579 | const VkImageMemoryBarrier* pImageMemoryBarriers) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1580 | bool skip = false; |
| 1581 | |
| 1582 | skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask); |
| 1583 | skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask); |
| 1584 | |
| 1585 | return skip; |
| 1586 | } |
| 1587 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1588 | bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents, |
| 1589 | const VkDependencyInfoKHR* pDependencyInfos) const { |
| 1590 | bool skip = false; |
| 1591 | for (uint32_t i = 0; i < eventCount; i++) { |
| 1592 | skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]); |
| 1593 | } |
| 1594 | |
| 1595 | return skip; |
| 1596 | } |
| 1597 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1598 | bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents, |
| 1599 | const VkDependencyInfo* pDependencyInfos) const { |
| 1600 | bool skip = false; |
| 1601 | for (uint32_t i = 0; i < eventCount; i++) { |
| 1602 | skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]); |
| 1603 | } |
| 1604 | |
| 1605 | return skip; |
| 1606 | } |
| 1607 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1608 | bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1609 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1610 | uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, |
| 1611 | uint32_t bufferMemoryBarrierCount, |
| 1612 | const VkBufferMemoryBarrier* pBufferMemoryBarriers, |
| 1613 | uint32_t imageMemoryBarrierCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1614 | const VkImageMemoryBarrier* pImageMemoryBarriers) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1615 | bool skip = false; |
| 1616 | |
| 1617 | skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask); |
| 1618 | skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask); |
| 1619 | |
ziga-lunarg | b65dbfb | 2022-03-19 18:45:09 +0100 | [diff] [blame] | 1620 | for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) { |
| 1621 | if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && |
| 1622 | IsImageLayoutReadOnly(pImageMemoryBarriers[i].newLayout)) { |
| 1623 | skip |= LogWarning(device, kVUID_BestPractices_TransitionUndefinedToReadOnly, |
| 1624 | "VkImageMemoryBarrier is being submitted with oldLayout VK_IMAGE_LAYOUT_UNDEFINED and the contents " |
| 1625 | "may be discarded, but the newLayout is %s, which is read only.", |
| 1626 | string_VkImageLayout(pImageMemoryBarriers[i].newLayout)); |
| 1627 | } |
| 1628 | } |
| 1629 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1630 | if (VendorCheckEnabled(kBPVendorAMD)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1631 | auto num = num_barriers_objects_.load(); |
| 1632 | if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1633 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount, |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1634 | "%s Performance warning: In this frame, %" PRIu32 |
| 1635 | " barriers were already submitted. Barriers have a high cost and can " |
| 1636 | "stall the GPU. " |
| 1637 | "Consider consolidating and re-organizing the frame to use fewer barriers.", |
| 1638 | VendorSpecificTag(kBPVendorAMD), num); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1639 | } |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1640 | } |
| 1641 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1642 | static constexpr std::array<VkImageLayout, 3> read_layouts = { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1643 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, |
| 1644 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 1645 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1646 | }; |
| 1647 | |
| 1648 | for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) { |
| 1649 | // read to read barriers |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1650 | const auto &image_barrier = pImageMemoryBarriers[i]; |
| 1651 | bool old_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.oldLayout) != read_layouts.end(); |
| 1652 | bool new_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.newLayout) != read_layouts.end(); |
| 1653 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1654 | if (old_is_read_layout && new_is_read_layout) { |
| 1655 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1656 | "%s %s Performance warning: Don't issue read-to-read barriers. " |
| 1657 | "Get the resource in the right state the first time you use it.", |
| 1658 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA)); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | // general with no storage |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 1662 | if (VendorCheckEnabled(kBPVendorAMD) && image_barrier.newLayout == VK_IMAGE_LAYOUT_GENERAL) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1663 | auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image); |
| 1664 | if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) { |
| 1665 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral, |
| 1666 | "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with " |
| 1667 | "VK_IMAGE_USAGE_STORAGE_BIT images.", |
| 1668 | VendorSpecificTag(kBPVendorAMD)); |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1674 | for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) { |
| 1675 | skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pImageMemoryBarriers[i]); |
| 1676 | } |
| 1677 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1678 | return skip; |
| 1679 | } |
| 1680 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1681 | bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, |
| 1682 | const VkDependencyInfoKHR* pDependencyInfo) const { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1683 | bool skip = false; |
| 1684 | |
| 1685 | skip |= CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo); |
| 1686 | |
| 1687 | for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) { |
| 1688 | skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]); |
| 1689 | } |
| 1690 | |
| 1691 | return skip; |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1694 | bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer, |
| 1695 | const VkDependencyInfo* pDependencyInfo) const { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1696 | bool skip = false; |
| 1697 | |
| 1698 | skip |= CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo); |
| 1699 | |
| 1700 | for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) { |
| 1701 | skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]); |
| 1702 | } |
| 1703 | |
| 1704 | return skip; |
| 1705 | } |
| 1706 | |
| 1707 | template <typename ImageMemoryBarrier> |
| 1708 | bool BestPractices::ValidateCmdPipelineBarrierImageBarrier(VkCommandBuffer commandBuffer, |
| 1709 | const ImageMemoryBarrier& barrier) const { |
| 1710 | |
| 1711 | bool skip = false; |
| 1712 | |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 1713 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 1714 | assert(cmd_state); |
| 1715 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1716 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1717 | if (barrier.oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && barrier.newLayout != VK_IMAGE_LAYOUT_UNDEFINED) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 1718 | skip |= ValidateZcull(*cmd_state, barrier.image, barrier.subresourceRange); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | return skip; |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1725 | bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1726 | VkQueryPool queryPool, uint32_t query) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1727 | bool skip = false; |
| 1728 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1729 | skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage)); |
| 1730 | |
| 1731 | return skip; |
| 1732 | } |
| 1733 | |
| 1734 | bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage, |
| 1735 | VkQueryPool queryPool, uint32_t query) const { |
| 1736 | bool skip = false; |
| 1737 | |
| 1738 | skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 1739 | |
| 1740 | return skip; |
| 1741 | } |
| 1742 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 1743 | bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage, |
| 1744 | VkQueryPool queryPool, uint32_t query) const { |
| 1745 | bool skip = false; |
| 1746 | |
| 1747 | skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage); |
| 1748 | |
| 1749 | return skip; |
| 1750 | } |
| 1751 | |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 1752 | void BestPractices::PreCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, |
| 1753 | VkPipeline pipeline) { |
| 1754 | StateTracker::PreCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline); |
| 1755 | |
| 1756 | auto pipeline_info = Get<PIPELINE_STATE>(pipeline); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1757 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 1758 | |
| 1759 | assert(pipeline_info); |
| 1760 | assert(cb); |
| 1761 | |
| 1762 | if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS && VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1763 | using TessGeometryMeshState = bp_state::CommandBufferStateNV::TessGeometryMesh::State; |
| 1764 | auto& tgm = cb->nv.tess_geometry_mesh; |
| 1765 | |
| 1766 | // Make sure the message is only signaled once per command buffer |
| 1767 | tgm.threshold_signaled = tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA; |
| 1768 | |
| 1769 | // Track pipeline switches with tessellation, geometry, and/or mesh shaders enabled, and disabled |
| 1770 | auto tgm_stages = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | |
| 1771 | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_TASK_BIT_NV | VK_SHADER_STAGE_MESH_BIT_NV; |
| 1772 | auto new_tgm_state = (pipeline_info->active_shaders & tgm_stages) != 0 |
| 1773 | ? TessGeometryMeshState::Enabled |
| 1774 | : TessGeometryMeshState::Disabled; |
| 1775 | if (tgm.state != new_tgm_state && tgm.state != TessGeometryMeshState::Unknown) { |
| 1776 | tgm.num_switches++; |
| 1777 | } |
| 1778 | tgm.state = new_tgm_state; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1779 | |
| 1780 | // Track depthTestEnable and depthCompareOp |
| 1781 | auto &pipeline_create_info = pipeline_info->GetCreateInfo<VkGraphicsPipelineCreateInfo>(); |
| 1782 | auto depth_stencil_state = pipeline_create_info.pDepthStencilState; |
| 1783 | auto dynamic_state = pipeline_create_info.pDynamicState; |
| 1784 | if (depth_stencil_state && dynamic_state) { |
| 1785 | auto dynamic_state_begin = dynamic_state->pDynamicStates; |
| 1786 | auto dynamic_state_end = dynamic_state->pDynamicStates + dynamic_state->dynamicStateCount; |
| 1787 | |
| 1788 | bool dynamic_depth_test_enable = std::find(dynamic_state_begin, dynamic_state_end, VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE) != dynamic_state_end; |
| 1789 | bool dynamic_depth_func = std::find(dynamic_state_begin, dynamic_state_end, VK_DYNAMIC_STATE_DEPTH_COMPARE_OP) != dynamic_state_end; |
| 1790 | |
| 1791 | if (!dynamic_depth_test_enable) { |
| 1792 | RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depth_stencil_state->depthTestEnable != VK_FALSE); |
| 1793 | } |
| 1794 | if (!dynamic_depth_func) { |
| 1795 | RecordSetDepthTestState(*cb, depth_stencil_state->depthCompareOp, cb->nv.depth_test_enable); |
| 1796 | } |
| 1797 | } |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1801 | void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, |
| 1802 | VkPipeline pipeline) { |
| 1803 | StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline); |
| 1804 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1805 | // AMD best practice |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1806 | PipelineUsedInFrame(pipeline); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 1807 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1808 | if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1809 | auto pipeline_state = Get<bp_state::Pipeline>(pipeline); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1810 | // check for depth/blend state tracking |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1811 | if (pipeline_state) { |
| 1812 | auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 1813 | assert(cb_node); |
| 1814 | auto& render_pass_state = cb_node->render_pass_state; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1815 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 1816 | render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments; |
Hans-Kristian Arntzen | 56232b9 | 2021-06-16 14:37:48 +0200 | [diff] [blame] | 1817 | render_pass_state.drawTouchAttachments = true; |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 1818 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 1819 | const auto* blend_state = pipeline_state->ColorBlendState(); |
| 1820 | const auto* stencil_state = pipeline_state->DepthStencilState(); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1821 | |
| 1822 | if (blend_state) { |
| 1823 | // assume the pipeline is depth-only unless any of the attachments have color writes enabled |
Hans-Kristian Arntzen | 56232b9 | 2021-06-16 14:37:48 +0200 | [diff] [blame] | 1824 | render_pass_state.depthOnly = true; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1825 | for (size_t i = 0; i < blend_state->attachmentCount; i++) { |
| 1826 | if (blend_state->pAttachments[i].colorWriteMask != 0) { |
Hans-Kristian Arntzen | 56232b9 | 2021-06-16 14:37:48 +0200 | [diff] [blame] | 1827 | render_pass_state.depthOnly = false; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | // check for depth value usage |
Hans-Kristian Arntzen | 56232b9 | 2021-06-16 14:37:48 +0200 | [diff] [blame] | 1833 | render_pass_state.depthEqualComparison = false; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1834 | |
| 1835 | if (stencil_state && stencil_state->depthTestEnable) { |
| 1836 | switch (stencil_state->depthCompareOp) { |
| 1837 | case VK_COMPARE_OP_EQUAL: |
| 1838 | case VK_COMPARE_OP_GREATER_OR_EQUAL: |
| 1839 | case VK_COMPARE_OP_LESS_OR_EQUAL: |
Hans-Kristian Arntzen | 56232b9 | 2021-06-16 14:37:48 +0200 | [diff] [blame] | 1840 | render_pass_state.depthEqualComparison = true; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1841 | break; |
| 1842 | default: |
| 1843 | break; |
| 1844 | } |
| 1845 | } |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1850 | void BestPractices::PreCallRecordCmdSetDepthCompareOp(VkCommandBuffer commandBuffer, VkCompareOp depthCompareOp) { |
| 1851 | StateTracker::PreCallRecordCmdSetDepthCompareOp(commandBuffer, depthCompareOp); |
| 1852 | |
| 1853 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1854 | assert(cb); |
| 1855 | |
| 1856 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1857 | RecordSetDepthTestState(*cb, depthCompareOp, cb->nv.depth_test_enable); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | void BestPractices::PreCallRecordCmdSetDepthCompareOpEXT(VkCommandBuffer commandBuffer, VkCompareOp depthCompareOp) { |
| 1862 | StateTracker::PreCallRecordCmdSetDepthCompareOpEXT(commandBuffer, depthCompareOp); |
| 1863 | |
| 1864 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1865 | assert(cb); |
| 1866 | |
| 1867 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1868 | RecordSetDepthTestState(*cb, depthCompareOp, cb->nv.depth_test_enable); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | void BestPractices::PreCallRecordCmdSetDepthTestEnable(VkCommandBuffer commandBuffer, VkBool32 depthTestEnable) { |
| 1873 | StateTracker::PreCallRecordCmdSetDepthTestEnable(commandBuffer, depthTestEnable); |
| 1874 | |
| 1875 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1876 | assert(cb); |
| 1877 | |
| 1878 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1879 | RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depthTestEnable != VK_FALSE); |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | void BestPractices::PreCallRecordCmdSetDepthTestEnableEXT(VkCommandBuffer commandBuffer, VkBool32 depthTestEnable) { |
| 1884 | StateTracker::PreCallRecordCmdSetDepthTestEnableEXT(commandBuffer, depthTestEnable); |
| 1885 | |
| 1886 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1887 | assert(cb); |
| 1888 | |
| 1889 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1890 | RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depthTestEnable != VK_FALSE); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | void BestPractices::RecordSetDepthTestState(bp_state::CommandBuffer& cmd_state, VkCompareOp new_depth_compare_op, bool new_depth_test_enable) { |
| 1895 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 1896 | |
| 1897 | if (cmd_state.nv.depth_compare_op != new_depth_compare_op) { |
| 1898 | switch (new_depth_compare_op) { |
| 1899 | case VK_COMPARE_OP_LESS: |
| 1900 | case VK_COMPARE_OP_LESS_OR_EQUAL: |
| 1901 | cmd_state.nv.zcull_direction = bp_state::CommandBufferStateNV::ZcullDirection::Less; |
| 1902 | break; |
| 1903 | case VK_COMPARE_OP_GREATER: |
| 1904 | case VK_COMPARE_OP_GREATER_OR_EQUAL: |
| 1905 | cmd_state.nv.zcull_direction = bp_state::CommandBufferStateNV::ZcullDirection::Greater; |
| 1906 | break; |
| 1907 | default: |
| 1908 | // The other ops carry over the previous state. |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
| 1912 | cmd_state.nv.depth_compare_op = new_depth_compare_op; |
| 1913 | cmd_state.nv.depth_test_enable = new_depth_test_enable; |
| 1914 | } |
| 1915 | |
| 1916 | void BestPractices::RecordCmdBeginRenderingCommon(VkCommandBuffer commandBuffer) { |
| 1917 | auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1918 | assert(cmd_state); |
| 1919 | |
| 1920 | auto rp = cmd_state->activeRenderPass.get(); |
| 1921 | assert(rp); |
| 1922 | |
| 1923 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1924 | std::shared_ptr<IMAGE_VIEW_STATE> depth_image_view_shared_ptr; |
| 1925 | IMAGE_VIEW_STATE* depth_image_view = nullptr; |
| 1926 | layer_data::optional<VkAttachmentLoadOp> load_op; |
| 1927 | |
| 1928 | if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) { |
| 1929 | const auto depth_attachment = rp->dynamic_rendering_begin_rendering_info.pDepthAttachment; |
| 1930 | if (depth_attachment) { |
| 1931 | load_op.emplace(depth_attachment->loadOp); |
| 1932 | depth_image_view_shared_ptr = Get<IMAGE_VIEW_STATE>(depth_attachment->imageView); |
| 1933 | depth_image_view = depth_image_view_shared_ptr.get(); |
| 1934 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 1935 | |
| 1936 | for (uint32_t i = 0; i < rp->dynamic_rendering_begin_rendering_info.colorAttachmentCount; ++i) { |
| 1937 | const auto& color_attachment = rp->dynamic_rendering_begin_rendering_info.pColorAttachments[i]; |
| 1938 | if (color_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 1939 | const VkFormat format = Get<IMAGE_VIEW_STATE>(color_attachment.imageView)->create_info.format; |
| 1940 | RecordClearColor(format, color_attachment.clearValue.color); |
| 1941 | } |
| 1942 | } |
| 1943 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1944 | } else { |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 1945 | if (rp->createInfo.pAttachments) { |
| 1946 | if (rp->createInfo.subpassCount > 0) { |
| 1947 | const auto depth_attachment = rp->createInfo.pSubpasses[0].pDepthStencilAttachment; |
| 1948 | if (depth_attachment) { |
| 1949 | const uint32_t attachment_index = depth_attachment->attachment; |
| 1950 | if (attachment_index != VK_ATTACHMENT_UNUSED) { |
| 1951 | load_op.emplace(rp->createInfo.pAttachments[attachment_index].loadOp); |
| 1952 | depth_image_view = (*cmd_state->active_attachments)[attachment_index]; |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | for (uint32_t i = 0; i < cmd_state->activeRenderPassBeginInfo.clearValueCount; ++i) { |
| 1957 | const auto& attachment = rp->createInfo.pAttachments[i]; |
| 1958 | if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 1959 | const auto& clear_color = cmd_state->activeRenderPassBeginInfo.pClearValues[i].color; |
| 1960 | RecordClearColor(attachment.format, clear_color); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | if (depth_image_view && (depth_image_view->create_info.subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U) { |
| 1966 | const VkImage depth_image = depth_image_view->image_state->image(); |
| 1967 | const VkImageSubresourceRange& subresource_range = depth_image_view->create_info.subresourceRange; |
| 1968 | RecordBindZcullScope(*cmd_state, depth_image, subresource_range); |
| 1969 | } else { |
| 1970 | RecordUnbindZcullScope(*cmd_state); |
| 1971 | } |
| 1972 | if (load_op) { |
| 1973 | if (*load_op == VK_ATTACHMENT_LOAD_OP_CLEAR || *load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE) { |
| 1974 | RecordResetScopeZcullDirection(*cmd_state); |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | void BestPractices::RecordCmdEndRenderingCommon(VkCommandBuffer commandBuffer) { |
| 1981 | auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 1982 | assert(cmd_state); |
| 1983 | |
| 1984 | auto rp = cmd_state->activeRenderPass.get(); |
| 1985 | assert(rp); |
| 1986 | |
| 1987 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 1988 | layer_data::optional<VkAttachmentStoreOp> store_op; |
| 1989 | |
| 1990 | if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) { |
| 1991 | const auto depth_attachment = rp->dynamic_rendering_begin_rendering_info.pDepthAttachment; |
| 1992 | if (depth_attachment) { |
| 1993 | store_op.emplace(depth_attachment->storeOp); |
| 1994 | } |
| 1995 | } else { |
| 1996 | if (rp->createInfo.subpassCount > 0) { |
| 1997 | const uint32_t last_subpass = rp->createInfo.subpassCount - 1; |
| 1998 | const auto depth_attachment = rp->createInfo.pSubpasses[last_subpass].pDepthStencilAttachment; |
| 1999 | if (depth_attachment) { |
| 2000 | const uint32_t attachment = depth_attachment->attachment; |
| 2001 | if (attachment != VK_ATTACHMENT_UNUSED) { |
| 2002 | store_op.emplace(rp->createInfo.pAttachments[attachment].storeOp); |
| 2003 | } |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | if (store_op) { |
| 2009 | if (*store_op == VK_ATTACHMENT_STORE_OP_DONT_CARE || *store_op == VK_ATTACHMENT_STORE_OP_NONE) { |
| 2010 | RecordResetScopeZcullDirection(*cmd_state); |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | RecordUnbindZcullScope(*cmd_state); |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | void BestPractices::RecordBindZcullScope(bp_state::CommandBuffer& cmd_state, VkImage depth_attachment, const VkImageSubresourceRange& subresource_range) { |
| 2019 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2020 | |
| 2021 | if (depth_attachment == VK_NULL_HANDLE) { |
| 2022 | cmd_state.nv.zcull_scope = {}; |
| 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | assert((subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U); |
| 2027 | |
| 2028 | auto image_state = Get<IMAGE_STATE>(depth_attachment); |
| 2029 | assert(image_state); |
| 2030 | |
| 2031 | const uint32_t mip_levels = image_state->createInfo.mipLevels; |
| 2032 | const uint32_t array_layers = image_state->createInfo.arrayLayers; |
| 2033 | |
| 2034 | auto& tree = cmd_state.nv.zcull_per_image[depth_attachment]; |
| 2035 | if (tree.states.empty()) { |
| 2036 | tree.mip_levels = mip_levels; |
| 2037 | tree.array_layers = array_layers; |
| 2038 | tree.states.resize(array_layers * mip_levels); |
| 2039 | } |
| 2040 | |
| 2041 | cmd_state.nv.zcull_scope.image = depth_attachment; |
| 2042 | cmd_state.nv.zcull_scope.range = subresource_range; |
| 2043 | cmd_state.nv.zcull_scope.tree = &tree; |
| 2044 | } |
| 2045 | |
| 2046 | void BestPractices::RecordUnbindZcullScope(bp_state::CommandBuffer& cmd_state) { |
| 2047 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2048 | |
| 2049 | RecordBindZcullScope(cmd_state, VK_NULL_HANDLE, VkImageSubresourceRange{}); |
| 2050 | } |
| 2051 | |
| 2052 | void BestPractices::RecordResetScopeZcullDirection(bp_state::CommandBuffer& cmd_state) { |
| 2053 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2054 | |
| 2055 | auto& scope = cmd_state.nv.zcull_scope; |
| 2056 | RecordResetZcullDirection(cmd_state, scope.image, scope.range); |
| 2057 | } |
| 2058 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2059 | template <typename Func> |
| 2060 | static void ForEachSubresource(const IMAGE_STATE& image, const VkImageSubresourceRange& range, Func&& func) |
| 2061 | { |
paul-lunarg | 0ec9556 | 2022-09-08 16:02:53 +0200 | [diff] [blame] | 2062 | const uint32_t layerCount = |
| 2063 | (range.layerCount == VK_REMAINING_ARRAY_LAYERS) ? (image.full_range.layerCount - range.baseArrayLayer) : range.layerCount; |
| 2064 | const uint32_t levelCount = |
| 2065 | (range.levelCount == VK_REMAINING_MIP_LEVELS) ? (image.full_range.levelCount - range.baseMipLevel) : range.levelCount; |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2066 | |
| 2067 | for (uint32_t i = 0; i < layerCount; ++i) { |
| 2068 | const uint32_t layer = range.baseArrayLayer + i; |
| 2069 | for (uint32_t j = 0; j < levelCount; ++j) { |
| 2070 | const uint32_t level = range.baseMipLevel + j; |
| 2071 | func(layer, level); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2076 | void BestPractices::RecordResetZcullDirection(bp_state::CommandBuffer& cmd_state, VkImage depth_image, |
| 2077 | const VkImageSubresourceRange& subresource_range) { |
| 2078 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2079 | |
| 2080 | RecordSetZcullDirection(cmd_state, depth_image, subresource_range, bp_state::CommandBufferStateNV::ZcullDirection::Unknown); |
| 2081 | |
| 2082 | const auto image_it = cmd_state.nv.zcull_per_image.find(depth_image); |
| 2083 | if (image_it == cmd_state.nv.zcull_per_image.end()) { |
| 2084 | return; |
| 2085 | } |
| 2086 | auto& tree = image_it->second; |
| 2087 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2088 | auto image = Get<IMAGE_STATE>(depth_image); |
| 2089 | if (!image) return; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2090 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2091 | ForEachSubresource(*image, subresource_range, [&tree](uint32_t layer, uint32_t level) { |
| 2092 | auto& subresource = tree.GetState(layer, level); |
| 2093 | subresource.num_less_draws = 0; |
| 2094 | subresource.num_greater_draws = 0; |
| 2095 | }); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | void BestPractices::RecordSetScopeZcullDirection(bp_state::CommandBuffer& cmd_state, bp_state::CommandBufferStateNV::ZcullDirection mode) { |
| 2099 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2100 | |
| 2101 | auto& scope = cmd_state.nv.zcull_scope; |
| 2102 | RecordSetZcullDirection(cmd_state, scope.image, scope.range, mode); |
| 2103 | } |
| 2104 | |
| 2105 | void BestPractices::RecordSetZcullDirection(bp_state::CommandBuffer& cmd_state, VkImage depth_image, |
| 2106 | const VkImageSubresourceRange& subresource_range, |
| 2107 | bp_state::CommandBufferStateNV::ZcullDirection mode) { |
| 2108 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2109 | |
| 2110 | const auto image_it = cmd_state.nv.zcull_per_image.find(depth_image); |
| 2111 | if (image_it == cmd_state.nv.zcull_per_image.end()) { |
| 2112 | return; |
| 2113 | } |
| 2114 | auto& tree = image_it->second; |
| 2115 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2116 | auto image = Get<IMAGE_STATE>(depth_image); |
| 2117 | if (!image) return; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2118 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2119 | ForEachSubresource(*image, subresource_range, [&tree, &cmd_state](uint32_t layer, uint32_t level) { |
| 2120 | tree.GetState(layer, level).direction = cmd_state.nv.zcull_direction; |
| 2121 | }); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | void BestPractices::RecordZcullDraw(bp_state::CommandBuffer& cmd_state) { |
| 2125 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2126 | |
| 2127 | // Add one draw to each subresource depending on the current Z-cull direction |
| 2128 | auto& scope = cmd_state.nv.zcull_scope; |
| 2129 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2130 | auto image = Get<IMAGE_STATE>(scope.image); |
| 2131 | if (!image) return; |
| 2132 | |
| 2133 | ForEachSubresource(*image, scope.range, [&scope](uint32_t layer, uint32_t level) { |
| 2134 | auto& subresource = scope.tree->GetState(layer, level); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2135 | |
| 2136 | switch (subresource.direction) { |
| 2137 | case bp_state::CommandBufferStateNV::ZcullDirection::Unknown: |
| 2138 | // Unreachable |
| 2139 | assert(0); |
| 2140 | break; |
| 2141 | case bp_state::CommandBufferStateNV::ZcullDirection::Less: |
| 2142 | ++subresource.num_less_draws; |
| 2143 | break; |
| 2144 | case bp_state::CommandBufferStateNV::ZcullDirection::Greater: |
| 2145 | ++subresource.num_greater_draws; |
| 2146 | break; |
| 2147 | } |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2148 | }); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2149 | } |
| 2150 | |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2151 | bool BestPractices::ValidateZcullScope(const bp_state::CommandBuffer& cmd_state) const { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2152 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2153 | |
| 2154 | bool skip = false; |
| 2155 | |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2156 | if (cmd_state.nv.depth_test_enable) { |
| 2157 | auto& scope = cmd_state.nv.zcull_scope; |
| 2158 | skip |= ValidateZcull(cmd_state, scope.image, scope.range); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | return skip; |
| 2162 | } |
| 2163 | |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2164 | bool BestPractices::ValidateZcull(const bp_state::CommandBuffer& cmd_state, VkImage image, |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2165 | const VkImageSubresourceRange& subresource_range) const { |
| 2166 | bool skip = false; |
| 2167 | |
| 2168 | const char* good_mode = nullptr; |
| 2169 | const char* bad_mode = nullptr; |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2170 | bool is_balanced = false; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2171 | |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2172 | const auto image_it = cmd_state.nv.zcull_per_image.find(image); |
| 2173 | if (image_it == cmd_state.nv.zcull_per_image.end()) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2174 | return skip; |
| 2175 | } |
| 2176 | const auto& tree = image_it->second; |
| 2177 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2178 | auto image_state = Get<IMAGE_STATE>(image); |
| 2179 | if (!image_state) { |
| 2180 | return skip; |
| 2181 | } |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2182 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2183 | ForEachSubresource(*image_state, subresource_range, [&](uint32_t layer, uint32_t level) { |
| 2184 | if (is_balanced) { |
| 2185 | return; |
| 2186 | } |
| 2187 | const auto& resource = tree.GetState(layer, level); |
| 2188 | const uint64_t num_draws = resource.num_less_draws + resource.num_greater_draws; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2189 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2190 | if (num_draws == 0) { |
| 2191 | return; |
| 2192 | } |
| 2193 | const uint64_t less_ratio = (resource.num_less_draws * 100) / num_draws; |
| 2194 | const uint64_t greater_ratio = (resource.num_greater_draws * 100) / num_draws; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2195 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2196 | if ((less_ratio > kZcullDirectionBalanceRatioNVIDIA) && (greater_ratio > kZcullDirectionBalanceRatioNVIDIA)) { |
| 2197 | is_balanced = true; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2198 | |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2199 | if (greater_ratio > less_ratio) { |
| 2200 | good_mode = "GREATER"; |
| 2201 | bad_mode = "LESS"; |
| 2202 | } else { |
| 2203 | good_mode = "LESS"; |
| 2204 | bad_mode = "GREATER"; |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2205 | } |
| 2206 | } |
Rodrigo Locatti | c94a5cd | 2022-08-24 18:33:07 -0300 | [diff] [blame] | 2207 | }); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2208 | |
| 2209 | if (is_balanced) { |
| 2210 | skip |= LogPerformanceWarning( |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2211 | cmd_state.commandBuffer(), kVUID_BestPractices_Zcull_LessGreaterRatio, |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2212 | "%s Depth attachment %s is primarily rendered with depth compare op %s, but some draws use %s. " |
| 2213 | "Z-cull is disabled for the least used direction, which harms depth testing performance. " |
| 2214 | "The Z-cull direction can be reset by clearing the depth attachment, transitioning from VK_IMAGE_LAYOUT_UNDEFINED, " |
| 2215 | "using VK_ATTACHMENT_LOAD_OP_DONT_CARE, or using VK_ATTACHMENT_STORE_OP_DONT_CARE.", |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 2216 | VendorSpecificTag(kBPVendorNVIDIA), report_data->FormatHandle(cmd_state.nv.zcull_scope.image).c_str(), good_mode, |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2217 | bad_mode); |
| 2218 | } |
| 2219 | |
| 2220 | return skip; |
| 2221 | } |
| 2222 | |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 2223 | static std::array<uint32_t, 4> GetRawClearColor(VkFormat format, const VkClearColorValue& clear_value) { |
| 2224 | std::array<uint32_t, 4> raw_color{}; |
| 2225 | std::copy_n(clear_value.uint32, raw_color.size(), raw_color.data()); |
| 2226 | |
| 2227 | // Zero out unused components to avoid polluting the cache with garbage |
| 2228 | if (!FormatHasRed(format)) raw_color[0] = 0; |
| 2229 | if (!FormatHasGreen(format)) raw_color[1] = 0; |
| 2230 | if (!FormatHasBlue(format)) raw_color[2] = 0; |
| 2231 | if (!FormatHasAlpha(format)) raw_color[3] = 0; |
| 2232 | |
| 2233 | return raw_color; |
| 2234 | } |
| 2235 | |
| 2236 | static bool IsClearColorZeroOrOne(VkFormat format, const std::array<uint32_t, 4> clear_color) { |
| 2237 | static_assert(sizeof(float) == sizeof(uint32_t), "Mismatching float <-> uint32 sizes"); |
| 2238 | const float one = 1.0f; |
| 2239 | const float zero = 0.0f; |
| 2240 | uint32_t raw_one{}; |
| 2241 | uint32_t raw_zero{}; |
| 2242 | memcpy(&raw_one, &one, sizeof(one)); |
| 2243 | memcpy(&raw_zero, &zero, sizeof(zero)); |
| 2244 | |
| 2245 | const bool is_one = (!FormatHasRed(format) || (clear_color[0] == raw_one)) && |
| 2246 | (!FormatHasGreen(format) || (clear_color[1] == raw_one)) && |
| 2247 | (!FormatHasBlue(format) || (clear_color[2] == raw_one)) && |
| 2248 | (!FormatHasAlpha(format) || (clear_color[3] == raw_one)); |
| 2249 | const bool is_zero = (!FormatHasRed(format) || (clear_color[0] == raw_zero)) && |
| 2250 | (!FormatHasGreen(format) || (clear_color[1] == raw_zero)) && |
| 2251 | (!FormatHasBlue(format) || (clear_color[2] == raw_zero)) && |
| 2252 | (!FormatHasAlpha(format) || (clear_color[3] == raw_zero)); |
| 2253 | return is_one || is_zero; |
| 2254 | } |
| 2255 | |
| 2256 | static std::string MakeCompressedFormatListNVIDIA() { |
| 2257 | std::string format_list; |
| 2258 | for (VkFormat compressed_format : kCustomClearColorCompressedFormatsNVIDIA) { |
| 2259 | if (compressed_format == kCustomClearColorCompressedFormatsNVIDIA.back()) { |
| 2260 | format_list += "or "; |
| 2261 | } |
| 2262 | format_list += string_VkFormat(compressed_format); |
| 2263 | if (compressed_format != kCustomClearColorCompressedFormatsNVIDIA.back()) { |
| 2264 | format_list += ", "; |
| 2265 | } |
| 2266 | } |
| 2267 | return format_list; |
| 2268 | } |
| 2269 | |
| 2270 | void BestPractices::RecordClearColor(VkFormat format, const VkClearColorValue& clear_value) { |
| 2271 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2272 | |
| 2273 | const std::array<uint32_t, 4> raw_color = GetRawClearColor(format, clear_value); |
| 2274 | if (IsClearColorZeroOrOne(format, raw_color)) { |
| 2275 | // These colors are always compressed |
| 2276 | return; |
| 2277 | } |
| 2278 | |
| 2279 | const auto it = std::find(kCustomClearColorCompressedFormatsNVIDIA.begin(), kCustomClearColorCompressedFormatsNVIDIA.end(), format); |
| 2280 | if (it == kCustomClearColorCompressedFormatsNVIDIA.end()) { |
| 2281 | // The format cannot be compressed with a custom color |
| 2282 | return; |
| 2283 | } |
| 2284 | |
| 2285 | // Record custom clear color |
| 2286 | WriteLockGuard guard{clear_colors_lock_}; |
| 2287 | if (clear_colors_.size() < kMaxRecommendedNumberOfClearColorsNVIDIA) { |
| 2288 | clear_colors_.insert(raw_color); |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | bool BestPractices::ValidateClearColor(VkCommandBuffer commandBuffer, VkFormat format, const VkClearColorValue& clear_value) const { |
| 2293 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 2294 | |
| 2295 | bool skip = false; |
| 2296 | |
| 2297 | const std::array<uint32_t, 4> raw_color = GetRawClearColor(format, clear_value); |
| 2298 | if (IsClearColorZeroOrOne(format, raw_color)) { |
| 2299 | return skip; |
| 2300 | } |
| 2301 | |
| 2302 | const auto it = std::find(kCustomClearColorCompressedFormatsNVIDIA.begin(), kCustomClearColorCompressedFormatsNVIDIA.end(), format); |
| 2303 | if (it == kCustomClearColorCompressedFormatsNVIDIA.end()) { |
| 2304 | // The format is not compressible |
| 2305 | static const std::string format_list = MakeCompressedFormatListNVIDIA(); |
| 2306 | |
| 2307 | skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_ClearColor_NotCompressed, |
| 2308 | "%s Clearing image with format %s without a 1.0f or 0.0f clear color. " |
| 2309 | "The clear will not get compressed in the GPU, harming performance. " |
| 2310 | "This can be fixed using a clear color of VkClearColorValue{0.0f, 0.0f, 0.0f, 0.0f}, or " |
| 2311 | "VkClearColorValue{1.0f, 1.0f, 1.0f, 1.0f}. Alternatively, use %s.", |
| 2312 | VendorSpecificTag(kBPVendorNVIDIA), string_VkFormat(format), format_list.c_str()); |
| 2313 | } else { |
| 2314 | // The format is compressible |
| 2315 | bool registered = false; |
| 2316 | { |
| 2317 | ReadLockGuard guard{clear_colors_lock_}; |
| 2318 | registered = clear_colors_.find(raw_color) != clear_colors_.end(); |
| 2319 | |
| 2320 | if (!registered) { |
| 2321 | // If it's not in the list, it might be new. Check if there's still space for new entries. |
| 2322 | registered = clear_colors_.size() < kMaxRecommendedNumberOfClearColorsNVIDIA; |
| 2323 | } |
| 2324 | } |
| 2325 | if (!registered) { |
| 2326 | std::string clear_color_str; |
| 2327 | |
| 2328 | if (FormatIsUINT(format)) { |
| 2329 | clear_color_str = std::to_string(clear_value.uint32[0]) + ", " + std::to_string(clear_value.uint32[1]) + ", " + |
| 2330 | std::to_string(clear_value.uint32[2]) + ", " + std::to_string(clear_value.uint32[3]); |
| 2331 | } else if (FormatIsSINT(format)) { |
| 2332 | clear_color_str = std::to_string(clear_value.int32[0]) + ", " + std::to_string(clear_value.int32[1]) + ", " + |
| 2333 | std::to_string(clear_value.int32[2]) + ", " + std::to_string(clear_value.int32[3]); |
| 2334 | } else { |
| 2335 | clear_color_str = std::to_string(clear_value.float32[0]) + ", " + std::to_string(clear_value.float32[1]) + ", " + |
| 2336 | std::to_string(clear_value.float32[2]) + ", " + std::to_string(clear_value.float32[3]); |
| 2337 | } |
| 2338 | |
| 2339 | skip |= LogPerformanceWarning( |
| 2340 | commandBuffer, kVUID_BestPractices_ClearColor_NotCompressed, |
| 2341 | "%s Clearing image with unregistered VkClearColorValue{%s}. " |
| 2342 | "This clear will not get compressed in the GPU, harming performance. " |
| 2343 | "The clear color is not registered because too many unique colors have been used. " |
| 2344 | "Select a discrete set of clear colors and stick to those. " |
| 2345 | "VkClearColorValue{0, 0, 0, 0} and VkClearColorValue{1.0f, 1.0f, 1.0f, 1.0f} are always registered.", |
| 2346 | VendorSpecificTag(kBPVendorNVIDIA), clear_color_str.c_str()); |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | return skip; |
| 2351 | } |
| 2352 | |
Hans-Kristian Arntzen | 237663c | 2021-07-01 14:36:40 +0200 | [diff] [blame] | 2353 | static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) { |
| 2354 | for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) { |
| 2355 | const auto& subpass_info = createInfo.pSubpasses[subpass]; |
| 2356 | if (subpass_info.pResolveAttachments) { |
| 2357 | for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) { |
| 2358 | if (subpass_info.pResolveAttachments[i].attachment == attachment) return true; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | return false; |
| 2364 | } |
| 2365 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2366 | static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) { |
| 2367 | for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2368 | const auto& subpass_info = createInfo.pSubpasses[subpass]; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2369 | |
| 2370 | // If an attachment is ever used as a color attachment, |
| 2371 | // resolve attachment or depth stencil attachment, |
| 2372 | // it needs to exist on tile at some point. |
| 2373 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2374 | for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) { |
| 2375 | if (subpass_info.pColorAttachments[i].attachment == attachment) return true; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2378 | if (subpass_info.pResolveAttachments) { |
| 2379 | for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) { |
| 2380 | if (subpass_info.pResolveAttachments[i].attachment == attachment) return true; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | return false; |
| 2388 | } |
| 2389 | |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2390 | static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) { |
| 2391 | if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) { |
| 2392 | return false; |
| 2393 | } |
| 2394 | |
| 2395 | for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2396 | const auto& subpassInfo = createInfo.pSubpasses[subpass]; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2397 | |
| 2398 | for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) { |
| 2399 | if (subpassInfo.pInputAttachments[i].attachment == attachment) { |
| 2400 | return true; |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | return false; |
| 2406 | } |
| 2407 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2408 | bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version, |
| 2409 | const VkRenderPassBeginInfo* pRenderPassBegin) const { |
| 2410 | bool skip = false; |
| 2411 | |
| 2412 | if (!pRenderPassBegin) { |
| 2413 | return skip; |
| 2414 | } |
| 2415 | |
Gareth Webb | dc6549a | 2021-06-16 03:52:24 +0100 | [diff] [blame] | 2416 | if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) { |
| 2417 | skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea, |
| 2418 | "This render pass has a zero-size render area. It cannot write to any attachments, " |
| 2419 | "and can only be used for side effects such as layout transitions."); |
| 2420 | } |
| 2421 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2422 | auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2423 | if (rp_state) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2424 | if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 2425 | const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext); |
Tony-LunarG | 767180f | 2020-04-23 14:03:59 -0600 | [diff] [blame] | 2426 | if (rpabi) { |
| 2427 | skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments); |
| 2428 | } |
| 2429 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2430 | // Check if any attachments have LOAD operation on them |
| 2431 | for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2432 | const auto& attachment = rp_state->createInfo.pAttachments[att]; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2433 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2434 | bool attachment_has_readback = false; |
Hans-Kristian Arntzen | 4afb59b | 2021-06-18 12:41:36 +0200 | [diff] [blame] | 2435 | if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2436 | attachment_has_readback = true; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2440 | attachment_has_readback = true; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2443 | bool attachment_needs_readback = false; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2444 | |
| 2445 | // Check if the attachment is actually used in any subpass on-tile |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2446 | if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) { |
| 2447 | attachment_needs_readback = true; |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement |
LawG4 | 7747b32 | 2022-02-23 16:12:10 +0000 | [diff] [blame] | 2451 | if (attachment_needs_readback && (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) { |
| 2452 | skip |= |
| 2453 | LogPerformanceWarning(device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 2454 | "%s %s: Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n" |
LawG4 | 7747b32 | 2022-02-23 16:12:10 +0000 | [diff] [blame] | 2455 | "Submitting this renderpass will cause the driver to inject a readback of the attachment " |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 2456 | "which will copy in total %u pixels (renderArea = " |
LawG4 | 7747b32 | 2022-02-23 16:12:10 +0000 | [diff] [blame] | 2457 | "{ %" PRId32 ", %" PRId32 ", %" PRIu32 ", %" PRIu32 " }) to the tile buffer.", |
| 2458 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), att, |
| 2459 | pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height, |
| 2460 | pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y, |
| 2461 | pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
paul-lunarg | 7089e27 | 2022-06-20 22:19:37 +0200 | [diff] [blame] | 2464 | |
| 2465 | // Check if renderpass has at least one VK_ATTACHMENT_LOAD_OP_CLEAR |
| 2466 | |
| 2467 | bool clearing = false; |
| 2468 | |
| 2469 | for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) { |
| 2470 | const auto& attachment = rp_state->createInfo.pAttachments[att]; |
| 2471 | |
| 2472 | if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 2473 | clearing = true; |
| 2474 | break; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | // Check if there are ClearValues passed to BeginRenderPass even though no attachments will be cleared |
| 2479 | if (!clearing && pRenderPassBegin->clearValueCount > 0) { |
| 2480 | // Flag as warning because nothing will happen per spec, and pClearValues will be ignored |
| 2481 | skip |= LogWarning( |
| 2482 | device, kVUID_BestPractices_ClearValueWithoutLoadOpClear, |
| 2483 | "This render pass does not have VkRenderPassCreateInfo.pAttachments->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR " |
| 2484 | "but VkRenderPassBeginInfo.clearValueCount > 0. VkRenderPassBeginInfo.pClearValues will be ignored and no " |
paul-lunarg | a0a149c | 2022-06-23 16:18:51 +0200 | [diff] [blame] | 2485 | "attachments will be cleared."); |
paul-lunarg | 7089e27 | 2022-06-20 22:19:37 +0200 | [diff] [blame] | 2486 | } |
paul-lunarg | a0a149c | 2022-06-23 16:18:51 +0200 | [diff] [blame] | 2487 | |
| 2488 | // Check if there are more clearValues than attachments |
| 2489 | if(pRenderPassBegin->clearValueCount > rp_state->createInfo.attachmentCount) { |
| 2490 | // Flag as warning because the overflowing clearValues will be ignored and could even be undefined on certain platforms. |
| 2491 | // This could signal a bug and there seems to be no reason for this to happen on purpose. |
| 2492 | skip |= LogWarning( |
| 2493 | device, kVUID_BestPractices_ClearValueCountHigherThanAttachmentCount, |
| 2494 | "This render pass has VkRenderPassBeginInfo.clearValueCount > VkRenderPassCreateInfo.attachmentCount " |
| 2495 | "(%" PRIu32 " > %" PRIu32 ") and as such the clearValues that do not have a corresponding attachment will be ignored.", |
| 2496 | pRenderPassBegin->clearValueCount, rp_state->createInfo.attachmentCount); |
| 2497 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 2498 | |
| 2499 | if (VendorCheckEnabled(kBPVendorNVIDIA) && rp_state->createInfo.pAttachments) { |
| 2500 | for (uint32_t i = 0; i < pRenderPassBegin->clearValueCount; ++i) { |
| 2501 | const auto& attachment = rp_state->createInfo.pAttachments[i]; |
| 2502 | if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 2503 | const auto& clear_color = pRenderPassBegin->pClearValues[i].color; |
| 2504 | skip |= ValidateClearColor(commandBuffer, attachment.format, clear_color); |
| 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | return skip; |
| 2511 | } |
| 2512 | |
| 2513 | bool BestPractices::ValidateCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const { |
| 2514 | bool skip = false; |
| 2515 | |
| 2516 | auto cmd_state = Get<bp_state::CommandBuffer>(commandBuffer); |
| 2517 | assert(cmd_state); |
| 2518 | |
| 2519 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 2520 | for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; ++i) { |
| 2521 | const auto& color_attachment = pRenderingInfo->pColorAttachments[i]; |
| 2522 | if (color_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 2523 | const VkFormat format = Get<IMAGE_VIEW_STATE>(color_attachment.imageView)->create_info.format; |
| 2524 | skip |= ValidateClearColor(commandBuffer, format, color_attachment.clearValue.color); |
| 2525 | } |
| 2526 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | return skip; |
| 2530 | } |
| 2531 | |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 2532 | void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name, |
| 2533 | IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) { |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2534 | if (view) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2535 | auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state); |
| 2536 | QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2540 | void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state, |
| 2541 | IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) { |
Hans-Kristian Arntzen | 9326420 | 2021-05-21 17:07:46 +0200 | [diff] [blame] | 2542 | // If we're viewing a 3D slice, ignore base array layer. |
| 2543 | // The entire 3D subresource is accessed as one atomic unit. |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2544 | const uint32_t base_array_layer = state->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer; |
Hans-Kristian Arntzen | 9326420 | 2021-05-21 17:07:46 +0200 | [diff] [blame] | 2545 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2546 | const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer; |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2547 | const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2548 | const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel; |
| 2549 | const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2550 | |
| 2551 | for (uint32_t layer = 0; layer < array_layers; layer++) { |
| 2552 | for (uint32_t level = 0; level < mip_levels; level++) { |
Hans-Kristian Arntzen | 9326420 | 2021-05-21 17:07:46 +0200 | [diff] [blame] | 2553 | QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer, |
| 2554 | level + subresource_range.baseMipLevel); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2559 | void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state, |
| 2560 | IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) { |
| 2561 | const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer; |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2562 | const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2563 | |
| 2564 | for (uint32_t layer = 0; layer < array_layers; layer++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 2565 | QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
paul-lunarg | 5eb5206 | 2022-06-27 18:57:15 +0200 | [diff] [blame] | 2569 | void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state, |
| 2570 | IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer, uint32_t mip_level) { |
paul-lunarg | 5e5d740 | 2022-09-07 16:23:10 +0200 | [diff] [blame] | 2571 | funcs.push_back([this, function_name, state, usage, array_layer, mip_level]( |
| 2572 | const ValidationStateTracker& vst, const QUEUE_STATE& qs, const CMD_BUFFER_STATE& cbs) -> bool { |
paul-lunarg | 670a562 | 2022-09-09 13:48:22 +0200 | [diff] [blame] | 2573 | ValidateImageInQueue(qs, cbs, function_name, *state, usage, array_layer, mip_level); |
paul-lunarg | 5eb5206 | 2022-06-27 18:57:15 +0200 | [diff] [blame] | 2574 | return false; |
| 2575 | }); |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 2576 | } |
| 2577 | |
LawG4 | 4d414ba | 2022-02-23 15:35:41 +0000 | [diff] [blame] | 2578 | void BestPractices::ValidateImageInQueueArmImg(const char* function_name, const bp_state::Image& image, |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2579 | IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage, |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2580 | uint32_t array_layer, uint32_t mip_level) { |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2581 | // Swapchain images are implicitly read so clear after store is expected. |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2582 | if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED && |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2583 | !image.IsSwapchainImage()) { |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2584 | LogPerformanceWarning( |
| 2585 | device, kVUID_BestPractices_RenderPass_RedundantStore, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 2586 | "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time " |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2587 | "image was used, it was written to with STORE_OP_STORE. " |
| 2588 | "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based " |
| 2589 | "architectures.", |
LawG4 | 4d414ba | 2022-02-23 15:35:41 +0000 | [diff] [blame] | 2590 | function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level); |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2591 | } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED) { |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2592 | LogPerformanceWarning( |
| 2593 | device, kVUID_BestPractices_RenderPass_RedundantClear, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 2594 | "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time " |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2595 | "image was used, it was written to with vkCmdClear*Image(). " |
| 2596 | "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on " |
LawG4 | 4d414ba | 2022-02-23 15:35:41 +0000 | [diff] [blame] | 2597 | "tile-based architectures.", |
| 2598 | function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level); |
Hans-Kristian Arntzen | 44f9d86 | 2021-03-22 13:56:39 +0100 | [diff] [blame] | 2599 | } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE && |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2600 | (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED || |
| 2601 | last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) { |
Hans-Kristian Arntzen | 44f9d86 | 2021-03-22 13:56:39 +0100 | [diff] [blame] | 2602 | const char *last_cmd = nullptr; |
| 2603 | const char *vuid = nullptr; |
| 2604 | const char *suggestion = nullptr; |
| 2605 | |
| 2606 | switch (last_usage) { |
| 2607 | case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE: |
| 2608 | vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad; |
| 2609 | last_cmd = "vkCmdBlitImage"; |
| 2610 | suggestion = |
| 2611 | "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. " |
| 2612 | "Rather than blitting, just render the source image in a fragment shader in this render pass, " |
| 2613 | "which avoids the memory roundtrip."; |
| 2614 | break; |
| 2615 | case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED: |
| 2616 | vuid = kVUID_BestPractices_RenderPass_InefficientClear; |
| 2617 | last_cmd = "vkCmdClear*Image"; |
| 2618 | suggestion = |
| 2619 | "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on " |
| 2620 | "tile-based architectures. " |
| 2621 | "Use LOAD_OP_CLEAR instead to clear the image for free."; |
| 2622 | break; |
| 2623 | case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE: |
| 2624 | vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad; |
| 2625 | last_cmd = "vkCmdCopy*Image"; |
| 2626 | suggestion = |
| 2627 | "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. " |
| 2628 | "Rather than copying, just render the source image in a fragment shader in this render pass, " |
| 2629 | "which avoids the memory roundtrip."; |
| 2630 | break; |
| 2631 | case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE: |
| 2632 | vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad; |
| 2633 | last_cmd = "vkCmdResolveImage"; |
| 2634 | suggestion = |
| 2635 | "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. " |
| 2636 | "Rather than resolving, and then loading, try to keep rendering in the same render pass, " |
| 2637 | "which avoids the memory roundtrip."; |
| 2638 | break; |
| 2639 | default: |
| 2640 | break; |
| 2641 | } |
| 2642 | |
| 2643 | LogPerformanceWarning( |
| 2644 | device, vuid, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 2645 | "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was loaded to tile as part of LOAD_OP_LOAD, but last " |
Hans-Kristian Arntzen | 44f9d86 | 2021-03-22 13:56:39 +0100 | [diff] [blame] | 2646 | "time image was used, it was written to with %s. %s", |
LawG4 | 4d414ba | 2022-02-23 15:35:41 +0000 | [diff] [blame] | 2647 | function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level, last_cmd, |
| 2648 | suggestion); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2649 | } |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2650 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2651 | |
paul-lunarg | 670a562 | 2022-09-09 13:48:22 +0200 | [diff] [blame] | 2652 | void BestPractices::ValidateImageInQueue(const QUEUE_STATE& qs, const CMD_BUFFER_STATE& cbs, const char* function_name, |
| 2653 | bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer, |
| 2654 | uint32_t mip_level) { |
| 2655 | auto last_queue_family = state.GetLastQueueFamily(array_layer, mip_level); |
| 2656 | auto queue_family = qs.queueFamilyIndex; |
| 2657 | auto last_usage = state.UpdateUsage(array_layer, mip_level, usage, queue_family); |
| 2658 | |
| 2659 | // Concurrent sharing usage of image with exclusive sharing mode |
| 2660 | if (state.createInfo.sharingMode == VK_SHARING_MODE_EXCLUSIVE && last_queue_family != queue_family) { |
| 2661 | // if UNDEFINED then first use/acquisition of subresource |
| 2662 | if (last_usage != IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED) { |
| 2663 | // If usage might read from the subresource, as contents are undefined |
| 2664 | // so write only is fine |
| 2665 | if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE || usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ || |
paul-lunarg | 8696c6b | 2022-09-09 16:55:10 +0200 | [diff] [blame] | 2666 | usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ || usage == IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS || |
| 2667 | usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ) { |
paul-lunarg | 670a562 | 2022-09-09 13:48:22 +0200 | [diff] [blame] | 2668 | LogWarning( |
| 2669 | state.Handle().Cast<VkImage>(), kVUID_BestPractices_ConcurrentUsageOfExclusiveImage, |
| 2670 | "%s : Subresource (arrayLayer: %u, mipLevel: %u) of image is used on queue family index %u after being used on " |
| 2671 | "queue family index %u, " |
| 2672 | "but has VK_SHARING_MODE_EXCLUSIVE, and has not been acquired and released with a ownership transfer operation", |
| 2673 | function_name, array_layer, mip_level, queue_family, last_queue_family); |
| 2674 | } |
| 2675 | } |
| 2676 | } |
paul-lunarg | 5eb5206 | 2022-06-27 18:57:15 +0200 | [diff] [blame] | 2677 | |
| 2678 | // When image was discarded with StoreOpDontCare but is now being read with LoadOpLoad |
| 2679 | if (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED && |
| 2680 | usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE) { |
| 2681 | LogWarning(device, kVUID_BestPractices_StoreOpDontCareThenLoadOpLoad, |
| 2682 | "Trying to load an attachment with LOAD_OP_LOAD that was previously stored with STORE_OP_DONT_CARE. This may " |
| 2683 | "result in undefined behaviour."); |
| 2684 | } |
| 2685 | |
LawG4 | 4d414ba | 2022-02-23 15:35:41 +0000 | [diff] [blame] | 2686 | if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) { |
| 2687 | ValidateImageInQueueArmImg(function_name, state, last_usage, usage, array_layer, mip_level); |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 2688 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2691 | void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) { |
| 2692 | cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(), |
| 2693 | cb.queue_submit_functions_after_render_pass.end()); |
| 2694 | cb.queue_submit_functions_after_render_pass.clear(); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2698 | RecordCmdEndRenderingCommon(commandBuffer); |
| 2699 | |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2700 | ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2701 | auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 2702 | if (cb_node) { |
| 2703 | AddDeferredQueueOperations(*cb_node); |
| 2704 | } |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2708 | RecordCmdEndRenderingCommon(commandBuffer); |
| 2709 | |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2710 | ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2711 | auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 2712 | if (cb_node) { |
| 2713 | AddDeferredQueueOperations(*cb_node); |
| 2714 | } |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2718 | RecordCmdEndRenderingCommon(commandBuffer); |
| 2719 | |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2720 | ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2721 | auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 2722 | if (cb_node) { |
| 2723 | AddDeferredQueueOperations(*cb_node); |
| 2724 | } |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2725 | } |
| 2726 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2727 | void BestPractices::PreCallRecordCmdEndRendering(VkCommandBuffer commandBuffer) { |
| 2728 | RecordCmdEndRenderingCommon(commandBuffer); |
| 2729 | |
| 2730 | ValidationStateTracker::PreCallRecordCmdEndRendering(commandBuffer); |
| 2731 | } |
| 2732 | |
| 2733 | void BestPractices::PreCallRecordCmdEndRenderingKHR(VkCommandBuffer commandBuffer) { |
| 2734 | RecordCmdEndRenderingCommon(commandBuffer); |
| 2735 | |
| 2736 | ValidationStateTracker::PreCallRecordCmdEndRenderingKHR(commandBuffer); |
| 2737 | } |
| 2738 | |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 2739 | void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, |
| 2740 | const VkRenderPassBeginInfo* pRenderPassBegin, |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2741 | VkSubpassContents contents) { |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2742 | ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2743 | RecordCmdBeginRenderingCommon(commandBuffer); |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 2744 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin); |
| 2745 | } |
| 2746 | |
| 2747 | void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, |
| 2748 | const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2749 | const VkSubpassBeginInfo* pSubpassBeginInfo) { |
| 2750 | ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2751 | RecordCmdBeginRenderingCommon(commandBuffer); |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 2752 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin); |
| 2753 | } |
| 2754 | |
| 2755 | void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2756 | const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2757 | const VkSubpassBeginInfo* pSubpassBeginInfo) { |
| 2758 | ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2759 | RecordCmdBeginRenderingCommon(commandBuffer); |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 2760 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin); |
| 2761 | } |
| 2762 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 2763 | void BestPractices::PreCallRecordCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) { |
| 2764 | ValidationStateTracker::PreCallRecordCmdBeginRendering(commandBuffer, pRenderingInfo); |
| 2765 | RecordCmdBeginRenderingCommon(commandBuffer); |
| 2766 | } |
| 2767 | |
| 2768 | void BestPractices::PreCallRecordCmdBeginRenderingKHR(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) { |
| 2769 | ValidationStateTracker::PreCallRecordCmdBeginRenderingKHR(commandBuffer, pRenderingInfo); |
| 2770 | RecordCmdBeginRenderingCommon(commandBuffer); |
| 2771 | } |
| 2772 | |
| 2773 | void BestPractices::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) { |
| 2774 | ValidationStateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents); |
| 2775 | |
| 2776 | auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 2777 | auto rp = cmd_state->activeRenderPass.get(); |
| 2778 | assert(rp); |
| 2779 | |
| 2780 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 2781 | IMAGE_VIEW_STATE* depth_image_view = nullptr; |
| 2782 | |
| 2783 | const auto depth_attachment = rp->createInfo.pSubpasses[cmd_state->activeSubpass].pDepthStencilAttachment; |
| 2784 | if (depth_attachment) { |
| 2785 | const uint32_t attachment_index = depth_attachment->attachment; |
| 2786 | if (attachment_index != VK_ATTACHMENT_UNUSED) { |
| 2787 | depth_image_view = (*cmd_state->active_attachments)[attachment_index]; |
| 2788 | } |
| 2789 | } |
| 2790 | if (depth_image_view && (depth_image_view->create_info.subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U) { |
| 2791 | const VkImage depth_image = depth_image_view->image_state->image(); |
| 2792 | const VkImageSubresourceRange& subresource_range = depth_image_view->create_info.subresourceRange; |
| 2793 | RecordBindZcullScope(*cmd_state, depth_image, subresource_range); |
| 2794 | } else { |
| 2795 | RecordUnbindZcullScope(*cmd_state); |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 2800 | void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) { |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2801 | |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2802 | if (!pRenderPassBegin) { |
| 2803 | return; |
| 2804 | } |
| 2805 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2806 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 2807 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2808 | auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2809 | if (rp_state) { |
| 2810 | // Check load ops |
| 2811 | for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2812 | const auto& attachment = rp_state->createInfo.pAttachments[att]; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2813 | |
| 2814 | if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) && |
| 2815 | !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) { |
| 2816 | continue; |
| 2817 | } |
| 2818 | |
paul-lunarg | ce0a206 | 2022-09-09 15:22:51 +0200 | [diff] [blame] | 2819 | // If renderpass doesn't load attachment, no need to validate image in queue |
| 2820 | if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_NONE_EXT) || |
| 2821 | (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_NONE_EXT)) { |
| 2822 | continue; |
| 2823 | } |
| 2824 | |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2825 | IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2826 | |
| 2827 | if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) || |
| 2828 | (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) { |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2829 | usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE; |
Hans-Kristian Arntzen | 5e56e55 | 2021-03-29 11:45:20 +0200 | [diff] [blame] | 2830 | } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) || |
| 2831 | (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) { |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2832 | usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED; |
Hans-Kristian Arntzen | 5e56e55 | 2021-03-29 11:45:20 +0200 | [diff] [blame] | 2833 | } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) { |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 2834 | usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2835 | } |
| 2836 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2837 | auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer); |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 2838 | std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr; |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2839 | |
Tony-LunarG | b3ab357 | 2021-07-02 09:45:17 -0600 | [diff] [blame] | 2840 | if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) { |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2841 | const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext); |
| 2842 | if (rpabi) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2843 | image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]); |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2844 | } |
| 2845 | } else { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2846 | image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]); |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2847 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2848 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 2849 | QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | // Check store ops |
| 2853 | for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 2854 | const auto& attachment = rp_state->createInfo.pAttachments[att]; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2855 | |
| 2856 | if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) { |
| 2857 | continue; |
| 2858 | } |
| 2859 | |
paul-lunarg | ce0a206 | 2022-09-09 15:22:51 +0200 | [diff] [blame] | 2860 | // If renderpass doesn't store attachment, no need to validate image in queue |
| 2861 | if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_NONE) || |
| 2862 | (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_NONE)) { |
| 2863 | continue; |
| 2864 | } |
| 2865 | |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2866 | IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2867 | |
| 2868 | if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) || |
| 2869 | (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) { |
Hans-Kristian Arntzen | 5b466db | 2021-03-18 13:59:46 +0100 | [diff] [blame] | 2870 | usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED; |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2873 | auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer); |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2874 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 2875 | std::shared_ptr<IMAGE_VIEW_STATE> image_view; |
Tony-LunarG | b3ab357 | 2021-07-02 09:45:17 -0600 | [diff] [blame] | 2876 | if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) { |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2877 | const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext); |
| 2878 | if (rpabi) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2879 | image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]); |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2880 | } |
| 2881 | } else { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 2882 | image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]); |
Hans-Kristian Arntzen | 9710e14 | 2021-03-18 12:19:02 +0100 | [diff] [blame] | 2883 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2884 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 2885 | QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2890 | bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2891 | VkSubpassContents contents) const { |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2892 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 2893 | skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2894 | return skip; |
| 2895 | } |
| 2896 | |
| 2897 | bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2898 | const VkRenderPassBeginInfo* pRenderPassBegin, |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2899 | const VkSubpassBeginInfo* pSubpassBeginInfo) const { |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2900 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2901 | skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2902 | return skip; |
| 2903 | } |
| 2904 | |
| 2905 | bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2906 | const VkSubpassBeginInfo* pSubpassBeginInfo) const { |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2907 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2908 | skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 2909 | return skip; |
| 2910 | } |
| 2911 | |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 2912 | bool BestPractices::PreCallValidateCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const { |
| 2913 | bool skip = StateTracker::PreCallValidateCmdBeginRendering(commandBuffer, pRenderingInfo); |
| 2914 | skip |= ValidateCmdBeginRendering(commandBuffer, pRenderingInfo); |
| 2915 | return skip; |
| 2916 | } |
| 2917 | |
| 2918 | bool BestPractices::PreCallValidateCmdBeginRenderingKHR(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const { |
| 2919 | bool skip = StateTracker::PreCallValidateCmdBeginRenderingKHR(commandBuffer, pRenderingInfo); |
| 2920 | skip |= ValidateCmdBeginRendering(commandBuffer, pRenderingInfo); |
| 2921 | return skip; |
| 2922 | } |
| 2923 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2924 | void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version, |
| 2925 | const VkRenderPassBeginInfo* pRenderPassBegin) { |
Hans-Kristian Arntzen | a900f5d | 2021-06-14 15:09:31 +0200 | [diff] [blame] | 2926 | // Reset the renderpass state |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2927 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
sjfricke | 52defd4 | 2022-08-08 16:37:46 +0900 | [diff] [blame] | 2928 | // TODO - move this logic to the Render Pass state as cb->has_draw_cmd should stay true for lifetime of command buffer |
| 2929 | cb->has_draw_cmd = false; |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 2930 | assert(cb); |
| 2931 | auto& render_pass_state = cb->render_pass_state; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 2932 | render_pass_state.touchesAttachments.clear(); |
| 2933 | render_pass_state.earlyClearAttachments.clear(); |
Hans-Kristian Arntzen | a900f5d | 2021-06-14 15:09:31 +0200 | [diff] [blame] | 2934 | render_pass_state.numDrawCallsDepthOnly = 0; |
| 2935 | render_pass_state.numDrawCallsDepthEqualCompare = 0; |
| 2936 | render_pass_state.colorAttachment = false; |
| 2937 | render_pass_state.depthAttachment = false; |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 2938 | render_pass_state.drawTouchAttachments = true; |
Hans-Kristian Arntzen | a900f5d | 2021-06-14 15:09:31 +0200 | [diff] [blame] | 2939 | // Don't reset state related to pipeline state. |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2940 | |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 2941 | // Reset NV state |
| 2942 | cb->nv = {}; |
| 2943 | |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 2944 | auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2945 | |
| 2946 | // track depth / color attachment usage within the renderpass |
| 2947 | for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) { |
| 2948 | // record if depth/color attachments are in use for this renderpass |
Hans-Kristian Arntzen | a900f5d | 2021-06-14 15:09:31 +0200 | [diff] [blame] | 2949 | if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2950 | |
Hans-Kristian Arntzen | a900f5d | 2021-06-14 15:09:31 +0200 | [diff] [blame] | 2951 | if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2956 | VkSubpassContents contents) { |
| 2957 | StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 2958 | RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin); |
| 2959 | } |
| 2960 | |
| 2961 | void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2962 | const VkSubpassBeginInfo* pSubpassBeginInfo) { |
| 2963 | StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2964 | RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
| 2965 | } |
| 2966 | |
| 2967 | void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2968 | const VkRenderPassBeginInfo* pRenderPassBegin, |
| 2969 | const VkSubpassBeginInfo* pSubpassBeginInfo) { |
| 2970 | StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2971 | RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin); |
| 2972 | } |
| 2973 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2974 | // Generic function to handle validation for all CmdDraw* type functions |
| 2975 | bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const { |
| 2976 | bool skip = false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 2977 | const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2978 | if (cb_state) { |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 2979 | const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2980 | const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state; |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2981 | const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings; |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 2982 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2983 | // Verify vertex binding |
Tony-LunarG | 2ffe1f5 | 2022-04-11 15:13:30 -0600 | [diff] [blame] | 2984 | if (pipeline_state && pipeline_state->vertex_input_state && |
| 2985 | pipeline_state->vertex_input_state->binding_descriptions.size() <= 0) { |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2986 | if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2987 | skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds, |
Mark Lobodzinski | f95a266 | 2020-01-29 15:43:32 -0700 | [diff] [blame] | 2988 | "Vertex buffers are bound to %s but no vertex buffers are attached to %s.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2989 | report_data->FormatHandle(cb_state->commandBuffer()).c_str(), |
| 2990 | report_data->FormatHandle(pipeline_state->pipeline()).c_str()); |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
Nathaniel Cesario | f7b732a | 2021-06-03 14:08:27 -0600 | [diff] [blame] | 2993 | |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 2994 | const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS); |
Nathaniel Cesario | f7b732a | 2021-06-03 14:08:27 -0600 | [diff] [blame] | 2995 | if (pipe) { |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 2996 | const auto& rp_state = pipe->RenderPassState(); |
Nathaniel Cesario | f7b732a | 2021-06-03 14:08:27 -0600 | [diff] [blame] | 2997 | if (rp_state) { |
| 2998 | for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) { |
| 2999 | const auto& subpass = rp_state->createInfo.pSubpasses[i]; |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 3000 | const auto* ds_state = pipe->DepthStencilState(); |
Jeremy Gebben | 11af979 | 2021-08-20 10:20:09 -0600 | [diff] [blame] | 3001 | const uint32_t depth_stencil_attachment = |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 3002 | GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment); |
| 3003 | const auto* raster_state = pipe->RasterizationState(); |
| 3004 | if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && raster_state && |
| 3005 | raster_state->depthBiasEnable == VK_TRUE) { |
Nathaniel Cesario | f7b732a | 2021-06-03 14:08:27 -0600 | [diff] [blame] | 3006 | skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment, |
| 3007 | "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller); |
| 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | } |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 3012 | } |
| 3013 | return skip; |
| 3014 | } |
| 3015 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3016 | void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3017 | auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer); |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3018 | assert(cb_node); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3019 | if (VendorCheckEnabled(kBPVendorArm)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3020 | RecordCmdDrawTypeArm(*cb_node, draw_count, caller); |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 3021 | } |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3022 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 3023 | RecordCmdDrawTypeNVIDIA(*cb_node); |
| 3024 | } |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 3025 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3026 | if (cb_node->render_pass_state.drawTouchAttachments) { |
| 3027 | for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) { |
| 3028 | RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects); |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 3029 | } |
| 3030 | // No need to touch the same attachments over and over. |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3031 | cb_node->render_pass_state.drawTouchAttachments = false; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3035 | void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) { |
| 3036 | auto& render_pass_state = cb_node.render_pass_state; |
LawG4 | b21485c | 2022-02-28 13:46:48 +0000 | [diff] [blame] | 3037 | // Each TBDR vendor requires a depth pre-pass draw call to have a minimum number of vertices/indices before it counts towards |
| 3038 | // depth prepass warnings First find the lowest enabled draw count |
| 3039 | uint32_t lowestEnabledMinDrawCount = 0; |
| 3040 | lowestEnabledMinDrawCount = VendorCheckEnabled(kBPVendorArm) * kDepthPrePassMinDrawCountArm; |
| 3041 | if (VendorCheckEnabled(kBPVendorIMG) && kDepthPrePassMinDrawCountIMG < lowestEnabledMinDrawCount) |
| 3042 | lowestEnabledMinDrawCount = kDepthPrePassMinDrawCountIMG; |
| 3043 | |
| 3044 | if (draw_count >= lowestEnabledMinDrawCount) { |
Hans-Kristian Arntzen | 8abca1e | 2021-06-16 13:57:45 +0200 | [diff] [blame] | 3045 | if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++; |
| 3046 | if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3050 | void BestPractices::RecordCmdDrawTypeNVIDIA(bp_state::CommandBuffer& cmd_state) { |
| 3051 | assert(VendorCheckEnabled(kBPVendorNVIDIA)); |
| 3052 | |
| 3053 | if (cmd_state.nv.depth_test_enable && cmd_state.nv.zcull_direction != bp_state::CommandBufferStateNV::ZcullDirection::Unknown) { |
| 3054 | RecordSetScopeZcullDirection(cmd_state, cmd_state.nv.zcull_direction); |
| 3055 | RecordZcullDraw(cmd_state); |
| 3056 | } |
| 3057 | } |
| 3058 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3059 | bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3060 | uint32_t firstVertex, uint32_t firstInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3061 | bool skip = false; |
| 3062 | |
| 3063 | if (instanceCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 3064 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero, |
| 3065 | "Warning: You are calling vkCmdDraw() with an instanceCount of Zero."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3066 | } |
Nathaniel Cesario | f7b732a | 2021-06-03 14:08:27 -0600 | [diff] [blame] | 3067 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()"); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3068 | |
| 3069 | return skip; |
| 3070 | } |
| 3071 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3072 | void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 3073 | uint32_t firstVertex, uint32_t firstInstance) { |
| 3074 | StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance); |
| 3075 | RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()"); |
| 3076 | } |
| 3077 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3078 | bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3079 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3080 | bool skip = false; |
| 3081 | |
| 3082 | if (instanceCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 3083 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero, |
| 3084 | "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3085 | } |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 3086 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()"); |
| 3087 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3088 | // Check if we reached the limit for small indexed draw calls. |
| 3089 | // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed. |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3090 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3091 | if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices && |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 3092 | (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) && |
LawG4 | ff42d72 | 2022-03-01 10:28:25 +0000 | [diff] [blame] | 3093 | (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) { |
Hans-Kristian Arntzen | b214795 | 2021-04-28 14:32:00 +0200 | [diff] [blame] | 3094 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls, |
LawG4 | ff42d72 | 2022-03-01 10:28:25 +0000 | [diff] [blame] | 3095 | "%s %s: The command buffer contains many small indexed drawcalls " |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3096 | "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. " |
| 3097 | "You can try batching drawcalls or instancing when applicable.", |
LawG4 | ff42d72 | 2022-03-01 10:28:25 +0000 | [diff] [blame] | 3098 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), kMaxSmallIndexedDrawcalls, |
| 3099 | kSmallIndexedDrawcallIndices); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3102 | if (VendorCheckEnabled(kBPVendorArm)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3103 | ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | return skip; |
| 3107 | } |
| 3108 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3109 | bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount, |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3110 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const { |
| 3111 | bool skip = false; |
| 3112 | |
| 3113 | // check for sparse/underutilised index buffer, and post-transform cache thrashing |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3114 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3115 | const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get(); |
| 3116 | if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip; |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3117 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3118 | const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type; |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 3119 | const auto& ib_mem_state = *ib_state->MemState(); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3120 | const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset; |
| 3121 | const void* ib_mem = ib_mem_state.p_driver_data; |
| 3122 | bool primitive_restart_enable = false; |
| 3123 | |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 3124 | const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3125 | const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point]; |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 3126 | const auto* pipeline_state = pipeline_binding_iter.pipeline_state; |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3127 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 3128 | const auto* ia_state = pipeline_state ? pipeline_state->InputAssemblyState() : nullptr; |
| 3129 | if (ia_state) { |
| 3130 | primitive_restart_enable = ia_state->primitiveRestartEnable == VK_TRUE; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3131 | } |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3132 | |
| 3133 | // no point checking index buffer if the memory is nonexistant/unmapped, or if there is no graphics pipeline bound to this CB |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 3134 | if (ib_mem && pipeline_binding_iter.IsUsing()) { |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3135 | uint32_t scan_stride; |
| 3136 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 3137 | scan_stride = sizeof(uint8_t); |
| 3138 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 3139 | scan_stride = sizeof(uint16_t); |
| 3140 | } else { |
| 3141 | scan_stride = sizeof(uint32_t); |
| 3142 | } |
| 3143 | |
| 3144 | const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride; |
| 3145 | const uint8_t* scan_end = scan_begin + indexCount * scan_stride; |
| 3146 | |
| 3147 | // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all |
| 3148 | // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded, |
| 3149 | // irrespective of whether or not they're part of the draw call. |
| 3150 | |
| 3151 | // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer |
| 3152 | uint32_t min_index = ~0u; |
| 3153 | // start with maximum as 0 and adjust to indices in the buffer |
| 3154 | uint32_t max_index = 0u; |
| 3155 | |
| 3156 | // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded |
| 3157 | // for the given index buffer |
| 3158 | uint32_t vertex_shade_count = 0; |
| 3159 | |
| 3160 | PostTransformLRUCacheModel post_transform_cache; |
| 3161 | |
| 3162 | // The size of the cache being modelled positively correlates with how much behaviour it can capture about |
| 3163 | // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the |
| 3164 | // target architecture. |
| 3165 | // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice. |
| 3166 | // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html |
| 3167 | post_transform_cache.resize(32); |
| 3168 | |
| 3169 | for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) { |
| 3170 | uint32_t scan_index; |
| 3171 | uint32_t primitive_restart_value; |
| 3172 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 3173 | scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr); |
| 3174 | primitive_restart_value = 0xFF; |
| 3175 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 3176 | scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr); |
| 3177 | primitive_restart_value = 0xFFFF; |
| 3178 | } else { |
| 3179 | scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr); |
| 3180 | primitive_restart_value = 0xFFFFFFFF; |
| 3181 | } |
| 3182 | |
| 3183 | max_index = std::max(max_index, scan_index); |
| 3184 | min_index = std::min(min_index, scan_index); |
| 3185 | |
| 3186 | if (!primitive_restart_enable || scan_index != primitive_restart_value) { |
| 3187 | bool in_cache = post_transform_cache.query_cache(scan_index); |
| 3188 | // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again |
| 3189 | if (!in_cache) vertex_shade_count++; |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | // if the max and min values were not set, then we either have no indices, or all primitive restarts, exit... |
Sam Walls | 61b0689 | 2020-07-23 16:20:50 +0100 | [diff] [blame] | 3194 | // if the max and min are the same, then it implies all the indices are the same, then we don't need to do anything |
| 3195 | if (max_index < min_index || max_index == min_index) return skip; |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3196 | |
| 3197 | if (max_index - min_index >= indexCount) { |
Mark Young | 0ec6b06 | 2020-11-19 15:32:17 -0700 | [diff] [blame] | 3198 | skip |= |
| 3199 | LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer, |
| 3200 | "%s The indices which were specified for the draw call only utilise approximately %.02f%% of " |
| 3201 | "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven " |
| 3202 | "Vertex Shading), meaning all vertices corresponding to indices between the minimum and " |
| 3203 | "maximum would be loaded, and possibly shaded, whether or not they are used.", |
| 3204 | VendorSpecificTag(kBPVendorArm), |
| 3205 | (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3206 | return skip; |
| 3207 | } |
| 3208 | |
| 3209 | // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call |
| 3210 | // each bit of the n-th bucket contains the inclusion information for indices (n*n_buckets) to ((n+1)*n_buckets) |
Sam Walls | 61b0689 | 2020-07-23 16:20:50 +0100 | [diff] [blame] | 3211 | const size_t refs_per_bucket = 64; |
| 3212 | std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets; |
| 3213 | |
| 3214 | const uint32_t n_indices = max_index - min_index + 1; |
| 3215 | const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) + |
| 3216 | ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0); |
| 3217 | |
| 3218 | // there needs to be at least one bitset to store a set of indices smaller than n_buckets |
| 3219 | vertex_reference_buckets.resize(std::max(1u, n_buckets)); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3220 | |
| 3221 | // To avoid using too much memory, we run over the indices again. |
| 3222 | // Knowing the size from the last scan allows us to record index usage with bitsets |
| 3223 | for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) { |
| 3224 | uint32_t scan_index; |
| 3225 | if (ib_type == VK_INDEX_TYPE_UINT8_EXT) { |
| 3226 | scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr); |
| 3227 | } else if (ib_type == VK_INDEX_TYPE_UINT16) { |
| 3228 | scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr); |
| 3229 | } else { |
| 3230 | scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr); |
| 3231 | } |
| 3232 | // keep track of the set of all indices used to reference vertices in the draw call |
| 3233 | size_t index_offset = scan_index - min_index; |
Sam Walls | 61b0689 | 2020-07-23 16:20:50 +0100 | [diff] [blame] | 3234 | size_t bitset_bucket_index = index_offset / refs_per_bucket; |
| 3235 | uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3236 | vertex_reference_buckets[bitset_bucket_index] |= used_indices; |
| 3237 | } |
| 3238 | |
| 3239 | uint32_t vertex_reference_count = 0; |
| 3240 | for (const auto& bitset : vertex_reference_buckets) { |
| 3241 | vertex_reference_count += static_cast<uint32_t>(bitset.count()); |
| 3242 | } |
| 3243 | |
| 3244 | // low index buffer utilization implies that: of the vertices available to the draw call, not all are utilized |
Mark Young | 0ec6b06 | 2020-11-19 15:32:17 -0700 | [diff] [blame] | 3245 | float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3246 | // low hit rate (high miss rate) implies the order of indices in the draw call may be possible to improve |
Mark Young | 0ec6b06 | 2020-11-19 15:32:17 -0700 | [diff] [blame] | 3247 | float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count); |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 3248 | |
| 3249 | if (utilization < 0.5f) { |
| 3250 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer, |
| 3251 | "%s The indices which were specified for the draw call only utilise approximately " |
| 3252 | "%.02f%% of the bound vertex buffer.", |
| 3253 | VendorSpecificTag(kBPVendorArm), utilization); |
| 3254 | } |
| 3255 | |
| 3256 | if (cache_hit_rate <= 0.5f) { |
| 3257 | skip |= |
| 3258 | LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing, |
| 3259 | "%s The indices which were specified for the draw call are estimated to cause thrashing of " |
| 3260 | "the post-transform vertex cache, with a hit-rate of %.02f%%. " |
| 3261 | "I.e. the ordering of the index buffer may not make optimal use of indices associated with " |
| 3262 | "recently shaded vertices.", |
| 3263 | VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f); |
| 3264 | } |
| 3265 | } |
| 3266 | |
Mark Lobodzinski | 4c4cf94 | 2019-12-20 11:09:51 -0700 | [diff] [blame] | 3267 | return skip; |
| 3268 | } |
| 3269 | |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3270 | bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount, |
| 3271 | const VkCommandBuffer* pCommandBuffers) const { |
| 3272 | bool skip = false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3273 | const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3274 | for (uint32_t i = 0; i < commandBufferCount; i++) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3275 | const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]); |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3276 | if (secondary_cb == nullptr) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3277 | continue; |
| 3278 | } |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3279 | const auto& secondary = secondary_cb->render_pass_state; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3280 | for (auto& clear : secondary.earlyClearAttachments) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3281 | if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) { |
| 3282 | skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3283 | } |
| 3284 | } |
| 3285 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 3286 | |
| 3287 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 3288 | if (commandBufferCount > 0) { |
| 3289 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers, |
| 3290 | "%s Performance warning: Use of secondary command buffers is not recommended. ", |
| 3291 | VendorSpecificTag(kBPVendorAMD)); |
| 3292 | } |
| 3293 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3294 | return skip; |
| 3295 | } |
| 3296 | |
| 3297 | void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount, |
| 3298 | const VkCommandBuffer* pCommandBuffers) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3299 | ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers); |
| 3300 | |
| 3301 | auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 3302 | if (!primary) { |
| 3303 | return; |
| 3304 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3305 | |
| 3306 | for (uint32_t i = 0; i < commandBufferCount; i++) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3307 | auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]); |
| 3308 | if (!secondary) { |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3309 | continue; |
| 3310 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3311 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3312 | for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) { |
| 3313 | if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) { |
| 3314 | RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment, |
| 3315 | early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data()); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3316 | } else { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3317 | RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3321 | for (auto& touch : secondary->render_pass_state.touchesAttachments) { |
| 3322 | RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3323 | } |
Hans-Kristian Arntzen | c7eb82a | 2021-06-16 13:57:18 +0200 | [diff] [blame] | 3324 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3325 | primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare; |
| 3326 | primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3327 | } |
| 3328 | |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3329 | } |
| 3330 | |
Rodrigo Locatti | 7d716e1 | 2022-03-09 19:15:17 -0300 | [diff] [blame] | 3331 | bool BestPractices::PreCallValidateCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, |
| 3332 | const VkAccelerationStructureInfoNV* pInfo, |
| 3333 | VkBuffer instanceData, VkDeviceSize instanceOffset, |
| 3334 | VkBool32 update, VkAccelerationStructureNV dst, |
| 3335 | VkAccelerationStructureNV src, VkBuffer scratch, |
| 3336 | VkDeviceSize scratchOffset) const { |
| 3337 | return ValidateBuildAccelerationStructure(commandBuffer); |
| 3338 | } |
| 3339 | |
| 3340 | bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresIndirectKHR( |
| 3341 | VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos, |
| 3342 | const VkDeviceAddress* pIndirectDeviceAddresses, const uint32_t* pIndirectStrides, |
| 3343 | const uint32_t* const* ppMaxPrimitiveCounts) const { |
| 3344 | return ValidateBuildAccelerationStructure(commandBuffer); |
| 3345 | } |
| 3346 | |
| 3347 | bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresKHR( |
| 3348 | VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos, |
| 3349 | const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) const { |
| 3350 | return ValidateBuildAccelerationStructure(commandBuffer); |
| 3351 | } |
| 3352 | |
| 3353 | bool BestPractices::ValidateBuildAccelerationStructure(VkCommandBuffer commandBuffer) const { |
| 3354 | bool skip = false; |
| 3355 | auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3356 | assert(cb_node); |
| 3357 | |
| 3358 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 3359 | if ((cb_node->GetQueueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 3360 | skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_AccelerationStructure_NotAsync, |
| 3361 | "%s Performance warning: Prefer building acceleration structures on an asynchronous " |
| 3362 | "compute queue, instead of using the universal graphics queue.", |
| 3363 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | return skip; |
| 3368 | } |
| 3369 | |
Rodrigo Locatti | 66b2335 | 2022-03-15 17:28:32 -0300 | [diff] [blame] | 3370 | bool BestPractices::ValidateBindMemory(VkDevice device, VkDeviceMemory memory) const { |
| 3371 | bool skip = false; |
| 3372 | |
| 3373 | if (VendorCheckEnabled(kBPVendorNVIDIA) && device_extensions.vk_ext_pageable_device_local_memory) { |
| 3374 | auto mem_info = std::static_pointer_cast<const bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory)); |
| 3375 | if (!mem_info->dynamic_priority) { |
| 3376 | skip |= |
| 3377 | LogPerformanceWarning(device, kVUID_BestPractices_BindMemory_NoPriority, |
| 3378 | "%s Use vkSetDeviceMemoryPriorityEXT to provide the OS with information on which allocations " |
| 3379 | "should stay in memory and which should be demoted first when video memory is limited. The " |
| 3380 | "highest priority should be given to GPU-written resources like color attachments, depth " |
| 3381 | "attachments, storage images, and buffers written from the GPU.", |
| 3382 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | return skip; |
| 3387 | } |
| 3388 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3389 | void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) { |
| 3390 | auto& state = cb_state.render_pass_state; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3391 | // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment. |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3392 | auto itr = |
| 3393 | std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(), |
| 3394 | [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; }); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3395 | |
| 3396 | if (itr != state.touchesAttachments.end()) { |
| 3397 | itr->aspects |= aspects; |
| 3398 | } else { |
| 3399 | state.touchesAttachments.push_back({ fb_attachment, aspects }); |
| 3400 | } |
| 3401 | } |
| 3402 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3403 | void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment, |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3404 | uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount, |
| 3405 | const VkClearRect* pRects) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3406 | auto& state = cmd_state.render_pass_state; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3407 | // If we observe a full clear before any other access to a frame buffer attachment, |
| 3408 | // we have candidate for redundant clear attachments. |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3409 | auto itr = |
| 3410 | std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(), |
| 3411 | [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; }); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3412 | |
| 3413 | uint32_t new_aspects = aspects; |
| 3414 | if (itr != state.touchesAttachments.end()) { |
| 3415 | new_aspects = aspects & ~itr->aspects; |
| 3416 | itr->aspects |= aspects; |
| 3417 | } else { |
| 3418 | state.touchesAttachments.push_back({ fb_attachment, aspects }); |
| 3419 | } |
| 3420 | |
| 3421 | if (new_aspects == 0) { |
| 3422 | return; |
| 3423 | } |
| 3424 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3425 | if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3426 | // The first command might be a clear, but might not be the first in the render pass, defer any checks until |
| 3427 | // CmdExecuteCommands. |
| 3428 | state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects, |
| 3429 | std::vector<VkClearRect>{pRects, pRects + rectCount} }); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer, |
| 3434 | uint32_t attachmentCount, const VkClearAttachment* pClearAttachments, |
| 3435 | uint32_t rectCount, const VkClearRect* pRects) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3436 | ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects); |
| 3437 | |
| 3438 | auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 3439 | auto* rp_state = cmd_state->activeRenderPass.get(); |
| 3440 | auto* fb_state = cmd_state->activeFramebuffer.get(); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3441 | bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| 3442 | |
| 3443 | if (rectCount == 0 || !rp_state) { |
| 3444 | return; |
| 3445 | } |
| 3446 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3447 | if (!is_secondary && !fb_state && !rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3448 | return; |
| 3449 | } |
| 3450 | |
| 3451 | // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command. |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3452 | const bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3453 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3454 | if (rp_state->UsesDynamicRendering()) { |
| 3455 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 3456 | auto pColorAttachments = rp_state->dynamic_rendering_begin_rendering_info.pColorAttachments; |
| 3457 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3458 | for (uint32_t i = 0; i < attachmentCount; i++) { |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 3459 | auto& clear_attachment = pClearAttachments[i]; |
| 3460 | |
| 3461 | if (clear_attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3462 | RecordResetScopeZcullDirection(*cmd_state); |
| 3463 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 3464 | if ((clear_attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) && |
| 3465 | clear_attachment.colorAttachment != VK_ATTACHMENT_UNUSED && |
| 3466 | pColorAttachments) { |
| 3467 | const auto& attachment = pColorAttachments[clear_attachment.colorAttachment]; |
| 3468 | if (attachment.imageView) { |
| 3469 | auto image_view_state = Get<IMAGE_VIEW_STATE>(attachment.imageView); |
| 3470 | const VkFormat format = image_view_state->create_info.format; |
| 3471 | RecordClearColor(format, clear_attachment.clearValue.color); |
| 3472 | } |
| 3473 | } |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | // TODO: Implement other best practices for dynamic rendering |
| 3478 | |
| 3479 | } else { |
ziga-lunarg | 885c654 | 2022-03-07 01:08:25 +0100 | [diff] [blame] | 3480 | auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass]; |
| 3481 | for (uint32_t i = 0; i < attachmentCount; i++) { |
| 3482 | auto& attachment = pClearAttachments[i]; |
| 3483 | uint32_t fb_attachment = VK_ATTACHMENT_UNUSED; |
| 3484 | VkImageAspectFlags aspects = attachment.aspectMask; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3485 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3486 | if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT) { |
| 3487 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 3488 | RecordResetScopeZcullDirection(*cmd_state); |
| 3489 | } |
| 3490 | } |
ziga-lunarg | 885c654 | 2022-03-07 01:08:25 +0100 | [diff] [blame] | 3491 | if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { |
| 3492 | if (subpass.pDepthStencilAttachment) { |
| 3493 | fb_attachment = subpass.pDepthStencilAttachment->attachment; |
| 3494 | } |
| 3495 | } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) { |
| 3496 | fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3497 | } |
ziga-lunarg | 885c654 | 2022-03-07 01:08:25 +0100 | [diff] [blame] | 3498 | if (fb_attachment != VK_ATTACHMENT_UNUSED) { |
| 3499 | if (full_clear) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3500 | RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment, |
ziga-lunarg | 885c654 | 2022-03-07 01:08:25 +0100 | [diff] [blame] | 3501 | aspects, rectCount, pRects); |
| 3502 | } else { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3503 | RecordAttachmentAccess(*cmd_state, fb_attachment, aspects); |
ziga-lunarg | 885c654 | 2022-03-07 01:08:25 +0100 | [diff] [blame] | 3504 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 3505 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 3506 | const VkFormat format = rp_state->createInfo.pAttachments[fb_attachment].format; |
| 3507 | RecordClearColor(format, attachment.clearValue.color); |
| 3508 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3514 | void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 3515 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
| 3516 | ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, |
| 3517 | firstInstance); |
| 3518 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3519 | auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3520 | if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) { |
| 3521 | cmd_state->small_indexed_draw_call_count++; |
| 3522 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 3523 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3524 | ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()"); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3527 | void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 3528 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
| 3529 | StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); |
| 3530 | RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()"); |
| 3531 | } |
| 3532 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3533 | bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3534 | uint32_t drawCount, uint32_t stride) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3535 | bool skip = false; |
| 3536 | |
| 3537 | if (drawCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 3538 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero, |
| 3539 | "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3540 | } |
| 3541 | |
Rodrigo Locatti | 8419cde | 2022-03-30 18:45:13 -0300 | [diff] [blame] | 3542 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()"); |
| 3543 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3544 | return skip; |
| 3545 | } |
| 3546 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3547 | void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3548 | uint32_t count, uint32_t stride) { |
| 3549 | StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride); |
| 3550 | RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()"); |
| 3551 | } |
| 3552 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3553 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3554 | uint32_t drawCount, uint32_t stride) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3555 | bool skip = false; |
| 3556 | |
| 3557 | if (drawCount == 0) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 3558 | skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero, |
| 3559 | "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero."); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3560 | } |
| 3561 | |
Rodrigo Locatti | 8419cde | 2022-03-30 18:45:13 -0300 | [diff] [blame] | 3562 | skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()"); |
| 3563 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3564 | return skip; |
| 3565 | } |
| 3566 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3567 | void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3568 | uint32_t count, uint32_t stride) { |
| 3569 | StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride); |
| 3570 | RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()"); |
| 3571 | } |
| 3572 | |
Rodrigo Locatti | 467344a | 2022-03-30 18:48:13 -0300 | [diff] [blame] | 3573 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3574 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3575 | uint32_t maxDrawCount, uint32_t stride) const { |
| 3576 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()"); |
| 3577 | |
| 3578 | return skip; |
| 3579 | } |
| 3580 | |
| 3581 | void BestPractices::PostCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3582 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3583 | uint32_t maxDrawCount, uint32_t stride) { |
| 3584 | StateTracker::PostCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 3585 | maxDrawCount, stride); |
| 3586 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCount()"); |
| 3587 | } |
| 3588 | |
| 3589 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3590 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3591 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3592 | uint32_t stride) const { |
| 3593 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountAMD"); |
| 3594 | |
| 3595 | return skip; |
| 3596 | } |
| 3597 | |
| 3598 | void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3599 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3600 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3601 | uint32_t stride) { |
| 3602 | StateTracker::PostCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 3603 | maxDrawCount, stride); |
| 3604 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountAMD()"); |
| 3605 | } |
| 3606 | |
| 3607 | bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3608 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3609 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3610 | uint32_t stride) const { |
| 3611 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR"); |
| 3612 | |
| 3613 | return skip; |
| 3614 | } |
| 3615 | |
| 3616 | void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3617 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3618 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3619 | uint32_t stride) { |
| 3620 | StateTracker::PostCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 3621 | maxDrawCount, stride); |
| 3622 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountKHR()"); |
| 3623 | } |
| 3624 | |
| 3625 | bool BestPractices::PreCallValidateCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount, |
| 3626 | uint32_t firstInstance, VkBuffer counterBuffer, |
| 3627 | VkDeviceSize counterBufferOffset, uint32_t counterOffset, |
| 3628 | uint32_t vertexStride) const { |
| 3629 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectByteCountEXT"); |
| 3630 | |
| 3631 | return skip; |
| 3632 | } |
| 3633 | |
| 3634 | void BestPractices::PostCallRecordCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount, |
| 3635 | uint32_t firstInstance, VkBuffer counterBuffer, |
| 3636 | VkDeviceSize counterBufferOffset, uint32_t counterOffset, |
| 3637 | uint32_t vertexStride) { |
| 3638 | StateTracker::PostCallRecordCmdDrawIndirectByteCountEXT(commandBuffer, instanceCount, firstInstance, counterBuffer, |
| 3639 | counterBufferOffset, counterOffset, vertexStride); |
| 3640 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectByteCountEXT()"); |
| 3641 | } |
| 3642 | |
| 3643 | bool BestPractices::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3644 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3645 | uint32_t stride) const { |
| 3646 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCount"); |
| 3647 | |
| 3648 | return skip; |
| 3649 | } |
| 3650 | |
| 3651 | void BestPractices::PostCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3652 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3653 | uint32_t stride) { |
| 3654 | StateTracker::PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, |
| 3655 | stride); |
| 3656 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCount()"); |
| 3657 | } |
| 3658 | |
| 3659 | bool BestPractices::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3660 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3661 | uint32_t maxDrawCount, uint32_t stride) const { |
| 3662 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountAMD"); |
| 3663 | |
| 3664 | return skip; |
| 3665 | } |
| 3666 | |
| 3667 | void BestPractices::PostCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3668 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3669 | uint32_t maxDrawCount, uint32_t stride) { |
| 3670 | StateTracker::PostCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, |
| 3671 | stride); |
| 3672 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountAMD()"); |
| 3673 | } |
| 3674 | |
| 3675 | bool BestPractices::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3676 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3677 | uint32_t maxDrawCount, uint32_t stride) const { |
| 3678 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountKHR"); |
| 3679 | |
| 3680 | return skip; |
| 3681 | } |
| 3682 | |
| 3683 | void BestPractices::PostCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3684 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3685 | uint32_t maxDrawCount, uint32_t stride) { |
| 3686 | StateTracker::PostCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, |
| 3687 | stride); |
| 3688 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountKHR()"); |
| 3689 | } |
| 3690 | |
| 3691 | bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3692 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3693 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3694 | uint32_t stride) const { |
| 3695 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectCountNV"); |
| 3696 | |
| 3697 | return skip; |
| 3698 | } |
| 3699 | |
| 3700 | void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3701 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3702 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3703 | uint32_t stride) { |
| 3704 | StateTracker::PostCallRecordCmdDrawMeshTasksIndirectCountNV(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 3705 | maxDrawCount, stride); |
| 3706 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectCountNV()"); |
| 3707 | } |
| 3708 | |
| 3709 | bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3710 | uint32_t drawCount, uint32_t stride) const { |
| 3711 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectNV"); |
| 3712 | |
| 3713 | return skip; |
| 3714 | } |
| 3715 | |
| 3716 | void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3717 | uint32_t drawCount, uint32_t stride) { |
| 3718 | StateTracker::PostCallRecordCmdDrawMeshTasksIndirectNV(commandBuffer, buffer, offset, drawCount, stride); |
| 3719 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectNV()"); |
| 3720 | } |
| 3721 | |
| 3722 | bool BestPractices::PreCallValidateCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) const { |
| 3723 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksNV"); |
| 3724 | |
| 3725 | return skip; |
| 3726 | } |
| 3727 | |
| 3728 | void BestPractices::PostCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) { |
| 3729 | StateTracker::PostCallRecordCmdDrawMeshTasksNV(commandBuffer, taskCount, firstTask); |
| 3730 | RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksNV()"); |
| 3731 | } |
| 3732 | |
| 3733 | bool BestPractices::PreCallValidateCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 3734 | const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount, |
| 3735 | uint32_t firstInstance, uint32_t stride, |
| 3736 | const int32_t* pVertexOffset) const { |
| 3737 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiIndexedEXT"); |
| 3738 | |
| 3739 | return skip; |
| 3740 | } |
| 3741 | |
| 3742 | void BestPractices::PostCallRecordCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 3743 | const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount, |
| 3744 | uint32_t firstInstance, uint32_t stride, const int32_t* pVertexOffset) { |
| 3745 | StateTracker::PostCallRecordCmdDrawMultiIndexedEXT(commandBuffer, drawCount, pIndexInfo, instanceCount, firstInstance, stride, |
| 3746 | pVertexOffset); |
| 3747 | uint32_t count = 0; |
| 3748 | for (uint32_t i = 0; i < drawCount; ++i) { |
| 3749 | count += pIndexInfo[i].indexCount; |
| 3750 | } |
| 3751 | RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiIndexedEXT()"); |
| 3752 | } |
| 3753 | |
| 3754 | bool BestPractices::PreCallValidateCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, const VkMultiDrawInfoEXT* pVertexInfo, |
| 3755 | uint32_t instanceCount, uint32_t firstInstance, uint32_t stride) const { |
| 3756 | bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiEXT"); |
| 3757 | |
| 3758 | return skip; |
| 3759 | } |
| 3760 | |
| 3761 | void BestPractices::PostCallRecordCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 3762 | const VkMultiDrawInfoEXT* pVertexInfo, uint32_t instanceCount, |
| 3763 | uint32_t firstInstance, uint32_t stride) { |
| 3764 | StateTracker::PostCallRecordCmdDrawMultiEXT(commandBuffer, drawCount, pVertexInfo, instanceCount, firstInstance, stride); |
| 3765 | uint32_t count = 0; |
| 3766 | for (uint32_t i = 0; i < drawCount; ++i) { |
| 3767 | count += pVertexInfo[i].vertexCount; |
| 3768 | } |
| 3769 | RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiEXT()"); |
| 3770 | } |
| 3771 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3772 | void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) { |
| 3773 | for (auto descriptor_set : cb_state.validated_descriptor_sets) { |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 3774 | for (const auto& binding : *descriptor_set) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3775 | // For bindless scenarios, we should not attempt to track descriptor set state. |
| 3776 | // It is highly uncertain which resources are actually bound. |
| 3777 | // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state. |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 3778 | if (binding->binding_flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT | |
| 3779 | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3780 | continue; |
| 3781 | } |
Hans-Kristian Arntzen | a819901 | 2021-03-22 12:10:07 +0100 | [diff] [blame] | 3782 | |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 3783 | for (uint32_t i = 0; i < binding->count; ++i) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3784 | VkImageView image_view{VK_NULL_HANDLE}; |
| 3785 | |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 3786 | auto descriptor = binding->GetDescriptor(i); |
ziga-lunarg | 33d806c | 2022-05-05 17:00:52 +0200 | [diff] [blame] | 3787 | if (!descriptor) { |
| 3788 | continue; |
| 3789 | } |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3790 | switch (descriptor->GetClass()) { |
| 3791 | case cvdescriptorset::DescriptorClass::Image: { |
| 3792 | if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) { |
| 3793 | image_view = image_descriptor->GetImageView(); |
| 3794 | } |
| 3795 | break; |
| 3796 | } |
| 3797 | case cvdescriptorset::DescriptorClass::ImageSampler: { |
| 3798 | if (const auto image_sampler_descriptor = |
| 3799 | static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) { |
| 3800 | image_view = image_sampler_descriptor->GetImageView(); |
| 3801 | } |
| 3802 | break; |
| 3803 | } |
| 3804 | default: |
| 3805 | break; |
Hans-Kristian Arntzen | a819901 | 2021-03-22 12:10:07 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3808 | if (image_view) { |
| 3809 | auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view); |
| 3810 | QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(), |
| 3811 | IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 3819 | uint32_t firstVertex, uint32_t firstInstance) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3820 | const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 3821 | ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()"); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3825 | uint32_t drawCount, uint32_t stride) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3826 | const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 3827 | ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()"); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3831 | uint32_t drawCount, uint32_t stride) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3832 | const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 3833 | ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndexedIndirect()"); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 3834 | } |
| 3835 | |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3836 | bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3837 | uint32_t groupCountZ) const { |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3838 | bool skip = false; |
| 3839 | |
| 3840 | if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 3841 | skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero, |
| 3842 | "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32 |
| 3843 | ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").", |
| 3844 | groupCountX, groupCountY, groupCountZ); |
Camden | 5b184be | 2019-08-13 07:50:19 -0600 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | return skip; |
| 3848 | } |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 3849 | |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 3850 | bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const { |
| 3851 | bool skip = false; |
| 3852 | skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
| 3853 | skip |= ValidateCmdEndRenderPass(commandBuffer); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3854 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 3855 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3856 | assert(cmd_state); |
| 3857 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3858 | } |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 3859 | return skip; |
| 3860 | } |
| 3861 | |
| 3862 | bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const { |
| 3863 | bool skip = false; |
| 3864 | skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
| 3865 | skip |= ValidateCmdEndRenderPass(commandBuffer); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3866 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 3867 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3868 | assert(cmd_state); |
| 3869 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3870 | } |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 3871 | return skip; |
| 3872 | } |
| 3873 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3874 | bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const { |
| 3875 | bool skip = false; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3876 | skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer); |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 3877 | skip |= ValidateCmdEndRenderPass(commandBuffer); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3878 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 3879 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3880 | assert(cmd_state); |
| 3881 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3882 | } |
| 3883 | return skip; |
| 3884 | } |
| 3885 | |
| 3886 | bool BestPractices::PreCallValidateCmdEndRendering(VkCommandBuffer commandBuffer) const { |
| 3887 | bool skip = false; |
| 3888 | skip |= StateTracker::PreCallValidateCmdEndRendering(commandBuffer); |
| 3889 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 3890 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3891 | assert(cmd_state); |
| 3892 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3893 | } |
| 3894 | return skip; |
| 3895 | } |
| 3896 | |
| 3897 | bool BestPractices::PreCallValidateCmdEndRenderingKHR(VkCommandBuffer commandBuffer) const { |
| 3898 | bool skip = false; |
| 3899 | skip |= StateTracker::PreCallValidateCmdEndRenderingKHR(commandBuffer); |
| 3900 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 3901 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 3902 | assert(cmd_state); |
| 3903 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 3904 | } |
Hans-Kristian Arntzen | d9941a9 | 2021-06-18 12:31:30 +0200 | [diff] [blame] | 3905 | return skip; |
| 3906 | } |
| 3907 | |
| 3908 | bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const { |
| 3909 | bool skip = false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3910 | const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3911 | |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3912 | if (cmd == nullptr) return skip; |
| 3913 | auto &render_pass_state = cmd->render_pass_state; |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3914 | |
LawG4 | b21485c | 2022-02-28 13:46:48 +0000 | [diff] [blame] | 3915 | // Does the number of draw calls classified as depth only surpass the vendor limit for a specified vendor |
| 3916 | bool depth_only_arm = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm && |
| 3917 | render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm; |
| 3918 | bool depth_only_img = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsIMG && |
| 3919 | render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsIMG; |
| 3920 | |
| 3921 | // Only send the warning when the vendor is enabled and a depth prepass is detected |
LawG4 | 98ec450 | 2022-04-05 09:08:25 +0100 | [diff] [blame] | 3922 | bool uses_depth = |
| 3923 | (render_pass_state.depthAttachment || render_pass_state.colorAttachment) && |
LawG4 | 5507e14 | 2022-04-08 09:36:54 +0100 | [diff] [blame] | 3924 | ((depth_only_arm && VendorCheckEnabled(kBPVendorArm)) || (depth_only_img && VendorCheckEnabled(kBPVendorIMG))); |
LawG4 | b21485c | 2022-02-28 13:46:48 +0000 | [diff] [blame] | 3925 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3926 | if (uses_depth) { |
| 3927 | skip |= LogPerformanceWarning( |
| 3928 | device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 3929 | "%s %s: Depth pre-passes may be in use. In general, this is not recommended in tile-based deferred " |
LawG4 | b21485c | 2022-02-28 13:46:48 +0000 | [diff] [blame] | 3930 | "renderering architectures; such as those in Arm Mali or PowerVR GPUs. Since they can remove geometry " |
| 3931 | "hidden by other opaque geometry. Mali has Forward Pixel Killing (FPK), PowerVR has Hiden Surface " |
| 3932 | "Remover (HSR) in which case, using depth pre-passes for hidden surface removal may worsen performance.", |
| 3933 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG)); |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3934 | } |
| 3935 | |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3936 | RENDER_PASS_STATE* rp = cmd->activeRenderPass.get(); |
| 3937 | |
LawG4 | 0da9c3d | 2022-03-01 09:51:01 +0000 | [diff] [blame] | 3938 | if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) && rp) { |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3939 | // If we use an attachment on-tile, we should access it in some way. Otherwise, |
| 3940 | // it is redundant to have it be part of the render pass. |
| 3941 | // Only consider it redundant if it will actually consume bandwidth, i.e. |
| 3942 | // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign, |
| 3943 | // as is using pure input attachments. |
| 3944 | // CLEAR -> STORE might be considered a "useful" thing to do, but |
| 3945 | // the optimal thing to do is to defer the clear until you're actually |
| 3946 | // going to render to the image. |
| 3947 | |
| 3948 | uint32_t num_attachments = rp->createInfo.attachmentCount; |
| 3949 | for (uint32_t i = 0; i < num_attachments; i++) { |
Hans-Kristian Arntzen | 237663c | 2021-07-01 14:36:40 +0200 | [diff] [blame] | 3950 | if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) || |
| 3951 | RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) { |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3952 | continue; |
| 3953 | } |
| 3954 | |
| 3955 | auto& attachment = rp->createInfo.pAttachments[i]; |
| 3956 | |
| 3957 | VkImageAspectFlags bandwidth_aspects = 0; |
| 3958 | |
| 3959 | if (!FormatIsStencilOnly(attachment.format) && |
| 3960 | (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || |
| 3961 | attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) { |
| 3962 | if (FormatHasDepth(attachment.format)) { |
| 3963 | bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT; |
| 3964 | } else { |
| 3965 | bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT; |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | if (FormatHasStencil(attachment.format) && |
| 3970 | (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD || |
| 3971 | attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) { |
| 3972 | bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT; |
| 3973 | } |
| 3974 | |
| 3975 | if (!bandwidth_aspects) { |
| 3976 | continue; |
| 3977 | } |
| 3978 | |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3979 | auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(), |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 3980 | [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; }); |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3981 | uint32_t untouched_aspects = bandwidth_aspects; |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 3982 | if (itr != render_pass_state.touchesAttachments.end()) { |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3983 | untouched_aspects &= ~itr->aspects; |
| 3984 | } |
| 3985 | |
| 3986 | if (untouched_aspects) { |
| 3987 | skip |= LogPerformanceWarning( |
| 3988 | device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile, |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 3989 | "%s %s: Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) " |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3990 | "was never accessed by a pipeline or clear command. " |
LawG4 | 0da9c3d | 2022-03-01 09:51:01 +0000 | [diff] [blame] | 3991 | "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the " |
LawG4 | 015be1c | 2022-03-01 10:37:52 +0000 | [diff] [blame] | 3992 | "render pass if the attachments are not intended to be accessed.", |
LawG4 | 0da9c3d | 2022-03-01 09:51:01 +0000 | [diff] [blame] | 3993 | VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), i, attachment.format, untouched_aspects); |
Hans-Kristian Arntzen | 808bfa1 | 2021-06-18 13:52:45 +0200 | [diff] [blame] | 3994 | } |
| 3995 | } |
| 3996 | } |
| 3997 | |
Sam Walls | 0961ec0 | 2020-03-31 16:39:15 +0100 | [diff] [blame] | 3998 | return skip; |
| 3999 | } |
| 4000 | |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4001 | void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4002 | const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 4003 | ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()"); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4007 | const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
| 4008 | ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()"); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4009 | } |
| 4010 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 4011 | bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice, |
| 4012 | const char* api_name) const { |
| 4013 | bool skip = false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4014 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 4015 | |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 4016 | if (bp_pd_state) { |
| 4017 | if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) { |
| 4018 | skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled, |
| 4019 | "Potential problem with calling %s() without first retrieving properties from " |
| 4020 | "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.", |
| 4021 | api_name); |
| 4022 | } |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | return skip; |
| 4026 | } |
| 4027 | |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 4028 | bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4029 | uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const { |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 4030 | bool skip = false; |
| 4031 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 4032 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR"); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 4033 | |
Camden Stocker | 9c05144 | 2019-11-06 14:28:43 -0800 | [diff] [blame] | 4034 | return skip; |
| 4035 | } |
| 4036 | |
| 4037 | bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode, |
| 4038 | uint32_t planeIndex, |
| 4039 | VkDisplayPlaneCapabilitiesKHR* pCapabilities) const { |
| 4040 | bool skip = false; |
| 4041 | |
| 4042 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR"); |
| 4043 | |
| 4044 | return skip; |
| 4045 | } |
| 4046 | |
| 4047 | bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice, |
| 4048 | const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo, |
| 4049 | VkDisplayPlaneCapabilities2KHR* pCapabilities) const { |
| 4050 | bool skip = false; |
| 4051 | |
| 4052 | skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR"); |
Camden | 83a9c37 | 2019-08-14 11:41:38 -0600 | [diff] [blame] | 4053 | |
| 4054 | return skip; |
| 4055 | } |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4056 | |
| 4057 | bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4058 | VkImage* pSwapchainImages) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4059 | bool skip = false; |
| 4060 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4061 | auto swapchain_state = Get<bp_state::Swapchain>(swapchain); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4062 | |
Nathaniel Cesario | 39152e6 | 2021-07-02 13:04:16 -0600 | [diff] [blame] | 4063 | if (swapchain_state && pSwapchainImages) { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4064 | // Compare the preliminary value of *pSwapchainImageCount with the value this time: |
Nathaniel Cesario | 39152e6 | 2021-07-02 13:04:16 -0600 | [diff] [blame] | 4065 | if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4066 | skip |= |
| 4067 | LogWarning(device, kVUID_Core_Swapchain_PriorCount, |
| 4068 | "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has " |
| 4069 | "been seen for pSwapchainImages."); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4070 | } |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4071 | |
Nathaniel Cesario | 4ce9838 | 2021-05-28 11:33:20 -0600 | [diff] [blame] | 4072 | if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) { |
| 4073 | skip |= LogWarning( |
| 4074 | device, kVUID_BestPractices_Swapchain_InvalidCount, |
| 4075 | "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a " |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4076 | "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.", |
Nathaniel Cesario | 4ce9838 | 2021-05-28 11:33:20 -0600 | [diff] [blame] | 4077 | *pSwapchainImageCount, swapchain_state->get_swapchain_image_count); |
| 4078 | } |
| 4079 | } |
| 4080 | |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4081 | return skip; |
| 4082 | } |
| 4083 | |
| 4084 | // Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version |
Jeremy Gebben | 383b9a3 | 2021-09-08 16:31:33 -0600 | [diff] [blame] | 4085 | bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4086 | uint32_t requested_queue_family_property_count, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4087 | const CALL_STATE call_state, |
| 4088 | const char* caller_name) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4089 | bool skip = false; |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4090 | // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count |
| 4091 | if (UNCALLED == call_state) { |
| 4092 | skip |= LogWarning( |
Jeremy Gebben | 383b9a3 | 2021-09-08 16:31:33 -0600 | [diff] [blame] | 4093 | bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4094 | "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is " |
| 4095 | "recommended " |
| 4096 | "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.", |
| 4097 | caller_name, caller_name); |
| 4098 | // Then verify that pCount that is passed in on second call matches what was returned |
Jeremy Gebben | 383b9a3 | 2021-09-08 16:31:33 -0600 | [diff] [blame] | 4099 | } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) { |
| 4100 | skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4101 | "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32 |
| 4102 | ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32 |
| 4103 | ". It is recommended to instead receive all the properties by calling %s with " |
| 4104 | "pQueueFamilyPropertyCount that was " |
| 4105 | "previously obtained by calling %s with NULL pQueueFamilyProperties.", |
Jeremy Gebben | 383b9a3 | 2021-09-08 16:31:33 -0600 | [diff] [blame] | 4106 | caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4107 | caller_name); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | return skip; |
| 4111 | } |
| 4112 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4113 | bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV( |
| 4114 | VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const { |
Camden Stocker | 8251058 | 2019-09-03 14:00:16 -0600 | [diff] [blame] | 4115 | bool skip = false; |
| 4116 | |
| 4117 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 4118 | auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure); |
Camden Stocker | 8251058 | 2019-09-03 14:00:16 -0600 | [diff] [blame] | 4119 | if (!as_state->memory_requirements_checked) { |
| 4120 | // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling |
| 4121 | // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with |
| 4122 | // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4123 | skip |= LogWarning( |
| 4124 | device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery, |
Camden Stocker | 8251058 | 2019-09-03 14:00:16 -0600 | [diff] [blame] | 4125 | "vkBindAccelerationStructureMemoryNV(): " |
| 4126 | "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.", |
| 4127 | report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str()); |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | return skip; |
| 4132 | } |
| 4133 | |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4134 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, |
| 4135 | uint32_t* pQueueFamilyPropertyCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4136 | VkQueueFamilyProperties* pQueueFamilyProperties) const { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4137 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4138 | if (pQueueFamilyProperties && bp_pd_state) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4139 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4140 | bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState, |
| 4141 | "vkGetPhysicalDeviceQueueFamilyProperties()"); |
| 4142 | } |
| 4143 | return false; |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4144 | } |
| 4145 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 4146 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice, |
| 4147 | uint32_t* pQueueFamilyPropertyCount, |
| 4148 | VkQueueFamilyProperties2* pQueueFamilyProperties) const { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4149 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4150 | if (pQueueFamilyProperties && bp_pd_state) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4151 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4152 | bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State, |
| 4153 | "vkGetPhysicalDeviceQueueFamilyProperties2()"); |
| 4154 | } |
| 4155 | return false; |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4156 | } |
| 4157 | |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4158 | bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR( |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 4159 | VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4160 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4161 | if (pQueueFamilyProperties && bp_pd_state) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4162 | return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount, |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 4163 | bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState, |
| 4164 | "vkGetPhysicalDeviceQueueFamilyProperties2KHR()"); |
| 4165 | } |
| 4166 | return false; |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, |
| 4170 | uint32_t* pSurfaceFormatCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4171 | VkSurfaceFormatKHR* pSurfaceFormats) const { |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4172 | if (!pSurfaceFormats) return false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4173 | const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 4174 | const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState; |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4175 | bool skip = false; |
| 4176 | if (call_state == UNCALLED) { |
| 4177 | // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't |
| 4178 | // previously call this function with a NULL value of pSurfaceFormats: |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4179 | skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount, |
| 4180 | "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior " |
| 4181 | "positive value has been seen for pSurfaceFormats."); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4182 | } else { |
Jeremy Gebben | c7a834a | 2021-09-08 18:39:30 -0600 | [diff] [blame] | 4183 | if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) { |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4184 | skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch, |
| 4185 | "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with " |
| 4186 | "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned " |
| 4187 | "when pSurfaceFormatCount was NULL.", |
Jeremy Gebben | c7a834a | 2021-09-08 18:39:30 -0600 | [diff] [blame] | 4188 | *pSurfaceFormatCount, bp_pd_state->surface_formats_count); |
Camden | 05de2d4 | 2019-08-19 10:23:56 -0600 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | return skip; |
| 4192 | } |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4193 | |
| 4194 | bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4195 | VkFence fence) const { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4196 | bool skip = false; |
| 4197 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4198 | for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) { |
| 4199 | const VkBindSparseInfo& bind_info = pBindInfo[bind_idx]; |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4200 | // Store sparse binding image_state and after binding is complete make sure that any requiring metadata have it bound |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 4201 | layer_data::unordered_set<const IMAGE_STATE*> sparse_images; |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4202 | // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state |
| 4203 | // in RecordQueueBindSparse. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 4204 | layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata; |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4205 | // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4206 | for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) { |
| 4207 | const auto& image_bind = bind_info.pImageBinds[i]; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4208 | auto image_state = Get<IMAGE_STATE>(image_bind.image); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4209 | if (!image_state) { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4210 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4211 | } |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4212 | sparse_images.insert(image_state.get()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4213 | if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) { |
| 4214 | if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) { |
| 4215 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4216 | skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4217 | "vkQueueBindSparse(): Binding sparse memory to %s without first calling " |
| 4218 | "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4219 | report_data->FormatHandle(image_state->image()).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4220 | } |
| 4221 | } |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 4222 | if (!image_state->memory_requirements_checked[0]) { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4223 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4224 | skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4225 | "vkQueueBindSparse(): Binding sparse memory to %s without first calling " |
| 4226 | "vkGetImageMemoryRequirements() to retrieve requirements.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4227 | report_data->FormatHandle(image_state->image()).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4228 | } |
| 4229 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4230 | for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) { |
| 4231 | const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i]; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4232 | auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4233 | if (!image_state) { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4234 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4235 | } |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4236 | sparse_images.insert(image_state.get()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4237 | if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) { |
| 4238 | if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) { |
| 4239 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4240 | skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4241 | "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling " |
| 4242 | "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4243 | report_data->FormatHandle(image_state->image()).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4244 | } |
| 4245 | } |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 4246 | if (!image_state->memory_requirements_checked[0]) { |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4247 | // For now just warning if sparse image binding occurs without calling to get reqs first |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4248 | skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4249 | "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling " |
| 4250 | "vkGetImageMemoryRequirements() to retrieve requirements.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4251 | report_data->FormatHandle(image_state->image()).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4252 | } |
| 4253 | for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) { |
| 4254 | if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4255 | sparse_images_with_metadata.insert(image_state.get()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | for (const auto& sparse_image_state : sparse_images) { |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4260 | if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound && |
| 4261 | 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] | 4262 | // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4263 | skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState, |
Mark Lobodzinski | b6e2a28 | 2020-01-29 16:03:26 -0700 | [diff] [blame] | 4264 | "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no " |
| 4265 | "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 4266 | report_data->FormatHandle(sparse_image_state->image()).c_str()); |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
Rodrigo Locatti | 7ab778d | 2022-03-09 18:57:15 -0300 | [diff] [blame] | 4271 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4272 | auto queue_state = Get<QUEUE_STATE>(queue); |
| 4273 | if (queue_state && queue_state->queueFamilyProperties.queueFlags != (VK_QUEUE_TRANSFER_BIT | VK_QUEUE_SPARSE_BINDING_BIT)) { |
| 4274 | skip |= LogPerformanceWarning(queue, kVUID_BestPractices_QueueBindSparse_NotAsync, |
| 4275 | "vkQueueBindSparse() issued on queue %s. All binds should happen on an asynchronous copy " |
| 4276 | "queue to hide the OS scheduling and submit costs.", |
| 4277 | report_data->FormatHandle(queue).c_str()); |
| 4278 | } |
| 4279 | } |
| 4280 | |
Camden Stocker | 23cc47d | 2019-09-03 14:53:57 -0600 | [diff] [blame] | 4281 | return skip; |
| 4282 | } |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4283 | |
Mark Lobodzinski | 84101d7 | 2020-04-24 09:43:48 -0600 | [diff] [blame] | 4284 | void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo, |
| 4285 | VkFence fence, VkResult result) { |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 4286 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 205b7a0 | 2020-02-21 13:23:17 -0700 | [diff] [blame] | 4287 | return; |
| 4288 | } |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4289 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4290 | for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) { |
| 4291 | const VkBindSparseInfo& bind_info = pBindInfo[bind_idx]; |
| 4292 | for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) { |
| 4293 | const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i]; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4294 | auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4295 | if (!image_state) { |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4296 | continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 4297 | } |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 4298 | for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) { |
| 4299 | if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) { |
| 4300 | image_state->sparse_metadata_bound = true; |
| 4301 | } |
| 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | } |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4306 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4307 | bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount, |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 4308 | const VkClearRect* pRects) const { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4309 | if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4310 | // We don't know the accurate render area in a secondary, |
| 4311 | // so assume we clear the entire frame buffer. |
| 4312 | // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear. |
| 4313 | return true; |
| 4314 | } |
| 4315 | |
| 4316 | // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command. |
| 4317 | for (uint32_t i = 0; i < rectCount; i++) { |
| 4318 | auto& rect = pRects[i]; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4319 | auto& render_area = cmd.activeRenderPassBeginInfo.renderArea; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4320 | if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) { |
| 4321 | return true; |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | return false; |
| 4326 | } |
| 4327 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4328 | bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment, |
| 4329 | VkImageAspectFlags aspects, bool secondary) const { |
| 4330 | const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get(); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4331 | bool skip = false; |
| 4332 | |
| 4333 | if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) { |
| 4334 | return skip; |
| 4335 | } |
| 4336 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4337 | const auto& rp_state = cmd.render_pass_state; |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4338 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4339 | auto attachment_itr = |
| 4340 | std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(), |
| 4341 | [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; }); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4342 | |
| 4343 | // Only report aspects which haven't been touched yet. |
| 4344 | VkImageAspectFlags new_aspects = aspects; |
Jeremy Gebben | 7c2cd8b | 2021-08-11 15:40:38 -0600 | [diff] [blame] | 4345 | if (attachment_itr != rp_state.touchesAttachments.end()) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4346 | new_aspects &= ~attachment_itr->aspects; |
| 4347 | } |
| 4348 | |
| 4349 | // Warn if this is issued prior to Draw Cmd and clearing the entire attachment |
sjfricke | 52defd4 | 2022-08-08 16:37:46 +0900 | [diff] [blame] | 4350 | if (!cmd.has_draw_cmd) { |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4351 | skip |= LogPerformanceWarning( |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4352 | cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw, |
Hans-Kristian Arntzen | 4ddd618 | 2021-06-18 12:16:33 +0200 | [diff] [blame] | 4353 | "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you " |
| 4354 | "use RenderPass LOAD_OP_CLEAR on attachments instead.", |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4355 | report_data->FormatHandle(cmd.Handle()).c_str()); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) && |
| 4359 | rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
| 4360 | skip |= LogPerformanceWarning( |
| 4361 | device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 4362 | "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, " |
| 4363 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 4364 | "it is more efficient.", |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4365 | secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && |
| 4369 | rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4370 | skip |= |
| 4371 | LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 4372 | "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, " |
| 4373 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 4374 | "it is more efficient.", |
| 4375 | secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str()); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4376 | |
| 4377 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 4378 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(cmd.commandBuffer()); |
| 4379 | assert(cmd_state); |
| 4380 | skip |= ValidateZcullScope(*cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4381 | } |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) && |
| 4385 | rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4386 | skip |= |
| 4387 | LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad, |
| 4388 | "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, " |
| 4389 | "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as " |
| 4390 | "it is more efficient.", |
| 4391 | secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str()); |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | return skip; |
| 4395 | } |
| 4396 | |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4397 | bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount, |
Camden Stocker | f55721f | 2019-09-09 11:04:49 -0600 | [diff] [blame] | 4398 | const VkClearAttachment* pAttachments, uint32_t rectCount, |
| 4399 | const VkClearRect* pRects) const { |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4400 | bool skip = false; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4401 | const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer); |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4402 | if (!cb_node) return skip; |
| 4403 | |
Hans-Kristian Arntzen | b658631 | 2021-07-05 11:43:39 +0200 | [diff] [blame] | 4404 | if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) { |
| 4405 | // Defer checks to ExecuteCommands. |
| 4406 | return skip; |
| 4407 | } |
| 4408 | |
| 4409 | // Only care about full clears, partial clears might have legitimate uses. |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4410 | const bool is_full_clear = ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects); |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4411 | |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4412 | // Check for uses of ClearAttachments along with LOAD_OP_LOAD, |
| 4413 | // as it can be more efficient to just use LOAD_OP_CLEAR |
locke-lunarg | aecf215 | 2020-05-12 17:15:41 -0600 | [diff] [blame] | 4414 | const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get(); |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4415 | if (rp) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4416 | if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) { |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4417 | const auto pColorAttachments = rp->dynamic_rendering_begin_rendering_info.pColorAttachments; |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4418 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4419 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4420 | for (uint32_t i = 0; i < attachmentCount; i++) { |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4421 | const auto& attachment = pAttachments[i]; |
| 4422 | if (attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 4423 | skip |= ValidateZcullScope(*cb_node); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4424 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4425 | if ((attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) && attachment.colorAttachment != VK_ATTACHMENT_UNUSED) { |
| 4426 | const auto& color_attachment = pColorAttachments[attachment.colorAttachment]; |
| 4427 | if (color_attachment.imageView) { |
| 4428 | auto image_view_state = Get<IMAGE_VIEW_STATE>(color_attachment.imageView); |
| 4429 | const VkFormat format = image_view_state->create_info.format; |
| 4430 | skip |= ValidateClearColor(commandBuffer, format, attachment.clearValue.color); |
| 4431 | } |
| 4432 | } |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4433 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4436 | if (is_full_clear) { |
| 4437 | // TODO: Implement ValidateClearAttachment for dynamic rendering |
| 4438 | } |
| 4439 | |
| 4440 | } else { |
| 4441 | const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass]; |
| 4442 | |
| 4443 | if (is_full_clear) { |
| 4444 | for (uint32_t i = 0; i < attachmentCount; i++) { |
| 4445 | const auto& attachment = pAttachments[i]; |
| 4446 | |
| 4447 | if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| 4448 | uint32_t color_attachment = attachment.colorAttachment; |
| 4449 | uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment; |
| 4450 | skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false); |
| 4451 | } |
| 4452 | |
| 4453 | if (subpass.pDepthStencilAttachment && |
| 4454 | (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) { |
| 4455 | uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment; |
| 4456 | skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false); |
| 4457 | } |
| 4458 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4459 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4460 | if (VendorCheckEnabled(kBPVendorNVIDIA) && rp->createInfo.pAttachments) { |
| 4461 | for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; ++attachment_idx) { |
| 4462 | const auto& attachment = pAttachments[attachment_idx]; |
| 4463 | |
| 4464 | if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| 4465 | const uint32_t fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment; |
| 4466 | if (fb_attachment != VK_ATTACHMENT_UNUSED) { |
| 4467 | const VkFormat format = rp->createInfo.pAttachments[fb_attachment].format; |
| 4468 | skip |= ValidateClearColor(commandBuffer, format, attachment.clearValue.color); |
| 4469 | } |
| 4470 | } |
| 4471 | } |
| 4472 | } |
Attilio Provenzano | 1d9a836 | 2020-02-27 12:23:51 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4476 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 4477 | for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) { |
| 4478 | if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) { |
| 4479 | bool black_check = false; |
| 4480 | black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f; |
| 4481 | black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f; |
| 4482 | black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f; |
| 4483 | black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f && |
| 4484 | pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f; |
| 4485 | |
| 4486 | bool white_check = false; |
| 4487 | white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f; |
| 4488 | white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f; |
| 4489 | white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f; |
| 4490 | white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f && |
| 4491 | pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f; |
| 4492 | |
| 4493 | if (black_check && white_check) { |
| 4494 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues, |
| 4495 | "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value." |
| 4496 | "Consider changing to one of the following:" |
| 4497 | "RGBA(0, 0, 0, 0) " |
| 4498 | "RGBA(0, 0, 0, 1) " |
| 4499 | "RGBA(1, 1, 1, 0) " |
| 4500 | "RGBA(1, 1, 1, 1)", |
| 4501 | VendorSpecificTag(kBPVendorAMD), attachment_idx); |
| 4502 | } |
| 4503 | } else { |
| 4504 | if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 && |
| 4505 | pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) && |
| 4506 | pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) { |
| 4507 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues, |
| 4508 | "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil " |
| 4509 | "attachment %" PRId32 " is not a fast clear value." |
| 4510 | "Consider changing to one of the following:" |
| 4511 | "D=0.0f, S=0" |
| 4512 | "D=1.0f, S=0", |
| 4513 | VendorSpecificTag(kBPVendorAMD), attachment_idx); |
| 4514 | } |
| 4515 | } |
| 4516 | } |
| 4517 | } |
| 4518 | |
Camden Stocker | f55721f | 2019-09-09 11:04:49 -0600 | [diff] [blame] | 4519 | return skip; |
Camden Stocker | 0e0f89b | 2019-10-16 12:24:31 -0700 | [diff] [blame] | 4520 | } |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 4521 | |
| 4522 | bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4523 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4524 | const VkImageResolve* pRegions) const { |
| 4525 | bool skip = false; |
| 4526 | |
| 4527 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 4528 | LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage, |
| 4529 | "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. " |
| 4530 | "This is a very slow and extremely bandwidth intensive path. " |
| 4531 | "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.", |
| 4532 | VendorSpecificTag(kBPVendorArm)); |
| 4533 | |
| 4534 | return skip; |
| 4535 | } |
| 4536 | |
Jeff Leger | 178b1e5 | 2020-10-05 12:22:23 -0400 | [diff] [blame] | 4537 | bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer, |
| 4538 | const VkResolveImageInfo2KHR* pResolveImageInfo) const { |
| 4539 | bool skip = false; |
| 4540 | |
| 4541 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 4542 | LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage, |
| 4543 | "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. " |
| 4544 | "This is a very slow and extremely bandwidth intensive path. " |
| 4545 | "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.", |
| 4546 | VendorSpecificTag(kBPVendorArm)); |
| 4547 | |
| 4548 | return skip; |
| 4549 | } |
| 4550 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 4551 | bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer, |
| 4552 | const VkResolveImageInfo2* pResolveImageInfo) const { |
| 4553 | bool skip = false; |
| 4554 | |
| 4555 | skip |= VendorCheckEnabled(kBPVendorArm) && |
| 4556 | LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage, |
| 4557 | "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. " |
| 4558 | "This is a very slow and extremely bandwidth intensive path. " |
| 4559 | "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.", |
| 4560 | VendorSpecificTag(kBPVendorArm)); |
| 4561 | |
| 4562 | return skip; |
| 4563 | } |
| 4564 | |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4565 | void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4566 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4567 | const VkImageResolve* pRegions) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4568 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4569 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4570 | auto src = Get<bp_state::Image>(srcImage); |
| 4571 | auto dst = Get<bp_state::Image>(dstImage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4572 | |
| 4573 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4574 | QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource); |
| 4575 | QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4576 | } |
| 4577 | } |
| 4578 | |
Hans-Kristian Arntzen | 9e030f1 | 2021-03-17 13:09:30 +0100 | [diff] [blame] | 4579 | void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer, |
| 4580 | const VkResolveImageInfo2KHR* pResolveImageInfo) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4581 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4582 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4583 | auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage); |
| 4584 | auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage); |
Hans-Kristian Arntzen | 9e030f1 | 2021-03-17 13:09:30 +0100 | [diff] [blame] | 4585 | uint32_t regionCount = pResolveImageInfo->regionCount; |
| 4586 | |
| 4587 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4588 | QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource); |
| 4589 | QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource); |
Hans-Kristian Arntzen | 9e030f1 | 2021-03-17 13:09:30 +0100 | [diff] [blame] | 4590 | } |
| 4591 | } |
| 4592 | |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 4593 | void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, |
| 4594 | const VkResolveImageInfo2* pResolveImageInfo) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4595 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 4596 | auto& funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4597 | auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage); |
| 4598 | auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage); |
Tony-LunarG | d36f5f3 | 2022-01-20 11:49:59 -0700 | [diff] [blame] | 4599 | uint32_t regionCount = pResolveImageInfo->regionCount; |
| 4600 | |
| 4601 | for (uint32_t i = 0; i < regionCount; i++) { |
| 4602 | QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, |
| 4603 | pResolveImageInfo->pRegions[i].srcSubresource); |
| 4604 | QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, |
| 4605 | pResolveImageInfo->pRegions[i].dstSubresource); |
| 4606 | } |
| 4607 | } |
| 4608 | |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4609 | void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 4610 | const VkClearColorValue* pColor, uint32_t rangeCount, |
| 4611 | const VkImageSubresourceRange* pRanges) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4612 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4613 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4614 | auto dst = Get<bp_state::Image>(image); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4615 | |
| 4616 | for (uint32_t i = 0; i < rangeCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4617 | QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4618 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4619 | |
| 4620 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4621 | RecordClearColor(dst->createInfo.format, *pColor); |
| 4622 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 4626 | const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount, |
| 4627 | const VkImageSubresourceRange* pRanges) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4628 | ValidationStateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, |
| 4629 | pRanges); |
| 4630 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4631 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4632 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4633 | auto dst = Get<bp_state::Image>(image); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4634 | |
| 4635 | for (uint32_t i = 0; i < rangeCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4636 | QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4637 | } |
Rodrigo Locatti | 6c4c266 | 2022-08-18 14:20:04 -0300 | [diff] [blame] | 4638 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4639 | for (uint32_t i = 0; i < rangeCount; i++) { |
| 4640 | RecordResetZcullDirection(*cb, image, pRanges[i]); |
| 4641 | } |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4642 | } |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4646 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4647 | const VkImageCopy* pRegions) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4648 | ValidationStateTracker::PreCallRecordCmdCopyImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, |
| 4649 | regionCount, pRegions); |
| 4650 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4651 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4652 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4653 | auto src = Get<bp_state::Image>(srcImage); |
| 4654 | auto dst = Get<bp_state::Image>(dstImage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4655 | |
| 4656 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4657 | QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource); |
| 4658 | QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 4663 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4664 | const VkBufferImageCopy* pRegions) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4665 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4666 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4667 | auto dst = Get<bp_state::Image>(dstImage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4668 | |
| 4669 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4670 | QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4675 | VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4676 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4677 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4678 | auto src = Get<bp_state::Image>(srcImage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4679 | |
| 4680 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4681 | QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4686 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4687 | const VkImageBlit* pRegions, VkFilter filter) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4688 | auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer); |
Hans-Kristian Arntzen | dd8acbb | 2021-03-22 13:41:47 +0100 | [diff] [blame] | 4689 | auto &funcs = cb->queue_submit_functions; |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4690 | auto src = Get<bp_state::Image>(srcImage); |
| 4691 | auto dst = Get<bp_state::Image>(dstImage); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4692 | |
| 4693 | for (uint32_t i = 0; i < regionCount; i++) { |
Hans-Kristian Arntzen | c8b831c | 2021-04-28 15:29:49 +0200 | [diff] [blame] | 4694 | QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource); |
| 4695 | QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource); |
ZandroFargnoli | 1ced2b6 | 2020-06-18 16:49:34 +0100 | [diff] [blame] | 4696 | } |
| 4697 | } |
| 4698 | |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 4699 | bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo, |
| 4700 | const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const { |
| 4701 | bool skip = false; |
| 4702 | |
| 4703 | if (VendorCheckEnabled(kBPVendorArm)) { |
| 4704 | if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) { |
| 4705 | skip |= LogPerformanceWarning( |
| 4706 | device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes, |
| 4707 | "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). " |
| 4708 | "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D " |
| 4709 | "image) are actually used. If you need different wrapping modes, disregard this warning.", |
Jeremy Gebben | da6b48f | 2021-05-13 10:46:18 -0600 | [diff] [blame] | 4710 | VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW); |
Attilio Provenzano | 02859b2 | 2020-02-27 14:17:28 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) { |
| 4714 | skip |= LogPerformanceWarning( |
| 4715 | device, kVUID_BestPractices_CreateSampler_LodClamping, |
| 4716 | "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. " |
| 4717 | "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod " |
| 4718 | "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.", |
| 4719 | VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod); |
| 4720 | } |
| 4721 | |
| 4722 | if (pCreateInfo->mipLodBias != 0.0f) { |
| 4723 | skip |= |
| 4724 | LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias, |
| 4725 | "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient " |
| 4726 | "descriptors being created and may cause reduced performance.", |
| 4727 | VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias); |
| 4728 | } |
| 4729 | |
| 4730 | if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER || |
| 4731 | pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER || |
| 4732 | pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) && |
| 4733 | (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) { |
| 4734 | skip |= LogPerformanceWarning( |
| 4735 | device, kVUID_BestPractices_CreateSampler_BorderClampColor, |
| 4736 | "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. " |
| 4737 | "This will lead to less efficient descriptors being created and may cause reduced performance. " |
| 4738 | "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.", |
| 4739 | VendorSpecificTag(kBPVendorArm)); |
| 4740 | } |
| 4741 | |
| 4742 | if (pCreateInfo->unnormalizedCoordinates) { |
| 4743 | skip |= LogPerformanceWarning( |
| 4744 | device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates, |
| 4745 | "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient " |
| 4746 | "descriptors being created and may cause reduced performance.", |
| 4747 | VendorSpecificTag(kBPVendorArm)); |
| 4748 | } |
| 4749 | |
| 4750 | if (pCreateInfo->anisotropyEnable) { |
| 4751 | skip |= LogPerformanceWarning( |
| 4752 | device, kVUID_BestPractices_CreateSampler_Anisotropy, |
| 4753 | "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created " |
| 4754 | "and may cause reduced performance.", |
| 4755 | VendorSpecificTag(kBPVendorArm)); |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | return skip; |
| 4760 | } |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 4761 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4762 | void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 4763 | const VkGraphicsPipelineCreateInfo* pCreateInfos, |
| 4764 | const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, |
| 4765 | void* cgpl_state) { |
| 4766 | ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, |
| 4767 | pPipelines); |
| 4768 | // AMD best practice |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4769 | num_pso_ += createInfoCount; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, |
| 4773 | const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount, |
| 4774 | const VkCopyDescriptorSet* pDescriptorCopies) const { |
| 4775 | bool skip = false; |
| 4776 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 4777 | if (descriptorCopyCount > 0) { |
| 4778 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors, |
| 4779 | "%s Performance warning: copying descriptor sets is not recommended", |
| 4780 | VendorSpecificTag(kBPVendorAMD)); |
| 4781 | } |
| 4782 | } |
| 4783 | |
| 4784 | return skip; |
| 4785 | } |
| 4786 | |
| 4787 | bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device, |
| 4788 | const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, |
| 4789 | const VkAllocationCallbacks* pAllocator, |
| 4790 | VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const { |
| 4791 | bool skip = false; |
| 4792 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 4793 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate, |
| 4794 | "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using " |
| 4795 | "vkUpdateDescriptorSet instead", |
| 4796 | VendorSpecificTag(kBPVendorAMD)); |
| 4797 | } |
| 4798 | |
| 4799 | return skip; |
| 4800 | } |
| 4801 | |
| 4802 | bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 4803 | const VkClearColorValue* pColor, uint32_t rangeCount, |
| 4804 | const VkImageSubresourceRange* pRanges) const { |
| 4805 | bool skip = false; |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4806 | |
| 4807 | auto dst = Get<bp_state::Image>(image); |
| 4808 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4809 | if (VendorCheckEnabled(kBPVendorAMD)) { |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 4810 | skip |= LogPerformanceWarning( |
| 4811 | device, kVUID_BestPractices_ClearAttachment_ClearImage, |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4812 | "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or " |
| 4813 | "vkCmdClearAttachments instead", |
| 4814 | VendorSpecificTag(kBPVendorAMD)); |
| 4815 | } |
Rodrigo Locatti | e4c08a0 | 2022-04-04 18:12:18 -0300 | [diff] [blame] | 4816 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4817 | skip |= ValidateClearColor(commandBuffer, dst->createInfo.format, *pColor); |
| 4818 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4819 | |
| 4820 | return skip; |
| 4821 | } |
| 4822 | |
| 4823 | bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, |
| 4824 | VkImageLayout imageLayout, |
| 4825 | const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount, |
| 4826 | const VkImageSubresourceRange* pRanges) const { |
| 4827 | bool skip = false; |
| 4828 | if (VendorCheckEnabled(kBPVendorAMD)) { |
| 4829 | skip |= LogPerformanceWarning( |
| 4830 | device, kVUID_BestPractices_ClearAttachment_ClearImage, |
| 4831 | "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or " |
| 4832 | "vkCmdClearAttachments instead", |
| 4833 | VendorSpecificTag(kBPVendorAMD)); |
| 4834 | } |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 4835 | const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer); |
| 4836 | assert(cmd_state); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4837 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4838 | for (uint32_t i = 0; i < rangeCount; i++) { |
Mark Young | 0a6b48f | 2022-08-18 11:17:02 -0600 | [diff] [blame] | 4839 | skip |= ValidateZcull(*cmd_state, image, pRanges[i]); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 4840 | } |
| 4841 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4842 | |
| 4843 | return skip; |
| 4844 | } |
| 4845 | |
| 4846 | bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo, |
| 4847 | const VkAllocationCallbacks* pAllocator, |
| 4848 | VkPipelineLayout* pPipelineLayout) const { |
| 4849 | bool skip = false; |
| 4850 | if (VendorCheckEnabled(kBPVendorAMD)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4851 | uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4852 | // Descriptor sets cost 1 DWORD each. |
| 4853 | // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. |
| 4854 | // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. |
| 4855 | // Push constants cost 1 DWORD per 4 bytes in the Push constant range. |
| 4856 | uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS |
| 4857 | for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 4858 | auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4859 | pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) { |
| 4863 | pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4; |
| 4864 | } |
| 4865 | |
| 4866 | if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) { |
| 4867 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall, |
| 4868 | "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts." |
| 4869 | "Descriptor sets cost 1 DWORD each. " |
| 4870 | "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. " |
| 4871 | "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. " |
| 4872 | "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ", |
| 4873 | VendorSpecificTag(kBPVendorAMD)); |
| 4874 | } |
| 4875 | } |
| 4876 | |
Rodrigo Locatti | 65b3383 | 2022-03-15 17:57:30 -0300 | [diff] [blame] | 4877 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4878 | bool has_separate_sampler = false; |
Rodrigo Locatti | 12f6ffc | 2022-03-15 18:29:11 -0300 | [diff] [blame] | 4879 | size_t fast_space_usage = 0; |
Rodrigo Locatti | 65b3383 | 2022-03-15 17:57:30 -0300 | [diff] [blame] | 4880 | |
| 4881 | for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; ++i) { |
| 4882 | auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]); |
| 4883 | for (const auto& binding : descriptor_set_layout_state->GetBindings()) { |
| 4884 | if (binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) { |
| 4885 | has_separate_sampler = true; |
| 4886 | } |
Rodrigo Locatti | 12f6ffc | 2022-03-15 18:29:11 -0300 | [diff] [blame] | 4887 | |
| 4888 | if ((descriptor_set_layout_state->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) == 0U) { |
| 4889 | size_t descriptor_type_size = 0; |
| 4890 | |
| 4891 | switch (binding.descriptorType) { |
| 4892 | case VK_DESCRIPTOR_TYPE_SAMPLER: |
| 4893 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 4894 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 4895 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 4896 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 4897 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 4898 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 4899 | descriptor_type_size = 4; |
| 4900 | break; |
| 4901 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 4902 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 4903 | case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: |
| 4904 | case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV: |
| 4905 | descriptor_type_size = 8; |
| 4906 | break; |
| 4907 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 4908 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 4909 | case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE: |
| 4910 | descriptor_type_size = 16; |
| 4911 | break; |
| 4912 | case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: |
| 4913 | descriptor_type_size = 1; |
| 4914 | default: |
| 4915 | // Unknown type. |
| 4916 | break; |
| 4917 | } |
| 4918 | |
| 4919 | size_t descriptor_size = descriptor_type_size * binding.descriptorCount; |
| 4920 | fast_space_usage += descriptor_size; |
| 4921 | } |
Rodrigo Locatti | 65b3383 | 2022-03-15 17:57:30 -0300 | [diff] [blame] | 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | if (has_separate_sampler) { |
| 4926 | skip |= LogPerformanceWarning( |
| 4927 | device, kVUID_BestPractices_CreatePipelineLayout_SeparateSampler, |
| 4928 | "%s Consider using combined image samplers instead of separate samplers for marginally better performance.", |
| 4929 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 4930 | } |
Rodrigo Locatti | 12f6ffc | 2022-03-15 18:29:11 -0300 | [diff] [blame] | 4931 | |
| 4932 | if (fast_space_usage > kPipelineLayoutFastDescriptorSpaceNVIDIA) { |
| 4933 | skip |= LogPerformanceWarning( |
| 4934 | device, kVUID_BestPractices_CreatePipelinesLayout_LargePipelineLayout, |
| 4935 | "%s Pipeline layout size is too large, prefer using pipeline-specific descriptor set layouts. " |
| 4936 | "Aim for consuming less than %" PRIu32 " bytes to allow fast reads for all non-bindless descriptors. " |
| 4937 | "Samplers, textures, texel buffers, and combined image samplers consume 4 bytes each. " |
| 4938 | "Uniform buffers and acceleration structures consume 8 bytes. " |
| 4939 | "Storage buffers consume 16 bytes. " |
| 4940 | "Push constants do not consume space.", |
| 4941 | VendorSpecificTag(kBPVendorNVIDIA), kPipelineLayoutFastDescriptorSpaceNVIDIA); |
| 4942 | } |
Rodrigo Locatti | 65b3383 | 2022-03-15 17:57:30 -0300 | [diff] [blame] | 4943 | } |
| 4944 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4945 | return skip; |
| 4946 | } |
| 4947 | |
| 4948 | bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 4949 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 4950 | const VkImageCopy* pRegions) const { |
| 4951 | bool skip = false; |
| 4952 | std::stringstream src_image_hex; |
| 4953 | std::stringstream dst_image_hex; |
| 4954 | src_image_hex << "0x" << std::hex << HandleToUint64(srcImage); |
| 4955 | dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage); |
| 4956 | |
| 4957 | if (VendorCheckEnabled(kBPVendorAMD)) { |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 4958 | auto src_state = Get<IMAGE_STATE>(srcImage); |
| 4959 | auto dst_state = Get<IMAGE_STATE>(dstImage); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4960 | |
| 4961 | if (src_state && dst_state) { |
| 4962 | VkImageTiling src_Tiling = src_state->createInfo.tiling; |
| 4963 | VkImageTiling dst_Tiling = dst_state->createInfo.tiling; |
| 4964 | if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) { |
| 4965 | skip |= |
| 4966 | LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy, |
| 4967 | "%s Performance warning: image %s and image %s have differing tilings. Use buffer to " |
| 4968 | "image (vkCmdCopyImageToBuffer) " |
| 4969 | "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image " |
| 4970 | "copies when converting between linear and optimal images", |
| 4971 | VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str()); |
| 4972 | } |
| 4973 | } |
| 4974 | } |
| 4975 | |
| 4976 | return skip; |
| 4977 | } |
| 4978 | |
| 4979 | bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, |
| 4980 | VkPipeline pipeline) const { |
| 4981 | bool skip = false; |
| 4982 | |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 4983 | auto cb = Get<bp_state::CommandBuffer>(commandBuffer); |
| 4984 | |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 4985 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 4986 | if (IsPipelineUsedInFrame(pipeline)) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4987 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 4988 | "%s %s Performance warning: Pipeline %s was bound twice in the frame. " |
| 4989 | "Keep pipeline state changes to a minimum, for example, by sorting draw calls by pipeline.", |
| 4990 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA), |
| 4991 | report_data->FormatHandle(pipeline).c_str()); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 4992 | } |
| 4993 | } |
Rodrigo Locatti | a5eaf6e | 2022-04-01 18:05:23 -0300 | [diff] [blame] | 4994 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 4995 | const auto& tgm = cb->nv.tess_geometry_mesh; |
| 4996 | if (tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA && !tgm.threshold_signaled) { |
| 4997 | LogPerformanceWarning(commandBuffer, kVUID_BestPractices_BindPipeline_SwitchTessGeometryMesh, |
| 4998 | "%s Avoid switching between pipelines with and without tessellation, geometry, task, " |
| 4999 | "and/or mesh shaders. Group draw calls using these shader stages together.", |
| 5000 | VendorSpecificTag(kBPVendorNVIDIA)); |
| 5001 | // Do not set 'skip' so the number of switches gets properly counted after the message. |
| 5002 | } |
| 5003 | } |
| 5004 | |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5005 | return skip; |
| 5006 | } |
| 5007 | |
| 5008 | void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, |
| 5009 | VkFence fence, VkResult result) { |
| 5010 | // AMD best practice |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5011 | num_queue_submissions_ += submitCount; |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const { |
| 5015 | bool skip = false; |
| 5016 | |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5017 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5018 | auto num = num_queue_submissions_.load(); |
| 5019 | if (num > kNumberOfSubmissionWarningLimitAMD) { |
| 5020 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5021 | "%s %s Performance warning: command buffers submitted %" PRId32 |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5022 | " times this frame. Submitting command buffers has a CPU " |
| 5023 | "and GPU overhead. Submit fewer times to incur less overhead.", |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5024 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA), num); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | return skip; |
| 5029 | } |
| 5030 | |
| 5031 | void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 5032 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 5033 | uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, |
| 5034 | uint32_t bufferMemoryBarrierCount, |
| 5035 | const VkBufferMemoryBarrier* pBufferMemoryBarriers, |
| 5036 | uint32_t imageMemoryBarrierCount, |
| 5037 | const VkImageMemoryBarrier* pImageMemoryBarriers) { |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 5038 | ValidationStateTracker::PostCallRecordCmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, |
| 5039 | memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, |
| 5040 | pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers); |
| 5041 | |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5042 | num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount); |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 5043 | |
| 5044 | for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) { |
| 5045 | RecordCmdPipelineBarrierImageBarrier(commandBuffer, pImageMemoryBarriers[i]); |
| 5046 | } |
| 5047 | } |
| 5048 | |
| 5049 | void BestPractices::PostCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) { |
| 5050 | ValidationStateTracker::PostCallRecordCmdPipelineBarrier2(commandBuffer, pDependencyInfo); |
| 5051 | |
| 5052 | for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) { |
| 5053 | RecordCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]); |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | void BestPractices::PostCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) { |
| 5058 | ValidationStateTracker::PostCallRecordCmdPipelineBarrier2KHR(commandBuffer, pDependencyInfo); |
| 5059 | |
| 5060 | for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) { |
| 5061 | RecordCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]); |
| 5062 | } |
| 5063 | } |
| 5064 | |
| 5065 | template <typename ImageMemoryBarrier> |
| 5066 | void BestPractices::RecordCmdPipelineBarrierImageBarrier(VkCommandBuffer commandBuffer, const ImageMemoryBarrier& barrier) { |
| 5067 | auto cb = Get<bp_state::CommandBuffer>(commandBuffer); |
| 5068 | assert(cb); |
| 5069 | |
paul-lunarg | 8696c6b | 2022-09-09 16:55:10 +0200 | [diff] [blame] | 5070 | // Is a queue ownership acquisition barrier |
| 5071 | if (barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex && |
| 5072 | barrier.dstQueueFamilyIndex == cb->command_pool->queueFamilyIndex) { |
| 5073 | auto image = Get<bp_state::Image>(barrier.image); |
| 5074 | auto subresource_range = barrier.subresourceRange; |
paul-lunarg | d89a81c | 2022-09-12 15:11:39 +0200 | [diff] [blame^] | 5075 | cb->queue_submit_functions.push_back([image, subresource_range](const ValidationStateTracker& vst, const QUEUE_STATE& qs, |
| 5076 | const CMD_BUFFER_STATE& cbs) -> bool { |
paul-lunarg | 8696c6b | 2022-09-09 16:55:10 +0200 | [diff] [blame] | 5077 | ForEachSubresource(*image, subresource_range, [&](uint32_t layer, uint32_t level) { |
| 5078 | // Update queue family index without changing usage, signifying a correct queue family transfer |
| 5079 | image->UpdateUsage(layer, level, image->GetUsage(layer, level), qs.queueFamilyIndex); |
| 5080 | }); |
| 5081 | return false; |
| 5082 | }); |
| 5083 | } |
| 5084 | |
Rodrigo Locatti | 0b16cda | 2022-04-01 17:59:53 -0300 | [diff] [blame] | 5085 | if (VendorCheckEnabled(kBPVendorNVIDIA)) { |
| 5086 | RecordResetZcullDirection(*cb, barrier.image, barrier.subresourceRange); |
| 5087 | } |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo, |
| 5091 | const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const { |
| 5092 | bool skip = false; |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5093 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5094 | if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5095 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5096 | "%s %s Performance warning: High number of vkSemaphore objects created. " |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5097 | "Minimize the amount of queue synchronization that is used. " |
| 5098 | "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.", |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5099 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA)); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | return skip; |
| 5104 | } |
| 5105 | |
| 5106 | bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo, |
| 5107 | const VkAllocationCallbacks* pAllocator, VkFence* pFence) const { |
| 5108 | bool skip = false; |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5109 | if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5110 | if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) { |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5111 | skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences, |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5112 | "%s %s Performance warning: High number of VkFence objects created." |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5113 | "Minimize the amount of CPU-GPU synchronization that is used. " |
Rodrigo Locatti | 494e448 | 2022-03-30 16:37:40 -0300 | [diff] [blame] | 5114 | "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.", |
| 5115 | VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA)); |
Nadav Geva | f080844 | 2021-05-21 13:51:25 -0400 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | return skip; |
| 5120 | } |
| 5121 | |
Sam Walls | 8e77e4f | 2020-03-16 20:47:40 +0000 | [diff] [blame] | 5122 | void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); } |
| 5123 | |
| 5124 | bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) { |
| 5125 | // look for a cache hit |
| 5126 | auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; }); |
| 5127 | if (hit != _entries.end()) { |
| 5128 | // mark the cache hit as being most recently used |
| 5129 | hit->age = iteration++; |
| 5130 | return true; |
| 5131 | } |
| 5132 | |
| 5133 | // if there's no cache hit, we need to model the entry being inserted into the cache |
| 5134 | CacheEntry new_entry = {value, iteration}; |
| 5135 | if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) { |
| 5136 | // if there is still space left in the cache, use the next available slot |
| 5137 | *(_entries.begin() + iteration) = new_entry; |
| 5138 | } else { |
| 5139 | // otherwise replace the least recently used cache entry |
| 5140 | auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; }); |
| 5141 | *lru = new_entry; |
| 5142 | } |
| 5143 | iteration++; |
| 5144 | return false; |
| 5145 | } |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5146 | |
| 5147 | bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, |
| 5148 | VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const { |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 5149 | auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5150 | bool skip = false; |
| 5151 | if (swapchain_data && swapchain_data->images.size() == 0) { |
| 5152 | skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound, |
| 5153 | "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call " |
| 5154 | "vkGetSwapchainImagesKHR after swapchain creation."); |
| 5155 | } |
| 5156 | return skip; |
| 5157 | } |
| 5158 | |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 5159 | void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) { |
| 5160 | if (no_pointer) { |
| 5161 | if (UNCALLED == call_state) { |
| 5162 | call_state = QUERY_COUNT; |
| 5163 | } |
| 5164 | } else { // Save queue family properties |
| 5165 | call_state = QUERY_DETAILS; |
| 5166 | } |
| 5167 | } |
| 5168 | |
Nathaniel Cesario | f121d12 | 2020-10-08 13:09:46 -0600 | [diff] [blame] | 5169 | void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, |
| 5170 | uint32_t* pQueueFamilyPropertyCount, |
| 5171 | VkQueueFamilyProperties* pQueueFamilyProperties) { |
| 5172 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, |
| 5173 | pQueueFamilyProperties); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5174 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5175 | if (bp_pd_state) { |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 5176 | CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState, |
| 5177 | nullptr == pQueueFamilyProperties); |
| 5178 | } |
| 5179 | } |
| 5180 | |
| 5181 | void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice, |
| 5182 | uint32_t* pQueueFamilyPropertyCount, |
| 5183 | VkQueueFamilyProperties2* pQueueFamilyProperties) { |
| 5184 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount, |
| 5185 | pQueueFamilyProperties); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5186 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 5187 | if (bp_pd_state) { |
| 5188 | CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State, |
| 5189 | nullptr == pQueueFamilyProperties); |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice, |
| 5194 | uint32_t* pQueueFamilyPropertyCount, |
| 5195 | VkQueueFamilyProperties2* pQueueFamilyProperties) { |
| 5196 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, |
| 5197 | pQueueFamilyProperties); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5198 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 56a9665 | 2020-12-30 13:23:42 -0700 | [diff] [blame] | 5199 | if (bp_pd_state) { |
| 5200 | CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState, |
| 5201 | nullptr == pQueueFamilyProperties); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5202 | } |
| 5203 | } |
| 5204 | |
Nathaniel Cesario | f121d12 | 2020-10-08 13:09:46 -0600 | [diff] [blame] | 5205 | void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) { |
| 5206 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5207 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5208 | if (bp_pd_state) { |
| 5209 | bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS; |
| 5210 | } |
| 5211 | } |
| 5212 | |
Nathaniel Cesario | f121d12 | 2020-10-08 13:09:46 -0600 | [diff] [blame] | 5213 | void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice, |
| 5214 | VkPhysicalDeviceFeatures2* pFeatures) { |
| 5215 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5216 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5217 | if (bp_pd_state) { |
| 5218 | bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS; |
| 5219 | } |
| 5220 | } |
| 5221 | |
Nathaniel Cesario | f121d12 | 2020-10-08 13:09:46 -0600 | [diff] [blame] | 5222 | void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice, |
| 5223 | VkPhysicalDeviceFeatures2* pFeatures) { |
| 5224 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures); |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5225 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5226 | if (bp_pd_state) { |
| 5227 | bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS; |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice, |
| 5232 | VkSurfaceKHR surface, |
| 5233 | VkSurfaceCapabilitiesKHR* pSurfaceCapabilities, |
| 5234 | VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5235 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5236 | if (bp_pd_state) { |
| 5237 | bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS; |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR( |
| 5242 | VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 5243 | VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5244 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5245 | if (bp_pd_state) { |
| 5246 | bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS; |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice, |
| 5251 | VkSurfaceKHR surface, |
| 5252 | VkSurfaceCapabilities2EXT* pSurfaceCapabilities, |
| 5253 | VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5254 | auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5255 | if (bp_pd_state) { |
| 5256 | bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS; |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice, |
| 5261 | VkSurfaceKHR surface, uint32_t* pPresentModeCount, |
| 5262 | VkPresentModeKHR* pPresentModes, VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5263 | auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5264 | if (bp_pd_data) { |
| 5265 | auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState; |
| 5266 | |
| 5267 | if (*pPresentModeCount) { |
| 5268 | if (call_state < QUERY_COUNT) { |
| 5269 | call_state = QUERY_COUNT; |
| 5270 | } |
| 5271 | } |
| 5272 | if (pPresentModes) { |
| 5273 | if (call_state < QUERY_DETAILS) { |
| 5274 | call_state = QUERY_DETAILS; |
| 5275 | } |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, |
| 5281 | uint32_t* pSurfaceFormatCount, |
| 5282 | VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5283 | auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5284 | if (bp_pd_data) { |
| 5285 | auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState; |
| 5286 | |
| 5287 | if (*pSurfaceFormatCount) { |
| 5288 | if (call_state < QUERY_COUNT) { |
| 5289 | call_state = QUERY_COUNT; |
| 5290 | } |
Jeremy Gebben | c7a834a | 2021-09-08 18:39:30 -0600 | [diff] [blame] | 5291 | bp_pd_data->surface_formats_count = *pSurfaceFormatCount; |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5292 | } |
| 5293 | if (pSurfaceFormats) { |
| 5294 | if (call_state < QUERY_DETAILS) { |
| 5295 | call_state = QUERY_DETAILS; |
| 5296 | } |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice, |
| 5302 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 5303 | uint32_t* pSurfaceFormatCount, |
| 5304 | VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5305 | auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5306 | if (bp_pd_data) { |
| 5307 | if (*pSurfaceFormatCount) { |
| 5308 | if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) { |
| 5309 | bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT; |
| 5310 | } |
Jeremy Gebben | c7a834a | 2021-09-08 18:39:30 -0600 | [diff] [blame] | 5311 | bp_pd_data->surface_formats_count = *pSurfaceFormatCount; |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5312 | } |
| 5313 | if (pSurfaceFormats) { |
| 5314 | if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) { |
| 5315 | bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS; |
| 5316 | } |
| 5317 | } |
| 5318 | } |
| 5319 | } |
| 5320 | |
| 5321 | void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice, |
| 5322 | uint32_t* pPropertyCount, |
| 5323 | VkDisplayPlanePropertiesKHR* pProperties, |
| 5324 | VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5325 | auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice); |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5326 | if (bp_pd_data) { |
| 5327 | if (*pPropertyCount) { |
| 5328 | if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) { |
| 5329 | bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT; |
| 5330 | } |
| 5331 | } |
| 5332 | if (pProperties) { |
| 5333 | if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) { |
| 5334 | bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS; |
| 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | } |
| 5339 | |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5340 | void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, |
| 5341 | uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages, |
| 5342 | VkResult result) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5343 | auto swapchain_state = Get<bp_state::Swapchain>(swapchain); |
Nathaniel Cesario | 39152e6 | 2021-07-02 13:04:16 -0600 | [diff] [blame] | 5344 | if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) { |
| 5345 | if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) { |
| 5346 | swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS; |
Nathaniel Cesario | 24184fe | 2020-10-06 12:46:12 -0600 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5351 | void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) { |
| 5352 | ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence); |
| 5353 | |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 5354 | auto queue_state = Get<QUEUE_STATE>(queue); |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5355 | for (uint32_t submit = 0; submit < submitCount; submit++) { |
Hans-Kristian Arntzen | 69ace7d | 2021-04-28 14:17:19 +0200 | [diff] [blame] | 5356 | const auto& submit_info = pSubmits[submit]; |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5357 | for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) { |
Jeremy Gebben | 20da7a1 | 2022-02-25 14:07:46 -0700 | [diff] [blame] | 5358 | auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]); |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5359 | for (auto &func : cb->queue_submit_functions) { |
Jeremy Gebben | e5361dd | 2021-11-18 14:23:56 -0700 | [diff] [blame] | 5360 | func(*this, *queue_state, *cb); |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5361 | } |
Rodrigo Locatti | c789fe8 | 2022-07-06 17:42:19 -0300 | [diff] [blame] | 5362 | cb->num_submits++; |
Hans-Kristian Arntzen | 66f4b52 | 2021-03-22 11:35:58 +0100 | [diff] [blame] | 5363 | } |
| 5364 | } |
| 5365 | } |