blob: 639ce9b59a5dc0f37ddaf4332f2851768aa187bf [file] [log] [blame]
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
Nadav Geva41c12a22021-05-21 13:14:05 -04004 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Camdeneaa86ea2019-07-26 11:00:09 -06005 *
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 Geva41c12a22021-05-21 13:14:05 -040019 * Author: Nadav Geva <nadav.geva@amd.com>
Camdeneaa86ea2019-07-26 11:00:09 -060020 */
21
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070022#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060023#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060024#include "best_practices_error_enums.h"
Sam Wallsd7ab6db2020-06-19 20:41:54 +010025#include "shader_validation.h"
Jeremy Gebbena3705f42021-01-19 16:47:43 -070026#include "sync_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060027#include "cmd_buffer_state.h"
28#include "device_state.h"
29#include "render_pass_state.h"
Camden5b184be2019-08-13 07:50:19 -060030
31#include <string>
Sam Walls8e77e4f2020-03-16 20:47:40 +000032#include <bitset>
Sam Wallsd7ab6db2020-06-19 20:41:54 +010033#include <memory>
Camden5b184be2019-08-13 07:50:19 -060034
Attilio Provenzano19d6a982020-02-27 12:41:41 +000035struct VendorSpecificInfo {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060036 EnableFlags vendor_id;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000037 std::string name;
38};
39
LawG475463092022-02-22 10:45:54 +000040const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {{kBPVendorArm, {vendor_specific_arm, "Arm"}},
41 {kBPVendorAMD, {vendor_specific_amd, "AMD"}},
Rodrigo Locattic779cb32022-02-25 19:26:31 -030042 {kBPVendorIMG, {vendor_specific_img, "IMG"}},
43 {kBPVendorNVIDIA, {vendor_specific_nvidia, "NVIDIA"}}};
Attilio Provenzano19d6a982020-02-27 12:41:41 +000044
Hannes Harnisch607d1d92021-07-10 18:44:56 +020045const 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
53const 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 Locattie4c08a02022-04-04 18:12:18 -030061static 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 Gebben20da7a12022-02-25 14:07:46 -070068ReadLockGuard 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
76WriteLockGuard 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 Gebben7c2cd8b2021-08-11 15:40:38 -060084std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb,
85 const VkCommandBufferAllocateInfo* pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060086 const COMMAND_POOL_STATE* pool) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -070087 return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<bp_state::CommandBuffer>(this, cb, pCreateInfo, pool));
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060088}
89
Jeremy Gebben20da7a12022-02-25 14:07:46 -070090bp_state::CommandBuffer::CommandBuffer(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo,
91 const COMMAND_POOL_STATE* pool)
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060092 : CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {}
93
Attilio Provenzano19d6a982020-02-27 12:41:41 +000094bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070095 for (const auto& vendor : kVendorInfo) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060096 if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000097 return true;
98 }
99 }
100 return false;
101}
102
103const char* VendorSpecificTag(BPVendorFlags vendors) {
104 // Cache built vendor tags in a map
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700105 static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
Attilio Provenzano19d6a982020-02-27 12:41:41 +0000106
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 Cesarioce9b4812020-12-17 08:55:28 -0700114 for (const auto& vendor : kVendorInfo) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +0000115 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 Lobodzinski6167e102020-02-24 17:03:55 -0700132const 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
149bool 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 Lobodzinski6a149702020-05-14 12:21:34 -0600155 if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
Tony-LunarGc30b59f2022-02-15 11:02:36 -0700156 ((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 Lobodzinski6167e102020-02-24 17:03:55 -0700158 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 Lobodzinski6a149702020-05-14 12:21:34 -0600161 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700162 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 Harnisch607d1d92021-07-10 18:44:56 +0200177bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
178{
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700179 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 Harnisch607d1d92021-07-10 18:44:56 +0200183 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-samsungef15e482022-01-26 11:32:49 -0800186
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700187 if (special_uses.find("cadsupport") != std::string::npos) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800188 skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
189 "specialized functionality used by CAD/CAM applications");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700190 }
191 if (special_uses.find("d3demulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200192 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 Lobodzinski057724a2020-11-09 17:13:18 -0700194 }
195 if (special_uses.find("devtools") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200196 skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
197 "developer tools such as capture-replay libraries");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700198 }
199 if (special_uses.find("debugging") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200200 skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
201 "use by applications when debugging");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700202 }
203 if (special_uses.find("glemulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200204 skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700205 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200206 "specific to those APIs");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700207 }
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700208 }
209 return skip;
210}
211
Camden5b184be2019-08-13 07:50:19 -0600212bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500213 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600214 bool skip = false;
215
216 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
217 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800218 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700219 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
220 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600221 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600222 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 Lobodzinski6167e102020-02-24 17:03:55 -0700225 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200226 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600227 }
228
229 return skip;
230}
231
Camden5b184be2019-08-13 07:50:19 -0600232bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500233 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600234 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 Bolz46c0ea02019-10-09 13:06:29 -0500239 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600240
241 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600242 if (api_version > device_api_version) {
243 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600244 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600245
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700246 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());
Camden5b184be2019-08-13 07:50:19 -0600249 }
250
Rodrigo Locattic2d5cf42022-03-01 18:05:26 -0300251 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
Camden5b184be2019-08-13 07:50:19 -0600265 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300266 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
267
268 if (white_list(extension_name, kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800269 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700270 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300271 extension_name);
Camden5b184be2019-08-13 07:50:19 -0600272 }
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300273
274 skip |= ValidateDeprecatedExtensions("CreateDevice", extension_name, api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700275 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300276 skip |= ValidateSpecialUseExtensions("CreateDevice", extension_name, kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600277 }
278
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700279 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600280 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700281 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
282 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600283 }
284
LawG43f848c72022-02-23 09:35:21 +0000285 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorIMG)) &&
286 (pCreateInfo->pEnabledFeatures != nullptr) && (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
Szilard Papp7d2c7952020-06-22 14:38:13 +0100287 skip |= LogPerformanceWarning(
288 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
LawG4015be1c2022-03-01 10:37:52 +0000289 "%s %s %s: vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100290 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
291 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
292 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
LawG43f848c72022-02-23 09:35:21 +0000293 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorIMG));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100294 }
295
Rodrigo Locatti8dde2ff2022-03-01 18:06:08 -0300296 const bool enabled_pageable_device_local_memory = IsExtEnabled(device_extensions.vk_ext_pageable_device_local_memory);
297 if (VendorCheckEnabled(kBPVendorNVIDIA) && !enabled_pageable_device_local_memory &&
298 std::find(extensions.begin(), extensions.end(), VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME) != extensions.end()) {
299 skip |= LogPerformanceWarning(
300 device, kVUID_BestPractices_CreateDevice_PageableDeviceLocalMemory,
301 "%s vkCreateDevice() called without pageable device local memory. "
302 "Use pageableDeviceLocalMemory from VK_EXT_pageable_device_local_memory when it is available.",
303 VendorSpecificTag(kBPVendorNVIDIA));
304 }
305
Camden5b184be2019-08-13 07:50:19 -0600306 return skip;
307}
308
309bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500310 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600311 bool skip = false;
312
313 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700314 std::stringstream buffer_hex;
315 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600316
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700317 skip |= LogWarning(
318 device, kVUID_BestPractices_SharingModeExclusive,
319 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
320 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700321 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600322 }
323
324 return skip;
325}
326
327bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500328 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600329 bool skip = false;
330
331 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700332 std::stringstream image_hex;
333 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600334
335 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700336 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
337 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
338 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700339 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600340 }
341
ziga-lunarg6df3d102022-03-18 17:02:14 +0100342 if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
343 skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags,
344 "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not "
345 "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the "
346 "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect.");
347 }
348
LawG4655f59c2022-02-23 13:55:55 +0000349 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000350 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
351 skip |= LogPerformanceWarning(
352 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
LawG4655f59c2022-02-23 13:55:55 +0000353 "%s %s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
Attilio Provenzano02859b22020-02-27 14:17:28 +0000354 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
355 "and do not need to be backed by physical storage. "
356 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
LawG4655f59c2022-02-23 13:55:55 +0000357 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG));
Attilio Provenzano02859b22020-02-27 14:17:28 +0000358 }
359 }
360
LawG4ba113892022-02-23 14:39:02 +0000361 if (VendorCheckEnabled(kBPVendorArm) && pCreateInfo->samples > kMaxEfficientSamplesArm) {
362 skip |= LogPerformanceWarning(
363 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
364 "%s vkCreateImage(): Trying to create an image with %u samples. "
365 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
366 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
367 }
368
369 if (VendorCheckEnabled(kBPVendorIMG) && pCreateInfo->samples > kMaxEfficientSamplesImg) {
370 skip |= LogPerformanceWarning(
371 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
372 "%s vkCreateImage(): Trying to create an image with %u samples. "
373 "The device may not have full support for true multisampling for images with more than %u samples. "
374 "XT devices support up to 8 samples, XE up to 4 samples.",
375 VendorSpecificTag(kBPVendorIMG), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesImg);
376 }
377
LawG4db16f802022-03-21 17:33:39 +0000378 if (VendorCheckEnabled(kBPVendorIMG) && (pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG ||
379 pCreateInfo->format == VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG ||
380 pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG ||
381 pCreateInfo->format == VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG ||
382 pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG ||
383 pCreateInfo->format == VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG ||
384 pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG ||
385 pCreateInfo->format == VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG)) {
386 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Texture_Format_PVRTC_Outdated,
387 "%s vkCreateImage(): Trying to create an image with a PVRTC format. Both PVRTC1 and PVRTC2 "
388 "are slower than standard image formats on PowerVR GPUs, prefer ETC, BC, ASTC, etc.",
389 VendorSpecificTag(kBPVendorIMG));
390 }
391
Nadav Gevaf0808442021-05-21 13:51:25 -0400392 if (VendorCheckEnabled(kBPVendorAMD)) {
393 std::stringstream image_hex;
394 image_hex << "0x" << std::hex << HandleToUint64(pImage);
395
396 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
397 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
398 skip |= LogPerformanceWarning(device,
399 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
400 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
401 "Using a SHARING_MODE_CONCURRENT "
402 "is not recommended with color and depth targets",
403 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
404 }
405
406 if ((pCreateInfo->usage &
407 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
408 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
409 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
410 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
411 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
412 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
413 }
414
415 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
416 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
417 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
418 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
419 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
420 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
421 }
422 }
423
Rodrigo Locatti5466f9d2022-03-09 18:20:38 -0300424 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
425 std::stringstream image_hex;
426 image_hex << "0x" << std::hex << HandleToUint64(pImage);
427
428 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) {
429 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateImage_TilingLinear,
430 "%s Performance warning: image (%s) is created with tiling VK_IMAGE_TILING_LINEAR. "
431 "Use VK_IMAGE_TILING_OPTIMAL instead.",
432 VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str());
433 }
Rodrigo Locatti3290c2b2022-03-09 18:25:56 -0300434
435 if (pCreateInfo->format == VK_FORMAT_D32_SFLOAT || pCreateInfo->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
436 skip |= LogPerformanceWarning(
437 device, kVUID_BestPractices_CreateImage_Depth32Format,
438 "%s Performance warning: image (%s) is created with a 32-bit depth format. Use VK_FORMAT_D24_UNORM_S8_UINT or "
439 "VK_FORMAT_D16_UNORM instead, unless the extra precision is needed.",
440 VendorSpecificTag(kBPVendorNVIDIA), image_hex.str().c_str());
441 }
Rodrigo Locatti5466f9d2022-03-09 18:20:38 -0300442 }
443
Camden5b184be2019-08-13 07:50:19 -0600444 return skip;
445}
446
447bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500448 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600449 bool skip = false;
450
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600451 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600452 if (bp_pd_state) {
453 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
454 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
455 "vkCreateSwapchainKHR() called before getting surface capabilities from "
456 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
457 }
Camden83a9c372019-08-14 11:41:38 -0600458
Shannon McPherson73e58c82021-03-05 17:14:26 -0700459 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
460 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600461 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
462 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
463 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
464 }
Camden83a9c372019-08-14 11:41:38 -0600465
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600466 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
467 skip |= LogWarning(
468 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
469 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
470 }
Camden83a9c372019-08-14 11:41:38 -0600471 }
472
Camden5b184be2019-08-13 07:50:19 -0600473 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700474 skip |=
475 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600476 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700477 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
478 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600479 }
480
ziga-lunarg79beba62022-03-30 01:17:30 +0200481 const auto present_mode = pCreateInfo->presentMode;
482 if (((present_mode == VK_PRESENT_MODE_MAILBOX_KHR) || (present_mode == VK_PRESENT_MODE_FIFO_KHR)) &&
483 (pCreateInfo->minImageCount == 2)) {
Szilard Papp48a6da32020-06-10 14:41:59 +0100484 skip |= LogPerformanceWarning(
485 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
486 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
487 ", which means double buffering is going "
488 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
489 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
490 "3 to use triple buffering to maximize performance in such cases.",
491 pCreateInfo->minImageCount);
492 }
493
Szilard Pappd5f0f812020-06-22 09:01:29 +0100494 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
495 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
496 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
497 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
498 "Presentation modes which are not FIFO will present the latest available frame and discard other "
499 "frame(s) if any.",
500 VendorSpecificTag(kBPVendorArm));
501 }
502
Camden5b184be2019-08-13 07:50:19 -0600503 return skip;
504}
505
506bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
507 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500508 const VkAllocationCallbacks* pAllocator,
509 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600510 bool skip = false;
511
512 for (uint32_t i = 0; i < swapchainCount; i++) {
513 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700514 skip |= LogWarning(
515 device, kVUID_BestPractices_SharingModeExclusive,
516 "Warning: A shared swapchain (index %" PRIu32
517 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
518 "queues (queueFamilyIndexCount of %" PRIu32 ").",
519 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600520 }
521 }
522
523 return skip;
524}
525
526bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500527 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600528 bool skip = false;
529
530 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
531 VkFormat format = pCreateInfo->pAttachments[i].format;
532 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
533 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
534 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700535 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
536 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
537 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
538 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
539 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600540 }
541 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700542 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
543 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
544 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
545 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
546 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600547 }
548 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000549
550 const auto& attachment = pCreateInfo->pAttachments[i];
551 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
552 bool access_requires_memory =
553 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
554
555 if (FormatHasStencil(format)) {
556 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
557 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
558 }
559
560 if (access_requires_memory) {
561 skip |= LogPerformanceWarning(
562 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
563 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
564 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
565 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
566 i, static_cast<uint32_t>(attachment.samples));
567 }
568 }
Camden5b184be2019-08-13 07:50:19 -0600569 }
570
571 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
572 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
573 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
574 }
575
576 return skip;
577}
578
Tony-LunarG767180f2020-04-23 14:03:59 -0600579bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
580 const VkImageView* image_views) const {
581 bool skip = false;
582
583 // Check for non-transient attachments that should be transient and vice versa
584 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200585 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600586 bool attachment_should_be_transient =
587 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
588
589 if (FormatHasStencil(attachment.format)) {
590 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
591 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
592 }
593
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600594 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600595 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600596 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600597
598 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
599
600 // The check for an image that should not be transient applies to all GPUs
601 if (!attachment_should_be_transient && image_is_transient) {
602 skip |= LogPerformanceWarning(
603 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
604 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
605 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
606 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
607 i);
608 }
609
610 bool supports_lazy = false;
611 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
612 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
613 supports_lazy = true;
614 }
615 }
616
617 // The check for an image that should be transient only applies to GPUs supporting
618 // lazily allocated memory
619 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
620 skip |= LogPerformanceWarning(
621 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
622 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
623 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
624 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
625 i);
626 }
627 }
628 }
629 return skip;
630}
631
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000632bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
633 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
634 bool skip = false;
635
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600636 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800637 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600638 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000639 }
640
641 return skip;
642}
643
Sam Wallse746d522020-03-16 21:20:23 +0000644bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
645 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
646 bool skip = false;
647 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
648
649 if (!skip) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700650 const auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000651 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
652 // this warning is specific to Arm
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700653 if (VendorCheckEnabled(kBPVendorArm) && pool_state && (pool_state->freed_count > 0)) {
Sam Wallse746d522020-03-16 21:20:23 +0000654 skip |= LogPerformanceWarning(
655 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
656 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
657 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
658 VendorSpecificTag(kBPVendorArm));
659 }
ziga-lunarg5a76c442022-04-17 18:04:08 +0200660
661 if (IsExtEnabled(device_extensions.vk_khr_maintenance1)) {
662 // Track number of descriptorSets allowable in this pool
663 if (pool_state->GetAvailableSets() < pAllocateInfo->descriptorSetCount) {
664 skip |= LogWarning(pool_state->Handle(), kVUID_BestPractices_EmptyDescriptorPool,
665 "vkAllocateDescriptorSets(): Unable to allocate %" PRIu32 " descriptorSets from %s"
666 ". This pool only has %" PRIu32 " descriptorSets remaining.",
667 pAllocateInfo->descriptorSetCount, report_data->FormatHandle(pool_state->Handle()).c_str(),
668 pool_state->GetAvailableSets());
669 }
670 }
Sam Wallse746d522020-03-16 21:20:23 +0000671 }
672
673 return skip;
674}
675
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600676void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
677 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000678 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700679 auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
680 if (pool_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000681 // we record successful allocations by subtracting the allocation count from the last recorded free count
682 const auto alloc_count = pAllocateInfo->descriptorSetCount;
683 // clamp the unsigned subtraction to the range [0, last_free_count]
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700684 if (pool_state->freed_count > alloc_count) {
685 pool_state->freed_count -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700686 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700687 pool_state->freed_count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700688 }
Sam Wallse746d522020-03-16 21:20:23 +0000689 }
690 }
691}
692
693void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
694 const VkDescriptorSet* pDescriptorSets, VkResult result) {
695 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
696 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700697 auto pool_state = Get<bp_state::DescriptorPool>(descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000698 // we want to track frees because we're interested in suggesting re-use
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700699 if (pool_state) {
700 pool_state->freed_count += descriptorSetCount;
Sam Wallse746d522020-03-16 21:20:23 +0000701 }
702 }
703}
704
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300705void BestPractices::PreCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
706 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) {
707 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
708 WriteLockGuard guard{memory_free_events_lock_};
709
710 // Release old allocations to avoid overpopulating the container
711 const auto now = std::chrono::high_resolution_clock::now();
712 const auto last_old = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [now](const MemoryFreeEvent& event) {
713 return now - event.time > kAllocateMemoryReuseTimeThresholdNVIDIA;
714 });
715 memory_free_events_.erase(memory_free_events_.begin(), last_old.base());
716 }
717}
718
Camden5b184be2019-08-13 07:50:19 -0600719bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500720 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600721 bool skip = false;
722
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700723 if ((Count<DEVICE_MEMORY_STATE>() + 1) > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700724 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
725 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600726 }
727
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000728 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
729 skip |= LogPerformanceWarning(
730 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600731 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
732 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000733 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
734 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
735 }
736
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300737 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
738 if (!device_extensions.vk_ext_pageable_device_local_memory &&
739 !LvlFindInChain<VkMemoryPriorityAllocateInfoEXT>(pAllocateInfo->pNext)) {
740 skip |= LogPerformanceWarning(
741 device, kVUID_BestPractices_AllocateMemory_SetPriority,
742 "%s Use VkMemoryPriorityAllocateInfoEXT to provide the operating system information on the allocations that "
743 "should stay in video memory and which should be demoted first when video memory is limited. "
744 "The highest priority should be given to GPU-written resources like color attachments, depth attachments, "
745 "storage images, and buffers written from the GPU.",
746 VendorSpecificTag(kBPVendorNVIDIA));
747 }
748
749 {
750 // Size in bytes for an allocation to be considered "compatible"
751 static constexpr VkDeviceSize size_threshold = VkDeviceSize{1} << 20;
752
753 ReadLockGuard guard{memory_free_events_lock_};
754
755 const auto now = std::chrono::high_resolution_clock::now();
756 const VkDeviceSize alloc_size = pAllocateInfo->allocationSize;
757 const uint32_t memory_type_index = pAllocateInfo->memoryTypeIndex;
758 const auto latest_event = std::find_if(memory_free_events_.rbegin(), memory_free_events_.rend(), [&](const MemoryFreeEvent& event) {
759 return (memory_type_index == event.memory_type_index) && (alloc_size <= event.allocation_size) &&
760 (alloc_size - event.allocation_size <= size_threshold) && (now - event.time < kAllocateMemoryReuseTimeThresholdNVIDIA);
761 });
762
763 if (latest_event != memory_free_events_.rend()) {
764 const auto time_delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - latest_event->time);
765 if (time_delta < std::chrono::milliseconds{5}) {
766 skip |=
767 LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_ReuseAllocations,
768 "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation "
769 "has just been released, and it could have been reused in place of this allocation.",
770 VendorSpecificTag(kBPVendorNVIDIA));
771 } else {
772 const uint32_t seconds = static_cast<uint32_t>(time_delta.count() / 1000);
773 const uint32_t milliseconds = static_cast<uint32_t>(time_delta.count() % 1000);
774
775 skip |= LogPerformanceWarning(
776 device, kVUID_BestPractices_AllocateMemory_ReuseAllocations,
777 "%s Reuse memory allocations instead of releasing and reallocating. A memory allocation has been released "
778 "%" PRIu32 ".%03" PRIu32 " seconds ago, and it could have been reused in place of this allocation.",
779 VendorSpecificTag(kBPVendorNVIDIA), seconds, milliseconds);
780 }
781 }
782 }
Rodrigo Locattie4f8d522022-03-15 16:30:49 -0300783 }
784
Camden83a9c372019-08-14 11:41:38 -0600785 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
786
787 return skip;
788}
789
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600790void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
791 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
792 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700793 if (result != VK_SUCCESS) {
794 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
795 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800796 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700797 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600798 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700799 return;
800 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700801}
Camden Stocker9738af92019-10-16 13:54:03 -0700802
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600803void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
804 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700805 auto error = std::find(error_codes.begin(), error_codes.end(), result);
806 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000807 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
808 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
809
810 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
811 if (common_failure != common_failure_codes.end()) {
812 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
813 } else {
814 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
815 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700816 return;
817 }
818 auto success = std::find(success_codes.begin(), success_codes.end(), result);
819 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600820 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
821 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500822 }
823}
824
Rodrigo Locattid5b54f52022-03-16 19:12:45 -0300825void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
826 if (memory != VK_NULL_HANDLE && VendorCheckEnabled(kBPVendorNVIDIA)) {
827 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
828
829 // Exclude memory free events on dedicated allocations, or imported/exported allocations.
830 if (!mem_info->IsDedicatedBuffer() && !mem_info->IsDedicatedImage() && !mem_info->IsExport() && !mem_info->IsImport()) {
831 MemoryFreeEvent event;
832 event.time = std::chrono::high_resolution_clock::now();
833 event.memory_type_index = mem_info->alloc_info.memoryTypeIndex;
834 event.allocation_size = mem_info->alloc_info.allocationSize;
835
836 WriteLockGuard guard{memory_free_events_lock_};
837 memory_free_events_.push_back(event);
838 }
839 }
840
841 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
842}
843
Jeff Bolz5c801d12019-10-09 10:38:45 -0500844bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
845 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700846 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600847 bool skip = false;
848
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700849 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600850
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700851 for (const auto& item : mem_info->ObjectBindings()) {
852 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600853 LogObjectList objlist(device);
854 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600855 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600856 skip |= LogWarning(objlist, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600857 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600858 }
859
Camden5b184be2019-08-13 07:50:19 -0600860 return skip;
861}
862
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000863bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600864 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700865 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600866
sfricke-samsunge2441192019-11-06 14:07:57 -0800867 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700868 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
869 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
870 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600871 }
872
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700873 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000874
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300875 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000876 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
877 skip |= LogPerformanceWarning(
878 device, kVUID_BestPractices_SmallDedicatedAllocation,
879 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600880 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
881 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000882 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
883 }
884
Rodrigo Locatti66b23352022-03-15 17:28:32 -0300885 skip |= ValidateBindMemory(device, memory);
886
Camden Stockerb603cc82019-09-03 10:09:02 -0600887 return skip;
888}
889
890bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500891 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600892 bool skip = false;
893 const char* api_name = "BindBufferMemory()";
894
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000895 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600896
897 return skip;
898}
899
900bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500901 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600902 char api_name[64];
903 bool skip = false;
904
905 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +0200906 snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000907 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600908 }
909
910 return skip;
911}
Camden Stockerb603cc82019-09-03 10:09:02 -0600912
913bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500914 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600915 char api_name[64];
916 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600917
Camden Stocker8b798ab2019-09-03 10:33:28 -0600918 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +0200919 snprintf(api_name, sizeof(api_name), "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000920 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600921 }
922
923 return skip;
924}
925
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000926bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600927 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700928 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600929
sfricke-samsung71bc6572020-04-29 15:49:43 -0700930 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600931 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700932 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
933 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
934 api_name, report_data->FormatHandle(image).c_str());
935 }
936 } else {
937 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
938 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600939 }
940
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700941 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000942
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600943 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000944 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
945 skip |= LogPerformanceWarning(
946 device, kVUID_BestPractices_SmallDedicatedAllocation,
947 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600948 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
949 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000950 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
951 }
952
953 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
954 // make sure this type is actually used.
955 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
956 // (i.e.most tile - based renderers)
957 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
958 bool supports_lazy = false;
959 uint32_t suggested_type = 0;
960
961 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600962 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000963 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
964 supports_lazy = true;
965 suggested_type = i;
966 break;
967 }
968 }
969 }
970
971 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
972
973 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
974 skip |= LogPerformanceWarning(
975 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600976 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000977 "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600978 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600979 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000980 }
981 }
982
Rodrigo Locatti66b23352022-03-15 17:28:32 -0300983 skip |= ValidateBindMemory(device, memory);
984
Camden Stocker8b798ab2019-09-03 10:33:28 -0600985 return skip;
986}
987
988bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500989 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600990 bool skip = false;
991 const char* api_name = "vkBindImageMemory()";
992
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000993 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600994
995 return skip;
996}
997
998bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500999 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -06001000 char api_name[64];
1001 bool skip = false;
1002
1003 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +02001004 snprintf(api_name, sizeof(api_name), "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001005 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -06001006 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
1007 }
Camden Stocker8b798ab2019-09-03 10:33:28 -06001008 }
1009
1010 return skip;
1011}
1012
1013bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001014 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -06001015 char api_name[64];
1016 bool skip = false;
1017
1018 for (uint32_t i = 0; i < bindInfoCount; i++) {
Frédéric Wangafb89862022-06-21 16:15:29 +02001019 snprintf(api_name, sizeof(api_name), "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +00001020 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -06001021 }
1022
1023 return skip;
1024}
Camden83a9c372019-08-14 11:41:38 -06001025
Rodrigo Locatti66b23352022-03-15 17:28:32 -03001026void BestPractices::PreCallRecordSetDeviceMemoryPriorityEXT(VkDevice device, VkDeviceMemory memory, float priority) {
1027 auto mem_info = std::static_pointer_cast<bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory));
1028 mem_info->dynamic_priority.emplace(priority);
1029}
1030
Attilio Provenzano02859b22020-02-27 14:17:28 +00001031static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
1032 switch (format) {
1033 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
1034 case VK_FORMAT_R16_SFLOAT:
1035 case VK_FORMAT_R16G16_SFLOAT:
1036 case VK_FORMAT_R16G16B16_SFLOAT:
1037 case VK_FORMAT_R16G16B16A16_SFLOAT:
1038 case VK_FORMAT_R32_SFLOAT:
1039 case VK_FORMAT_R32G32_SFLOAT:
1040 case VK_FORMAT_R32G32B32_SFLOAT:
1041 case VK_FORMAT_R32G32B32A32_SFLOAT:
1042 return false;
1043
1044 default:
1045 return true;
1046 }
1047}
1048
1049bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
1050 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
1051 bool skip = false;
1052
1053 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001054 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001055
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001056 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
1057 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
1058 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +00001059 return skip;
1060 }
1061
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001062 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001063 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001064
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +02001065 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1066 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
1067
1068 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001069 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001070 uint32_t att = subpass.pColorAttachments[j].attachment;
1071
1072 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
1073 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
1074 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
1075 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
1076 "color attachment #%u makes use "
1077 "of a format which cannot be blended at full throughput when using MSAA.",
1078 VendorSpecificTag(kBPVendorArm), i, j);
1079 }
1080 }
1081 }
1082 }
1083
1084 return skip;
1085}
1086
Nadav Gevaf0808442021-05-21 13:51:25 -04001087void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1088 const VkComputePipelineCreateInfo* pCreateInfos,
1089 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1090 VkResult result, void* pipe_state) {
1091 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001092 pipeline_cache_ = pipelineCache;
Nadav Gevaf0808442021-05-21 13:51:25 -04001093}
1094
Camden5b184be2019-08-13 07:50:19 -06001095bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1096 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001097 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001098 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001099 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1100 pAllocator, pPipelines, cgpl_state_data);
ziga-lunarg08c81582022-03-08 17:33:45 +01001101 if (skip) {
1102 return skip;
1103 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001104 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001105
1106 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001107 skip |= LogPerformanceWarning(
1108 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1109 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
1110 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001111 }
1112
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001113 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001114 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001115
Tony-LunarGb6a2daf2022-07-29 11:30:55 -06001116 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV) && create_info.pVertexInputState) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001117 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001118 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001119 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
1120 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001121 count++;
1122 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001123 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -06001124 if (count > kMaxInstancedVertexBuffers) {
1125 skip |= LogPerformanceWarning(
1126 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
1127 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
1128 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
1129 count, kMaxInstancedVertexBuffers);
1130 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001131 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001132
Szilard Pappaaf2da32020-06-22 10:37:35 +01001133 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
1134 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001135 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
1136 VendorCheckEnabled(kBPVendorArm)) {
1137 skip |= LogPerformanceWarning(
1138 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
1139 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
1140 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
1141 "efficiency during rasterization. Consider disabling depthBias or increasing either "
1142 "depthBiasConstantFactor or depthBiasSlopeFactor.",
1143 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +01001144 }
1145
Attilio Provenzano02859b22020-02-27 14:17:28 +00001146 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001147 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001148 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001149 auto prev_pipeline = pipeline_cache_.load();
1150 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001151 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001152 "%s %s Performance Warning: A second pipeline cache is in use. "
1153 "Consider using only one pipeline cache to improve cache hit rate.",
1154 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04001155 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001156 }
1157 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001158 if (num_pso_ > kMaxRecommendedNumberOfPSOAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001159 skip |=
1160 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
1161 "%s Performance warning: Too many pipelines created, consider consolidation",
1162 VendorSpecificTag(kBPVendorAMD));
1163 }
1164
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -06001165 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001166 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
1167 "%s Performance warning: Use of primitive restart is not recommended",
1168 VendorSpecificTag(kBPVendorAMD));
1169 }
1170
1171 // TODO: this might be too aggressive of a check
1172 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
1173 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
1174 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1175 VendorSpecificTag(kBPVendorAMD));
1176 }
1177 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001178
Camden5b184be2019-08-13 07:50:19 -06001179 return skip;
1180}
1181
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001182static std::vector<bp_state::AttachmentInfo> GetAttachmentAccess(const safe_VkGraphicsPipelineCreateInfo& create_info,
1183 std::shared_ptr<const RENDER_PASS_STATE>& rp) {
1184 std::vector<bp_state::AttachmentInfo> result;
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06001185 if (!rp || rp->UsesDynamicRendering()) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001186 return result;
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001187 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001188
1189 const auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1190
1191 // NOTE: see PIPELINE_LAYOUT and safe_VkGraphicsPipelineCreateInfo constructors. pColorBlendState and pDepthStencilState
1192 // are only non-null if they are enabled.
1193 if (create_info.pColorBlendState) {
1194 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1195 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info.pColorBlendState->attachmentCount);
1196 for (uint32_t j = 0; j < num_color_attachments; j++) {
1197 if (create_info.pColorBlendState->pAttachments[j].colorWriteMask != 0) {
1198 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1199 if (attachment != VK_ATTACHMENT_UNUSED) {
1200 result.push_back({attachment, VK_IMAGE_ASPECT_COLOR_BIT});
1201 }
1202 }
1203 }
1204 }
1205
1206 if (create_info.pDepthStencilState &&
1207 (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable ||
1208 create_info.pDepthStencilState->stencilTestEnable)) {
1209 uint32_t attachment = subpass.pDepthStencilAttachment ? subpass.pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
1210 if (attachment != VK_ATTACHMENT_UNUSED) {
1211 VkImageAspectFlags aspects = 0;
1212 if (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable) {
1213 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1214 }
1215 if (create_info.pDepthStencilState->stencilTestEnable) {
1216 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1217 }
1218 result.push_back({attachment, aspects});
1219 }
1220 }
1221 return result;
1222}
1223
1224bp_state::Pipeline::Pipeline(const ValidationStateTracker* state_data, const VkGraphicsPipelineCreateInfo* pCreateInfo,
1225 std::shared_ptr<const RENDER_PASS_STATE>&& rpstate,
1226 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout)
1227 : PIPELINE_STATE(state_data, pCreateInfo, std::move(rpstate), std::move(layout)),
1228 access_framebuffer_attachments(GetAttachmentAccess(create_info.graphics, rp_state)) {}
1229
1230std::shared_ptr<PIPELINE_STATE> BestPractices::CreateGraphicsPipelineState(
1231 const VkGraphicsPipelineCreateInfo* pCreateInfo, std::shared_ptr<const RENDER_PASS_STATE>&& render_pass,
1232 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) const {
1233 return std::static_pointer_cast<PIPELINE_STATE>(
1234 std::make_shared<bp_state::Pipeline>(this, pCreateInfo, std::move(render_pass), std::move(layout)));
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001235}
1236
Sam Walls0961ec02020-03-31 16:39:15 +01001237void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1238 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1239 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1240 VkResult result, void* cgpl_state_data) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001241 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001242 pipeline_cache_ = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001243}
1244
Camden5b184be2019-08-13 07:50:19 -06001245bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1246 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001247 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001248 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001249 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1250 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001251
1252 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001253 skip |= LogPerformanceWarning(
1254 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1255 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1256 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001257 }
1258
Nadav Gevaf0808442021-05-21 13:51:25 -04001259 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001260 auto prev_pipeline = pipeline_cache_.load();
1261 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
1262 skip |= LogPerformanceWarning(
1263 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1264 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
Nadav Gevaf0808442021-05-21 13:51:25 -04001265 "improve cache hit rate",
1266 VendorSpecificTag(kBPVendorAMD));
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001267 }
1268 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001269
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001270 for (uint32_t i = 0; i < createInfoCount; i++) {
1271 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1272 if (VendorCheckEnabled(kBPVendorArm)) {
1273 skip |= ValidateCreateComputePipelineArm(createInfo);
1274 }
sfricke-samsung86d055a2022-02-11 14:43:50 -08001275
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001276 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1277 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1278 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1279 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1280 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1281 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1282 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1283 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1284 i);
sfricke-samsung86d055a2022-02-11 14:43:50 -08001285 }
1286 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001287 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001288 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001289
1290 return skip;
1291}
1292
1293bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1294 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001295 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001296 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001297 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1298 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001299
1300 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001301 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001302
1303 uint32_t thread_count = x * y * z;
1304
1305 // Generate a priori warnings about work group sizes.
1306 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1307 skip |= LogPerformanceWarning(
1308 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1309 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1310 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1311 "groups with less than %u threads, especially when using barrier() or shared memory.",
1312 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1313 }
1314
1315 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1316 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1317 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1318 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1319 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1320 "%u, %u) is not aligned to %u "
1321 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1322 "leave threads idle on the shader "
1323 "core.",
1324 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1325 kThreadGroupDispatchCountAlignmentArm);
1326 }
1327
sfricke-samsungef15e482022-01-26 11:32:49 -08001328 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1329 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001330
1331 unsigned dimensions = 0;
1332 if (x > 1) dimensions++;
1333 if (y > 1) dimensions++;
1334 if (z > 1) dimensions++;
1335 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1336 dimensions = std::max(dimensions, 1u);
1337
1338 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1339 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1340 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1341 bool accesses_2d = false;
1342 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001343 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001344 if (dim < 0) continue;
1345 auto spvdim = spv::Dim(dim);
1346 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1347 }
1348
1349 if (accesses_2d && dimensions < 2) {
1350 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1351 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1352 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1353 "exhibiting poor spatial locality with respect to one or more shader resources.",
1354 VendorSpecificTag(kBPVendorArm), x, y, z);
1355 }
1356
Camden5b184be2019-08-13 07:50:19 -06001357 return skip;
1358}
1359
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001360bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001361 bool skip = false;
1362
1363 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001364 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1365 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001366 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001367 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1368 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001369 }
1370
1371 return skip;
1372}
1373
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001374bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1375 bool skip = false;
1376
1377 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1378 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1379 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1380 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1381 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1382 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1383 }
1384
1385 return skip;
1386}
1387
1388bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1389 bool skip = false;
1390 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1391
1392 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1393 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1394
1395 return skip;
1396}
1397
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001398void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001399 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1400 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1401 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1402 LogPerformanceWarning(
1403 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1404 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1405 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1406 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1407 "convenient opportunity.",
1408 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1409 }
1410 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001411
1412 // AMD best practice
1413 // end-of-frame cleanup
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001414 num_queue_submissions_ = 0;
1415 num_barriers_objects_ = 0;
1416 ClearPipelinesUsedInFrame();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001417}
1418
Jeff Bolz5c801d12019-10-09 10:38:45 -05001419bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1420 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001421 bool skip = false;
1422
1423 for (uint32_t submit = 0; submit < submitCount; submit++) {
1424 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1425 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1426 }
ziga-lunargc77f0c02022-04-18 00:15:16 +02001427 if (pSubmits[submit].signalSemaphoreCount == 0 && pSubmits[submit].pSignalSemaphores != nullptr) {
1428 skip |=
1429 LogWarning(device, kVUID_BestPractices_SemaphoreCount,
1430 "pSubmits[%" PRIu32 "].pSignalSemaphores is set, but pSubmits[%" PRIu32 "].signalSemaphoreCount is 0.",
1431 submit, submit);
1432 }
1433 if (pSubmits[submit].waitSemaphoreCount == 0 && pSubmits[submit].pWaitSemaphores != nullptr) {
1434 skip |= LogWarning(device, kVUID_BestPractices_SemaphoreCount,
1435 "pSubmits[%" PRIu32 "].pWaitSemaphores is set, but pSubmits[%" PRIu32 "].waitSemaphoreCount is 0.",
1436 submit, submit);
1437 }
Camden5b184be2019-08-13 07:50:19 -06001438 }
1439
1440 return skip;
1441}
1442
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001443bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1444 VkFence fence) const {
1445 bool skip = false;
1446
1447 for (uint32_t submit = 0; submit < submitCount; submit++) {
1448 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1449 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1450 }
1451 }
1452
1453 return skip;
1454}
1455
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001456bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1457 VkFence fence) const {
1458 bool skip = false;
1459
1460 for (uint32_t submit = 0; submit < submitCount; submit++) {
1461 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1462 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1463 }
1464 }
1465
1466 return skip;
1467}
1468
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001469bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1470 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1471 bool skip = false;
1472
1473 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1474 skip |= LogPerformanceWarning(
1475 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1476 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1477 "pool instead.");
1478 }
1479
1480 return skip;
1481}
1482
1483bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1484 const VkCommandBufferBeginInfo* pBeginInfo) const {
1485 bool skip = false;
1486
1487 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1488 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1489 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1490 }
1491
Rodrigo Locattife5172b2022-03-22 18:49:29 -03001492 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorNVIDIA)) {
1493 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) {
1494 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
1495 "%s %s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1496 "For best performance on Mali and NVIDIA GPUs, consider setting ONE_TIME_SUBMIT by default.",
1497 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorNVIDIA));
1498 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001499 }
1500
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001501 return skip;
1502}
1503
Jeff Bolz5c801d12019-10-09 10:38:45 -05001504bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001505 bool skip = false;
1506
1507 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1508
1509 return skip;
1510}
1511
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001512bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1513 const VkDependencyInfoKHR* pDependencyInfo) const {
1514 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1515}
1516
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001517bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1518 const VkDependencyInfo* pDependencyInfo) const {
1519 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1520}
1521
Jeff Bolz5c801d12019-10-09 10:38:45 -05001522bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1523 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001524 bool skip = false;
1525
1526 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1527
1528 return skip;
1529}
1530
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001531bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1532 VkPipelineStageFlags2KHR stageMask) const {
1533 bool skip = false;
1534
1535 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1536
1537 return skip;
1538}
1539
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001540bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1541 VkPipelineStageFlags2 stageMask) const {
1542 bool skip = false;
1543
1544 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1545
1546 return skip;
1547}
1548
Camden5b184be2019-08-13 07:50:19 -06001549bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1550 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1551 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1552 uint32_t bufferMemoryBarrierCount,
1553 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1554 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001555 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001556 bool skip = false;
1557
1558 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1559 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1560
1561 return skip;
1562}
1563
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001564bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1565 const VkDependencyInfoKHR* pDependencyInfos) const {
1566 bool skip = false;
1567 for (uint32_t i = 0; i < eventCount; i++) {
1568 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1569 }
1570
1571 return skip;
1572}
1573
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001574bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1575 const VkDependencyInfo* pDependencyInfos) const {
1576 bool skip = false;
1577 for (uint32_t i = 0; i < eventCount; i++) {
1578 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1579 }
1580
1581 return skip;
1582}
1583
Camden5b184be2019-08-13 07:50:19 -06001584bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1585 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1586 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1587 uint32_t bufferMemoryBarrierCount,
1588 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1589 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001590 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001591 bool skip = false;
1592
1593 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1594 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1595
ziga-lunargb65dbfb2022-03-19 18:45:09 +01001596 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
1597 if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
1598 IsImageLayoutReadOnly(pImageMemoryBarriers[i].newLayout)) {
1599 skip |= LogWarning(device, kVUID_BestPractices_TransitionUndefinedToReadOnly,
1600 "VkImageMemoryBarrier is being submitted with oldLayout VK_IMAGE_LAYOUT_UNDEFINED and the contents "
1601 "may be discarded, but the newLayout is %s, which is read only.",
1602 string_VkImageLayout(pImageMemoryBarriers[i].newLayout));
1603 }
1604 }
1605
Nadav Gevaf0808442021-05-21 13:51:25 -04001606 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001607 auto num = num_barriers_objects_.load();
1608 if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001609 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001610 "%s Performance warning: In this frame, %" PRIu32
1611 " barriers were already submitted. Barriers have a high cost and can "
1612 "stall the GPU. "
1613 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1614 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04001615 }
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001616 }
1617 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
1618 static constexpr std::array<VkImageLayout, 3> read_layouts = {
Nadav Gevaf0808442021-05-21 13:51:25 -04001619 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1620 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1621 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1622 };
1623
1624 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1625 // read to read barriers
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001626 const auto &image_barrier = pImageMemoryBarriers[i];
1627 bool old_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.oldLayout) != read_layouts.end();
1628 bool new_is_read_layout = std::find(read_layouts.begin(), read_layouts.end(), image_barrier.newLayout) != read_layouts.end();
1629
Nadav Gevaf0808442021-05-21 13:51:25 -04001630 if (old_is_read_layout && new_is_read_layout) {
1631 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001632 "%s %s Performance warning: Don't issue read-to-read barriers. "
1633 "Get the resource in the right state the first time you use it.",
1634 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04001635 }
1636
1637 // general with no storage
Rodrigo Locatti494e4482022-03-30 16:37:40 -03001638 if (VendorCheckEnabled(kBPVendorAMD) && image_barrier.newLayout == VK_IMAGE_LAYOUT_GENERAL) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001639 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1640 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1641 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1642 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1643 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1644 VendorSpecificTag(kBPVendorAMD));
1645 }
1646 }
1647 }
1648 }
1649
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001650 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
1651 skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pImageMemoryBarriers[i]);
1652 }
1653
Camden5b184be2019-08-13 07:50:19 -06001654 return skip;
1655}
1656
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001657bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1658 const VkDependencyInfoKHR* pDependencyInfo) const {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001659 bool skip = false;
1660
1661 skip |= CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1662
1663 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) {
1664 skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]);
1665 }
1666
1667 return skip;
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001668}
1669
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001670bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1671 const VkDependencyInfo* pDependencyInfo) const {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001672 bool skip = false;
1673
1674 skip |= CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1675
1676 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) {
1677 skip |= ValidateCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]);
1678 }
1679
1680 return skip;
1681}
1682
1683template <typename ImageMemoryBarrier>
1684bool BestPractices::ValidateCmdPipelineBarrierImageBarrier(VkCommandBuffer commandBuffer,
1685 const ImageMemoryBarrier& barrier) const {
1686
1687 bool skip = false;
1688
1689 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1690 if (barrier.oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && barrier.newLayout != VK_IMAGE_LAYOUT_UNDEFINED) {
1691 skip |= ValidateZcull(commandBuffer, barrier.image, barrier.subresourceRange);
1692 }
1693 }
1694
1695 return skip;
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001696}
1697
Camden5b184be2019-08-13 07:50:19 -06001698bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001699 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001700 bool skip = false;
1701
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001702 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1703
1704 return skip;
1705}
1706
1707bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1708 VkQueryPool queryPool, uint32_t query) const {
1709 bool skip = false;
1710
1711 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001712
1713 return skip;
1714}
1715
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001716bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1717 VkQueryPool queryPool, uint32_t query) const {
1718 bool skip = false;
1719
1720 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1721
1722 return skip;
1723}
1724
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03001725void BestPractices::PreCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1726 VkPipeline pipeline) {
1727 StateTracker::PreCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1728
1729 auto pipeline_info = Get<PIPELINE_STATE>(pipeline);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001730 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03001731
1732 assert(pipeline_info);
1733 assert(cb);
1734
1735 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS && VendorCheckEnabled(kBPVendorNVIDIA)) {
1736 using TessGeometryMeshState = bp_state::CommandBufferStateNV::TessGeometryMesh::State;
1737 auto& tgm = cb->nv.tess_geometry_mesh;
1738
1739 // Make sure the message is only signaled once per command buffer
1740 tgm.threshold_signaled = tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA;
1741
1742 // Track pipeline switches with tessellation, geometry, and/or mesh shaders enabled, and disabled
1743 auto tgm_stages = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT |
1744 VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_TASK_BIT_NV | VK_SHADER_STAGE_MESH_BIT_NV;
1745 auto new_tgm_state = (pipeline_info->active_shaders & tgm_stages) != 0
1746 ? TessGeometryMeshState::Enabled
1747 : TessGeometryMeshState::Disabled;
1748 if (tgm.state != new_tgm_state && tgm.state != TessGeometryMeshState::Unknown) {
1749 tgm.num_switches++;
1750 }
1751 tgm.state = new_tgm_state;
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001752
1753 // Track depthTestEnable and depthCompareOp
1754 auto &pipeline_create_info = pipeline_info->GetCreateInfo<VkGraphicsPipelineCreateInfo>();
1755 auto depth_stencil_state = pipeline_create_info.pDepthStencilState;
1756 auto dynamic_state = pipeline_create_info.pDynamicState;
1757 if (depth_stencil_state && dynamic_state) {
1758 auto dynamic_state_begin = dynamic_state->pDynamicStates;
1759 auto dynamic_state_end = dynamic_state->pDynamicStates + dynamic_state->dynamicStateCount;
1760
1761 bool dynamic_depth_test_enable = std::find(dynamic_state_begin, dynamic_state_end, VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE) != dynamic_state_end;
1762 bool dynamic_depth_func = std::find(dynamic_state_begin, dynamic_state_end, VK_DYNAMIC_STATE_DEPTH_COMPARE_OP) != dynamic_state_end;
1763
1764 if (!dynamic_depth_test_enable) {
1765 RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depth_stencil_state->depthTestEnable != VK_FALSE);
1766 }
1767 if (!dynamic_depth_func) {
1768 RecordSetDepthTestState(*cb, depth_stencil_state->depthCompareOp, cb->nv.depth_test_enable);
1769 }
1770 }
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03001771 }
1772}
1773
Sam Walls0961ec02020-03-31 16:39:15 +01001774void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1775 VkPipeline pipeline) {
1776 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1777
Nadav Gevaf0808442021-05-21 13:51:25 -04001778 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001779 PipelineUsedInFrame(pipeline);
Nadav Gevaf0808442021-05-21 13:51:25 -04001780
Sam Walls0961ec02020-03-31 16:39:15 +01001781 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001782 auto pipeline_state = Get<bp_state::Pipeline>(pipeline);
Sam Walls0961ec02020-03-31 16:39:15 +01001783 // check for depth/blend state tracking
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001784 if (pipeline_state) {
1785 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001786 assert(cb_node);
1787 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001788
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001789 render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments;
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001790 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001791
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001792 const auto* blend_state = pipeline_state->ColorBlendState();
1793 const auto* stencil_state = pipeline_state->DepthStencilState();
Sam Walls0961ec02020-03-31 16:39:15 +01001794
1795 if (blend_state) {
1796 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001797 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001798 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1799 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001800 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001801 }
1802 }
1803 }
1804
1805 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001806 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001807
1808 if (stencil_state && stencil_state->depthTestEnable) {
1809 switch (stencil_state->depthCompareOp) {
1810 case VK_COMPARE_OP_EQUAL:
1811 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1812 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001813 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001814 break;
1815 default:
1816 break;
1817 }
1818 }
Sam Walls0961ec02020-03-31 16:39:15 +01001819 }
1820 }
1821}
1822
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001823void BestPractices::PreCallRecordCmdSetDepthCompareOp(VkCommandBuffer commandBuffer, VkCompareOp depthCompareOp) {
1824 StateTracker::PreCallRecordCmdSetDepthCompareOp(commandBuffer, depthCompareOp);
1825
1826 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1827 assert(cb);
1828
1829 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1830 RecordSetDepthTestState(*cb, depthCompareOp, cb->nv.depth_test_enable);
1831 }
1832}
1833
1834void BestPractices::PreCallRecordCmdSetDepthCompareOpEXT(VkCommandBuffer commandBuffer, VkCompareOp depthCompareOp) {
1835 StateTracker::PreCallRecordCmdSetDepthCompareOpEXT(commandBuffer, depthCompareOp);
1836
1837 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1838 assert(cb);
1839
1840 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1841 RecordSetDepthTestState(*cb, depthCompareOp, cb->nv.depth_test_enable);
1842 }
1843}
1844
1845void BestPractices::PreCallRecordCmdSetDepthTestEnable(VkCommandBuffer commandBuffer, VkBool32 depthTestEnable) {
1846 StateTracker::PreCallRecordCmdSetDepthTestEnable(commandBuffer, depthTestEnable);
1847
1848 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1849 assert(cb);
1850
1851 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1852 RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depthTestEnable != VK_FALSE);
1853 }
1854}
1855
1856void BestPractices::PreCallRecordCmdSetDepthTestEnableEXT(VkCommandBuffer commandBuffer, VkBool32 depthTestEnable) {
1857 StateTracker::PreCallRecordCmdSetDepthTestEnableEXT(commandBuffer, depthTestEnable);
1858
1859 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1860 assert(cb);
1861
1862 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1863 RecordSetDepthTestState(*cb, cb->nv.depth_compare_op, depthTestEnable != VK_FALSE);
1864 }
1865}
1866
1867void BestPractices::RecordSetDepthTestState(bp_state::CommandBuffer& cmd_state, VkCompareOp new_depth_compare_op, bool new_depth_test_enable) {
1868 assert(VendorCheckEnabled(kBPVendorNVIDIA));
1869
1870 if (cmd_state.nv.depth_compare_op != new_depth_compare_op) {
1871 switch (new_depth_compare_op) {
1872 case VK_COMPARE_OP_LESS:
1873 case VK_COMPARE_OP_LESS_OR_EQUAL:
1874 cmd_state.nv.zcull_direction = bp_state::CommandBufferStateNV::ZcullDirection::Less;
1875 break;
1876 case VK_COMPARE_OP_GREATER:
1877 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1878 cmd_state.nv.zcull_direction = bp_state::CommandBufferStateNV::ZcullDirection::Greater;
1879 break;
1880 default:
1881 // The other ops carry over the previous state.
1882 break;
1883 }
1884 }
1885 cmd_state.nv.depth_compare_op = new_depth_compare_op;
1886 cmd_state.nv.depth_test_enable = new_depth_test_enable;
1887}
1888
1889void BestPractices::RecordCmdBeginRenderingCommon(VkCommandBuffer commandBuffer) {
1890 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1891 assert(cmd_state);
1892
1893 auto rp = cmd_state->activeRenderPass.get();
1894 assert(rp);
1895
1896 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1897 std::shared_ptr<IMAGE_VIEW_STATE> depth_image_view_shared_ptr;
1898 IMAGE_VIEW_STATE* depth_image_view = nullptr;
1899 layer_data::optional<VkAttachmentLoadOp> load_op;
1900
1901 if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) {
1902 const auto depth_attachment = rp->dynamic_rendering_begin_rendering_info.pDepthAttachment;
1903 if (depth_attachment) {
1904 load_op.emplace(depth_attachment->loadOp);
1905 depth_image_view_shared_ptr = Get<IMAGE_VIEW_STATE>(depth_attachment->imageView);
1906 depth_image_view = depth_image_view_shared_ptr.get();
1907 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03001908
1909 for (uint32_t i = 0; i < rp->dynamic_rendering_begin_rendering_info.colorAttachmentCount; ++i) {
1910 const auto& color_attachment = rp->dynamic_rendering_begin_rendering_info.pColorAttachments[i];
1911 if (color_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1912 const VkFormat format = Get<IMAGE_VIEW_STATE>(color_attachment.imageView)->create_info.format;
1913 RecordClearColor(format, color_attachment.clearValue.color);
1914 }
1915 }
1916
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001917 } else {
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03001918 if (rp->createInfo.pAttachments) {
1919 if (rp->createInfo.subpassCount > 0) {
1920 const auto depth_attachment = rp->createInfo.pSubpasses[0].pDepthStencilAttachment;
1921 if (depth_attachment) {
1922 const uint32_t attachment_index = depth_attachment->attachment;
1923 if (attachment_index != VK_ATTACHMENT_UNUSED) {
1924 load_op.emplace(rp->createInfo.pAttachments[attachment_index].loadOp);
1925 depth_image_view = (*cmd_state->active_attachments)[attachment_index];
1926 }
1927 }
1928 }
1929 for (uint32_t i = 0; i < cmd_state->activeRenderPassBeginInfo.clearValueCount; ++i) {
1930 const auto& attachment = rp->createInfo.pAttachments[i];
1931 if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1932 const auto& clear_color = cmd_state->activeRenderPassBeginInfo.pClearValues[i].color;
1933 RecordClearColor(attachment.format, clear_color);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03001934 }
1935 }
1936 }
1937 }
1938 if (depth_image_view && (depth_image_view->create_info.subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U) {
1939 const VkImage depth_image = depth_image_view->image_state->image();
1940 const VkImageSubresourceRange& subresource_range = depth_image_view->create_info.subresourceRange;
1941 RecordBindZcullScope(*cmd_state, depth_image, subresource_range);
1942 } else {
1943 RecordUnbindZcullScope(*cmd_state);
1944 }
1945 if (load_op) {
1946 if (*load_op == VK_ATTACHMENT_LOAD_OP_CLEAR || *load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE) {
1947 RecordResetScopeZcullDirection(*cmd_state);
1948 }
1949 }
1950 }
1951}
1952
1953void BestPractices::RecordCmdEndRenderingCommon(VkCommandBuffer commandBuffer) {
1954 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1955 assert(cmd_state);
1956
1957 auto rp = cmd_state->activeRenderPass.get();
1958 assert(rp);
1959
1960 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
1961 layer_data::optional<VkAttachmentStoreOp> store_op;
1962
1963 if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) {
1964 const auto depth_attachment = rp->dynamic_rendering_begin_rendering_info.pDepthAttachment;
1965 if (depth_attachment) {
1966 store_op.emplace(depth_attachment->storeOp);
1967 }
1968 } else {
1969 if (rp->createInfo.subpassCount > 0) {
1970 const uint32_t last_subpass = rp->createInfo.subpassCount - 1;
1971 const auto depth_attachment = rp->createInfo.pSubpasses[last_subpass].pDepthStencilAttachment;
1972 if (depth_attachment) {
1973 const uint32_t attachment = depth_attachment->attachment;
1974 if (attachment != VK_ATTACHMENT_UNUSED) {
1975 store_op.emplace(rp->createInfo.pAttachments[attachment].storeOp);
1976 }
1977 }
1978 }
1979 }
1980
1981 if (store_op) {
1982 if (*store_op == VK_ATTACHMENT_STORE_OP_DONT_CARE || *store_op == VK_ATTACHMENT_STORE_OP_NONE) {
1983 RecordResetScopeZcullDirection(*cmd_state);
1984 }
1985 }
1986
1987 RecordUnbindZcullScope(*cmd_state);
1988 }
1989}
1990
1991void BestPractices::RecordBindZcullScope(bp_state::CommandBuffer& cmd_state, VkImage depth_attachment, const VkImageSubresourceRange& subresource_range) {
1992 assert(VendorCheckEnabled(kBPVendorNVIDIA));
1993
1994 if (depth_attachment == VK_NULL_HANDLE) {
1995 cmd_state.nv.zcull_scope = {};
1996 return;
1997 }
1998
1999 assert((subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U);
2000
2001 auto image_state = Get<IMAGE_STATE>(depth_attachment);
2002 assert(image_state);
2003
2004 const uint32_t mip_levels = image_state->createInfo.mipLevels;
2005 const uint32_t array_layers = image_state->createInfo.arrayLayers;
2006
2007 auto& tree = cmd_state.nv.zcull_per_image[depth_attachment];
2008 if (tree.states.empty()) {
2009 tree.mip_levels = mip_levels;
2010 tree.array_layers = array_layers;
2011 tree.states.resize(array_layers * mip_levels);
2012 }
2013
2014 cmd_state.nv.zcull_scope.image = depth_attachment;
2015 cmd_state.nv.zcull_scope.range = subresource_range;
2016 cmd_state.nv.zcull_scope.tree = &tree;
2017}
2018
2019void BestPractices::RecordUnbindZcullScope(bp_state::CommandBuffer& cmd_state) {
2020 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2021
2022 RecordBindZcullScope(cmd_state, VK_NULL_HANDLE, VkImageSubresourceRange{});
2023}
2024
2025void BestPractices::RecordResetScopeZcullDirection(bp_state::CommandBuffer& cmd_state) {
2026 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2027
2028 auto& scope = cmd_state.nv.zcull_scope;
2029 RecordResetZcullDirection(cmd_state, scope.image, scope.range);
2030}
2031
2032void BestPractices::RecordResetZcullDirection(bp_state::CommandBuffer& cmd_state, VkImage depth_image,
2033 const VkImageSubresourceRange& subresource_range) {
2034 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2035
2036 RecordSetZcullDirection(cmd_state, depth_image, subresource_range, bp_state::CommandBufferStateNV::ZcullDirection::Unknown);
2037
2038 const auto image_it = cmd_state.nv.zcull_per_image.find(depth_image);
2039 if (image_it == cmd_state.nv.zcull_per_image.end()) {
2040 return;
2041 }
2042 auto& tree = image_it->second;
2043
2044 for (uint32_t i = 0; i < subresource_range.layerCount; ++i) {
2045 const uint32_t layer = subresource_range.baseArrayLayer + i;
2046
2047 for (uint32_t j = 0; j < subresource_range.levelCount; ++j) {
2048 const uint32_t level = subresource_range.baseMipLevel + j;
2049
2050 auto& subresource = tree.GetState(layer, level);
2051 subresource.num_less_draws = 0;
2052 subresource.num_greater_draws = 0;
2053 }
2054 }
2055}
2056
2057void BestPractices::RecordSetScopeZcullDirection(bp_state::CommandBuffer& cmd_state, bp_state::CommandBufferStateNV::ZcullDirection mode) {
2058 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2059
2060 auto& scope = cmd_state.nv.zcull_scope;
2061 RecordSetZcullDirection(cmd_state, scope.image, scope.range, mode);
2062}
2063
2064void BestPractices::RecordSetZcullDirection(bp_state::CommandBuffer& cmd_state, VkImage depth_image,
2065 const VkImageSubresourceRange& subresource_range,
2066 bp_state::CommandBufferStateNV::ZcullDirection mode) {
2067 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2068
2069 const auto image_it = cmd_state.nv.zcull_per_image.find(depth_image);
2070 if (image_it == cmd_state.nv.zcull_per_image.end()) {
2071 return;
2072 }
2073 auto& tree = image_it->second;
2074
2075 for (uint32_t i = 0; i < subresource_range.layerCount; ++i) {
2076 const uint32_t layer = subresource_range.baseArrayLayer + i;
2077
2078 for (uint32_t j = 0; j < subresource_range.levelCount; ++j) {
2079 const uint32_t level = subresource_range.baseMipLevel + j;
2080 tree.GetState(layer, level).direction = cmd_state.nv.zcull_direction;
2081 }
2082 }
2083}
2084
2085void BestPractices::RecordZcullDraw(bp_state::CommandBuffer& cmd_state) {
2086 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2087
2088 // Add one draw to each subresource depending on the current Z-cull direction
2089 auto& scope = cmd_state.nv.zcull_scope;
2090
2091 for (uint32_t i = 0; i < scope.range.layerCount; ++i) {
2092 const uint32_t layer = scope.range.baseArrayLayer + i;
2093 auto& subresource = scope.tree->GetState(layer, scope.range.baseMipLevel);
2094
2095 switch (subresource.direction) {
2096 case bp_state::CommandBufferStateNV::ZcullDirection::Unknown:
2097 // Unreachable
2098 assert(0);
2099 break;
2100 case bp_state::CommandBufferStateNV::ZcullDirection::Less:
2101 ++subresource.num_less_draws;
2102 break;
2103 case bp_state::CommandBufferStateNV::ZcullDirection::Greater:
2104 ++subresource.num_greater_draws;
2105 break;
2106 }
2107 }
2108}
2109
2110bool BestPractices::ValidateZcullScope(VkCommandBuffer commandBuffer) const {
2111 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2112
2113 bool skip = false;
2114
2115 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
2116 assert(cmd_state);
2117
2118 if (cmd_state->nv.depth_test_enable) {
2119 auto& scope = cmd_state->nv.zcull_scope;
2120 skip |= ValidateZcull(commandBuffer, scope.image, scope.range);
2121 }
2122
2123 return skip;
2124}
2125
2126bool BestPractices::ValidateZcull(VkCommandBuffer commandBuffer, VkImage image,
2127 const VkImageSubresourceRange& subresource_range) const {
2128 bool skip = false;
2129
2130 const char* good_mode = nullptr;
2131 const char* bad_mode = nullptr;
2132
2133 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
2134 assert(cmd_state);
2135
2136 const auto image_it = cmd_state->nv.zcull_per_image.find(image);
2137 if (image_it == cmd_state->nv.zcull_per_image.end()) {
2138 return skip;
2139 }
2140 const auto& tree = image_it->second;
2141
2142 bool is_balanced = false;
2143
2144 for (uint32_t i = 0; i < subresource_range.layerCount; ++i) {
2145 const uint32_t layer = subresource_range.baseArrayLayer + i;
2146
2147 for (uint32_t j = 0; j < subresource_range.levelCount; ++j) {
2148 const uint32_t level = subresource_range.baseMipLevel + j;
2149
2150 const auto& resource = tree.GetState(layer, level);
2151 const uint64_t num_draws = resource.num_less_draws + resource.num_greater_draws;
2152
2153 if (num_draws > 0) {
2154 const uint64_t less_ratio = (resource.num_less_draws * 100) / num_draws;
2155 const uint64_t greater_ratio = (resource.num_greater_draws * 100) / num_draws;
2156
2157 if ((less_ratio > kZcullDirectionBalanceRatioNVIDIA) && (greater_ratio > kZcullDirectionBalanceRatioNVIDIA)) {
2158 is_balanced = true;
2159
2160 if (greater_ratio > less_ratio) {
2161 good_mode = "GREATER";
2162 bad_mode = "LESS";
2163 } else {
2164 good_mode = "LESS";
2165 bad_mode = "GREATER";
2166 }
2167 break;
2168 }
2169 }
2170 }
2171 if (is_balanced) {
2172 break;
2173 }
2174 }
2175
2176 if (is_balanced) {
2177 skip |= LogPerformanceWarning(
2178 commandBuffer, kVUID_BestPractices_Zcull_LessGreaterRatio,
2179 "%s Depth attachment %s is primarily rendered with depth compare op %s, but some draws use %s. "
2180 "Z-cull is disabled for the least used direction, which harms depth testing performance. "
2181 "The Z-cull direction can be reset by clearing the depth attachment, transitioning from VK_IMAGE_LAYOUT_UNDEFINED, "
2182 "using VK_ATTACHMENT_LOAD_OP_DONT_CARE, or using VK_ATTACHMENT_STORE_OP_DONT_CARE.",
2183 VendorSpecificTag(kBPVendorNVIDIA), report_data->FormatHandle(cmd_state->nv.zcull_scope.image).c_str(), good_mode,
2184 bad_mode);
2185 }
2186
2187 return skip;
2188}
2189
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03002190static std::array<uint32_t, 4> GetRawClearColor(VkFormat format, const VkClearColorValue& clear_value) {
2191 std::array<uint32_t, 4> raw_color{};
2192 std::copy_n(clear_value.uint32, raw_color.size(), raw_color.data());
2193
2194 // Zero out unused components to avoid polluting the cache with garbage
2195 if (!FormatHasRed(format)) raw_color[0] = 0;
2196 if (!FormatHasGreen(format)) raw_color[1] = 0;
2197 if (!FormatHasBlue(format)) raw_color[2] = 0;
2198 if (!FormatHasAlpha(format)) raw_color[3] = 0;
2199
2200 return raw_color;
2201}
2202
2203static bool IsClearColorZeroOrOne(VkFormat format, const std::array<uint32_t, 4> clear_color) {
2204 static_assert(sizeof(float) == sizeof(uint32_t), "Mismatching float <-> uint32 sizes");
2205 const float one = 1.0f;
2206 const float zero = 0.0f;
2207 uint32_t raw_one{};
2208 uint32_t raw_zero{};
2209 memcpy(&raw_one, &one, sizeof(one));
2210 memcpy(&raw_zero, &zero, sizeof(zero));
2211
2212 const bool is_one = (!FormatHasRed(format) || (clear_color[0] == raw_one)) &&
2213 (!FormatHasGreen(format) || (clear_color[1] == raw_one)) &&
2214 (!FormatHasBlue(format) || (clear_color[2] == raw_one)) &&
2215 (!FormatHasAlpha(format) || (clear_color[3] == raw_one));
2216 const bool is_zero = (!FormatHasRed(format) || (clear_color[0] == raw_zero)) &&
2217 (!FormatHasGreen(format) || (clear_color[1] == raw_zero)) &&
2218 (!FormatHasBlue(format) || (clear_color[2] == raw_zero)) &&
2219 (!FormatHasAlpha(format) || (clear_color[3] == raw_zero));
2220 return is_one || is_zero;
2221}
2222
2223static std::string MakeCompressedFormatListNVIDIA() {
2224 std::string format_list;
2225 for (VkFormat compressed_format : kCustomClearColorCompressedFormatsNVIDIA) {
2226 if (compressed_format == kCustomClearColorCompressedFormatsNVIDIA.back()) {
2227 format_list += "or ";
2228 }
2229 format_list += string_VkFormat(compressed_format);
2230 if (compressed_format != kCustomClearColorCompressedFormatsNVIDIA.back()) {
2231 format_list += ", ";
2232 }
2233 }
2234 return format_list;
2235}
2236
2237void BestPractices::RecordClearColor(VkFormat format, const VkClearColorValue& clear_value) {
2238 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2239
2240 const std::array<uint32_t, 4> raw_color = GetRawClearColor(format, clear_value);
2241 if (IsClearColorZeroOrOne(format, raw_color)) {
2242 // These colors are always compressed
2243 return;
2244 }
2245
2246 const auto it = std::find(kCustomClearColorCompressedFormatsNVIDIA.begin(), kCustomClearColorCompressedFormatsNVIDIA.end(), format);
2247 if (it == kCustomClearColorCompressedFormatsNVIDIA.end()) {
2248 // The format cannot be compressed with a custom color
2249 return;
2250 }
2251
2252 // Record custom clear color
2253 WriteLockGuard guard{clear_colors_lock_};
2254 if (clear_colors_.size() < kMaxRecommendedNumberOfClearColorsNVIDIA) {
2255 clear_colors_.insert(raw_color);
2256 }
2257}
2258
2259bool BestPractices::ValidateClearColor(VkCommandBuffer commandBuffer, VkFormat format, const VkClearColorValue& clear_value) const {
2260 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2261
2262 bool skip = false;
2263
2264 const std::array<uint32_t, 4> raw_color = GetRawClearColor(format, clear_value);
2265 if (IsClearColorZeroOrOne(format, raw_color)) {
2266 return skip;
2267 }
2268
2269 const auto it = std::find(kCustomClearColorCompressedFormatsNVIDIA.begin(), kCustomClearColorCompressedFormatsNVIDIA.end(), format);
2270 if (it == kCustomClearColorCompressedFormatsNVIDIA.end()) {
2271 // The format is not compressible
2272 static const std::string format_list = MakeCompressedFormatListNVIDIA();
2273
2274 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_ClearColor_NotCompressed,
2275 "%s Clearing image with format %s without a 1.0f or 0.0f clear color. "
2276 "The clear will not get compressed in the GPU, harming performance. "
2277 "This can be fixed using a clear color of VkClearColorValue{0.0f, 0.0f, 0.0f, 0.0f}, or "
2278 "VkClearColorValue{1.0f, 1.0f, 1.0f, 1.0f}. Alternatively, use %s.",
2279 VendorSpecificTag(kBPVendorNVIDIA), string_VkFormat(format), format_list.c_str());
2280 } else {
2281 // The format is compressible
2282 bool registered = false;
2283 {
2284 ReadLockGuard guard{clear_colors_lock_};
2285 registered = clear_colors_.find(raw_color) != clear_colors_.end();
2286
2287 if (!registered) {
2288 // If it's not in the list, it might be new. Check if there's still space for new entries.
2289 registered = clear_colors_.size() < kMaxRecommendedNumberOfClearColorsNVIDIA;
2290 }
2291 }
2292 if (!registered) {
2293 std::string clear_color_str;
2294
2295 if (FormatIsUINT(format)) {
2296 clear_color_str = std::to_string(clear_value.uint32[0]) + ", " + std::to_string(clear_value.uint32[1]) + ", " +
2297 std::to_string(clear_value.uint32[2]) + ", " + std::to_string(clear_value.uint32[3]);
2298 } else if (FormatIsSINT(format)) {
2299 clear_color_str = std::to_string(clear_value.int32[0]) + ", " + std::to_string(clear_value.int32[1]) + ", " +
2300 std::to_string(clear_value.int32[2]) + ", " + std::to_string(clear_value.int32[3]);
2301 } else {
2302 clear_color_str = std::to_string(clear_value.float32[0]) + ", " + std::to_string(clear_value.float32[1]) + ", " +
2303 std::to_string(clear_value.float32[2]) + ", " + std::to_string(clear_value.float32[3]);
2304 }
2305
2306 skip |= LogPerformanceWarning(
2307 commandBuffer, kVUID_BestPractices_ClearColor_NotCompressed,
2308 "%s Clearing image with unregistered VkClearColorValue{%s}. "
2309 "This clear will not get compressed in the GPU, harming performance. "
2310 "The clear color is not registered because too many unique colors have been used. "
2311 "Select a discrete set of clear colors and stick to those. "
2312 "VkClearColorValue{0, 0, 0, 0} and VkClearColorValue{1.0f, 1.0f, 1.0f, 1.0f} are always registered.",
2313 VendorSpecificTag(kBPVendorNVIDIA), clear_color_str.c_str());
2314 }
2315 }
2316
2317 return skip;
2318}
2319
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002320static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
2321 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
2322 const auto& subpass_info = createInfo.pSubpasses[subpass];
2323 if (subpass_info.pResolveAttachments) {
2324 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
2325 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
2326 }
2327 }
2328 }
2329
2330 return false;
2331}
2332
Attilio Provenzano02859b22020-02-27 14:17:28 +00002333static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
2334 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002335 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00002336
2337 // If an attachment is ever used as a color attachment,
2338 // resolve attachment or depth stencil attachment,
2339 // it needs to exist on tile at some point.
2340
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002341 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
2342 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002343 }
2344
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002345 if (subpass_info.pResolveAttachments) {
2346 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
2347 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
2348 }
2349 }
2350
2351 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002352 }
2353
2354 return false;
2355}
2356
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002357static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
2358 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
2359 return false;
2360 }
2361
2362 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002363 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002364
2365 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
2366 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
2367 return true;
2368 }
2369 }
2370 }
2371
2372 return false;
2373}
2374
Attilio Provenzano02859b22020-02-27 14:17:28 +00002375bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
2376 const VkRenderPassBeginInfo* pRenderPassBegin) const {
2377 bool skip = false;
2378
2379 if (!pRenderPassBegin) {
2380 return skip;
2381 }
2382
Gareth Webbdc6549a2021-06-16 03:52:24 +01002383 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
2384 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
2385 "This render pass has a zero-size render area. It cannot write to any attachments, "
2386 "and can only be used for side effects such as layout transitions.");
2387 }
2388
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002389 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002390 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002391 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002392 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06002393 if (rpabi) {
2394 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
2395 }
2396 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00002397 // Check if any attachments have LOAD operation on them
2398 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002399 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00002400
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002401 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02002402 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002403 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002404 }
2405
2406 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002407 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002408 }
2409
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002410 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002411
2412 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002413 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
2414 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00002415 }
2416
2417 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
LawG47747b322022-02-23 16:12:10 +00002418 if (attachment_needs_readback && (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) {
2419 skip |=
2420 LogPerformanceWarning(device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
LawG4015be1c2022-03-01 10:37:52 +00002421 "%s %s: Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
LawG47747b322022-02-23 16:12:10 +00002422 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04002423 "which will copy in total %u pixels (renderArea = "
LawG47747b322022-02-23 16:12:10 +00002424 "{ %" PRId32 ", %" PRId32 ", %" PRIu32 ", %" PRIu32 " }) to the tile buffer.",
2425 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), att,
2426 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
2427 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
2428 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002429 }
2430 }
paul-lunarg7089e272022-06-20 22:19:37 +02002431
2432 // Check if renderpass has at least one VK_ATTACHMENT_LOAD_OP_CLEAR
2433
2434 bool clearing = false;
2435
2436 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
2437 const auto& attachment = rp_state->createInfo.pAttachments[att];
2438
2439 if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2440 clearing = true;
2441 break;
2442 }
2443 }
2444
2445 // Check if there are ClearValues passed to BeginRenderPass even though no attachments will be cleared
2446 if (!clearing && pRenderPassBegin->clearValueCount > 0) {
2447 // Flag as warning because nothing will happen per spec, and pClearValues will be ignored
2448 skip |= LogWarning(
2449 device, kVUID_BestPractices_ClearValueWithoutLoadOpClear,
2450 "This render pass does not have VkRenderPassCreateInfo.pAttachments->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR "
2451 "but VkRenderPassBeginInfo.clearValueCount > 0. VkRenderPassBeginInfo.pClearValues will be ignored and no "
paul-lunarga0a149c2022-06-23 16:18:51 +02002452 "attachments will be cleared.");
paul-lunarg7089e272022-06-20 22:19:37 +02002453 }
paul-lunarga0a149c2022-06-23 16:18:51 +02002454
2455 // Check if there are more clearValues than attachments
2456 if(pRenderPassBegin->clearValueCount > rp_state->createInfo.attachmentCount) {
2457 // Flag as warning because the overflowing clearValues will be ignored and could even be undefined on certain platforms.
2458 // This could signal a bug and there seems to be no reason for this to happen on purpose.
2459 skip |= LogWarning(
2460 device, kVUID_BestPractices_ClearValueCountHigherThanAttachmentCount,
2461 "This render pass has VkRenderPassBeginInfo.clearValueCount > VkRenderPassCreateInfo.attachmentCount "
2462 "(%" PRIu32 " > %" PRIu32 ") and as such the clearValues that do not have a corresponding attachment will be ignored.",
2463 pRenderPassBegin->clearValueCount, rp_state->createInfo.attachmentCount);
2464 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03002465
2466 if (VendorCheckEnabled(kBPVendorNVIDIA) && rp_state->createInfo.pAttachments) {
2467 for (uint32_t i = 0; i < pRenderPassBegin->clearValueCount; ++i) {
2468 const auto& attachment = rp_state->createInfo.pAttachments[i];
2469 if (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2470 const auto& clear_color = pRenderPassBegin->pClearValues[i].color;
2471 skip |= ValidateClearColor(commandBuffer, attachment.format, clear_color);
2472 }
2473 }
2474 }
2475 }
2476
2477 return skip;
2478}
2479
2480bool BestPractices::ValidateCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const {
2481 bool skip = false;
2482
2483 auto cmd_state = Get<bp_state::CommandBuffer>(commandBuffer);
2484 assert(cmd_state);
2485
2486 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
2487 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; ++i) {
2488 const auto& color_attachment = pRenderingInfo->pColorAttachments[i];
2489 if (color_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2490 const VkFormat format = Get<IMAGE_VIEW_STATE>(color_attachment.imageView)->create_info.format;
2491 skip |= ValidateClearColor(commandBuffer, format, color_attachment.clearValue.color);
2492 }
2493 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00002494 }
2495
2496 return skip;
2497}
2498
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002499void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
2500 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002501 if (view) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002502 auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state);
2503 QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002504 }
2505}
2506
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002507void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
2508 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02002509 // If we're viewing a 3D slice, ignore base array layer.
2510 // The entire 3D subresource is accessed as one atomic unit.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002511 const uint32_t base_array_layer = state->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02002512
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002513 const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002514 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002515 const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel;
2516 const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002517
2518 for (uint32_t layer = 0; layer < array_layers; layer++) {
2519 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02002520 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
2521 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002522 }
2523 }
2524}
2525
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002526void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
2527 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) {
2528 const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002529 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002530
2531 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02002532 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002533 }
2534}
2535
paul-lunarg5eb52062022-06-27 18:57:15 +02002536void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
2537 IMAGE_SUBRESOURCE_USAGE_BP usage, uint32_t array_layer, uint32_t mip_level) {
2538 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](const ValidationStateTracker&, const QUEUE_STATE&,
2539 const CMD_BUFFER_STATE&) -> bool {
2540 ValidateImageInQueue(function_name, *state, usage, array_layer, mip_level);
2541 return false;
2542 });
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002543}
2544
LawG44d414ba2022-02-23 15:35:41 +00002545void BestPractices::ValidateImageInQueueArmImg(const char* function_name, const bp_state::Image& image,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002546 IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002547 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002548 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002549 if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002550 !image.IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002551 LogPerformanceWarning(
2552 device, kVUID_BestPractices_RenderPass_RedundantStore,
LawG4015be1c2022-03-01 10:37:52 +00002553 "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time "
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002554 "image was used, it was written to with STORE_OP_STORE. "
2555 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
2556 "architectures.",
LawG44d414ba2022-02-23 15:35:41 +00002557 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002558 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002559 LogPerformanceWarning(
2560 device, kVUID_BestPractices_RenderPass_RedundantClear,
LawG4015be1c2022-03-01 10:37:52 +00002561 "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time "
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002562 "image was used, it was written to with vkCmdClear*Image(). "
2563 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
LawG44d414ba2022-02-23 15:35:41 +00002564 "tile-based architectures.",
2565 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01002566 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002567 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
2568 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01002569 const char *last_cmd = nullptr;
2570 const char *vuid = nullptr;
2571 const char *suggestion = nullptr;
2572
2573 switch (last_usage) {
2574 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
2575 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
2576 last_cmd = "vkCmdBlitImage";
2577 suggestion =
2578 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
2579 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
2580 "which avoids the memory roundtrip.";
2581 break;
2582 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
2583 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
2584 last_cmd = "vkCmdClear*Image";
2585 suggestion =
2586 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
2587 "tile-based architectures. "
2588 "Use LOAD_OP_CLEAR instead to clear the image for free.";
2589 break;
2590 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
2591 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
2592 last_cmd = "vkCmdCopy*Image";
2593 suggestion =
2594 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
2595 "Rather than copying, just render the source image in a fragment shader in this render pass, "
2596 "which avoids the memory roundtrip.";
2597 break;
2598 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
2599 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
2600 last_cmd = "vkCmdResolveImage";
2601 suggestion =
2602 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
2603 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
2604 "which avoids the memory roundtrip.";
2605 break;
2606 default:
2607 break;
2608 }
2609
2610 LogPerformanceWarning(
2611 device, vuid,
LawG4015be1c2022-03-01 10:37:52 +00002612 "%s %s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was loaded to tile as part of LOAD_OP_LOAD, but last "
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01002613 "time image was used, it was written to with %s. %s",
LawG44d414ba2022-02-23 15:35:41 +00002614 function_name, VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), array_layer, mip_level, last_cmd,
2615 suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002616 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002617}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002618
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002619void BestPractices::ValidateImageInQueue(const char* function_name, bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage,
2620 uint32_t array_layer, uint32_t mip_level) {
2621 auto last_usage = state.UpdateUsage(array_layer, mip_level, usage);
paul-lunarg5eb52062022-06-27 18:57:15 +02002622
2623 // When image was discarded with StoreOpDontCare but is now being read with LoadOpLoad
2624 if (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED &&
2625 usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE) {
2626 LogWarning(device, kVUID_BestPractices_StoreOpDontCareThenLoadOpLoad,
2627 "Trying to load an attachment with LOAD_OP_LOAD that was previously stored with STORE_OP_DONT_CARE. This may "
2628 "result in undefined behaviour.");
2629 }
2630
LawG44d414ba2022-02-23 15:35:41 +00002631 if (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) {
2632 ValidateImageInQueueArmImg(function_name, state, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002633 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002634}
2635
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002636void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) {
2637 cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(),
2638 cb.queue_submit_functions_after_render_pass.end());
2639 cb.queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002640}
2641
2642void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002643 RecordCmdEndRenderingCommon(commandBuffer);
2644
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002645 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002646 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2647 if (cb_node) {
2648 AddDeferredQueueOperations(*cb_node);
2649 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002650}
2651
2652void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002653 RecordCmdEndRenderingCommon(commandBuffer);
2654
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002655 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002656 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2657 if (cb_node) {
2658 AddDeferredQueueOperations(*cb_node);
2659 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002660}
2661
2662void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002663 RecordCmdEndRenderingCommon(commandBuffer);
2664
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002665 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002666 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2667 if (cb_node) {
2668 AddDeferredQueueOperations(*cb_node);
2669 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002670}
2671
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002672void BestPractices::PreCallRecordCmdEndRendering(VkCommandBuffer commandBuffer) {
2673 RecordCmdEndRenderingCommon(commandBuffer);
2674
2675 ValidationStateTracker::PreCallRecordCmdEndRendering(commandBuffer);
2676}
2677
2678void BestPractices::PreCallRecordCmdEndRenderingKHR(VkCommandBuffer commandBuffer) {
2679 RecordCmdEndRenderingCommon(commandBuffer);
2680
2681 ValidationStateTracker::PreCallRecordCmdEndRenderingKHR(commandBuffer);
2682}
2683
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002684void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
2685 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002686 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002687 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002688 RecordCmdBeginRenderingCommon(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002689 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2690}
2691
2692void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
2693 const VkRenderPassBeginInfo* pRenderPassBegin,
2694 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2695 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002696 RecordCmdBeginRenderingCommon(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002697 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2698}
2699
2700void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2701 const VkRenderPassBeginInfo* pRenderPassBegin,
2702 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2703 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002704 RecordCmdBeginRenderingCommon(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002705 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
2706}
2707
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002708void BestPractices::PreCallRecordCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) {
2709 ValidationStateTracker::PreCallRecordCmdBeginRendering(commandBuffer, pRenderingInfo);
2710 RecordCmdBeginRenderingCommon(commandBuffer);
2711}
2712
2713void BestPractices::PreCallRecordCmdBeginRenderingKHR(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) {
2714 ValidationStateTracker::PreCallRecordCmdBeginRenderingKHR(commandBuffer, pRenderingInfo);
2715 RecordCmdBeginRenderingCommon(commandBuffer);
2716}
2717
2718void BestPractices::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2719 ValidationStateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2720
2721 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2722 auto rp = cmd_state->activeRenderPass.get();
2723 assert(rp);
2724
2725 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
2726 IMAGE_VIEW_STATE* depth_image_view = nullptr;
2727
2728 const auto depth_attachment = rp->createInfo.pSubpasses[cmd_state->activeSubpass].pDepthStencilAttachment;
2729 if (depth_attachment) {
2730 const uint32_t attachment_index = depth_attachment->attachment;
2731 if (attachment_index != VK_ATTACHMENT_UNUSED) {
2732 depth_image_view = (*cmd_state->active_attachments)[attachment_index];
2733 }
2734 }
2735 if (depth_image_view && (depth_image_view->create_info.subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0U) {
2736 const VkImage depth_image = depth_image_view->image_state->image();
2737 const VkImageSubresourceRange& subresource_range = depth_image_view->create_info.subresourceRange;
2738 RecordBindZcullScope(*cmd_state, depth_image, subresource_range);
2739 } else {
2740 RecordUnbindZcullScope(*cmd_state);
2741 }
2742 }
2743}
2744
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002745void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002746
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002747 if (!pRenderPassBegin) {
2748 return;
2749 }
2750
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002751 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01002752
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002753 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002754 if (rp_state) {
2755 // Check load ops
2756 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002757 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002758
2759 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
2760 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
2761 continue;
2762 }
2763
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002764 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002765
2766 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
2767 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002768 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02002769 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
2770 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002771 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02002772 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01002773 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002774 }
2775
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002776 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002777 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002778
Tony-LunarGb3ab3572021-07-02 09:45:17 -06002779 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002780 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
2781 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002782 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002783 }
2784 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002785 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002786 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002787
Jeremy Gebben9f537102021-10-05 16:37:12 -06002788 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002789 }
2790
2791 // Check store ops
2792 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02002793 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002794
2795 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
2796 continue;
2797 }
2798
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002799 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002800
2801 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
2802 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01002803 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002804 }
2805
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002806 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002807
Jeremy Gebben9f537102021-10-05 16:37:12 -06002808 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06002809 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002810 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
2811 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002812 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002813 }
2814 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002815 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01002816 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002817
Jeremy Gebben9f537102021-10-05 16:37:12 -06002818 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002819 }
2820 }
2821}
2822
Attilio Provenzano02859b22020-02-27 14:17:28 +00002823bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2824 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002825 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2826 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002827 return skip;
2828}
2829
2830bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2831 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08002832 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002833 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2834 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002835 return skip;
2836}
2837
2838bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08002839 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01002840 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2841 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002842 return skip;
2843}
2844
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03002845bool BestPractices::PreCallValidateCmdBeginRendering(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const {
2846 bool skip = StateTracker::PreCallValidateCmdBeginRendering(commandBuffer, pRenderingInfo);
2847 skip |= ValidateCmdBeginRendering(commandBuffer, pRenderingInfo);
2848 return skip;
2849}
2850
2851bool BestPractices::PreCallValidateCmdBeginRenderingKHR(VkCommandBuffer commandBuffer, const VkRenderingInfo* pRenderingInfo) const {
2852 bool skip = StateTracker::PreCallValidateCmdBeginRenderingKHR(commandBuffer, pRenderingInfo);
2853 skip |= ValidateCmdBeginRendering(commandBuffer, pRenderingInfo);
2854 return skip;
2855}
2856
Sam Walls0961ec02020-03-31 16:39:15 +01002857void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
2858 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002859 // Reset the renderpass state
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002860 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
sjfricke52defd42022-08-08 16:37:46 +09002861 // TODO - move this logic to the Render Pass state as cb->has_draw_cmd should stay true for lifetime of command buffer
2862 cb->has_draw_cmd = false;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002863 assert(cb);
2864 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002865 render_pass_state.touchesAttachments.clear();
2866 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002867 render_pass_state.numDrawCallsDepthOnly = 0;
2868 render_pass_state.numDrawCallsDepthEqualCompare = 0;
2869 render_pass_state.colorAttachment = false;
2870 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002871 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002872 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01002873
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03002874 // Reset NV state
2875 cb->nv = {};
2876
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002877 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01002878
2879 // track depth / color attachment usage within the renderpass
2880 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
2881 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002882 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002883
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02002884 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01002885 }
2886}
2887
2888void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2889 VkSubpassContents contents) {
2890 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2891 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
2892}
2893
2894void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
2895 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2896 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2897 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2898}
2899
2900void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2901 const VkRenderPassBeginInfo* pRenderPassBegin,
2902 const VkSubpassBeginInfo* pSubpassBeginInfo) {
2903 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2904 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
2905}
2906
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002907// Generic function to handle validation for all CmdDraw* type functions
2908bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
2909 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002910 const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002911 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002912 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
2913 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002914 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002915
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002916 // Verify vertex binding
Tony-LunarG2ffe1f52022-04-11 15:13:30 -06002917 if (pipeline_state && pipeline_state->vertex_input_state &&
2918 pipeline_state->vertex_input_state->binding_descriptions.size() <= 0) {
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002919 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002920 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002921 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002922 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2923 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002924 }
2925 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002926
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002927 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002928 if (pipe) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002929 const auto& rp_state = pipe->RenderPassState();
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002930 if (rp_state) {
2931 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2932 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002933 const auto* ds_state = pipe->DepthStencilState();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002934 const uint32_t depth_stencil_attachment =
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002935 GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
2936 const auto* raster_state = pipe->RasterizationState();
2937 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && raster_state &&
2938 raster_state->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002939 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2940 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2941 }
2942 }
2943 }
2944 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002945 }
2946 return skip;
2947}
2948
Sam Walls0961ec02020-03-31 16:39:15 +01002949void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002950 auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002951 assert(cb_node);
Sam Walls0961ec02020-03-31 16:39:15 +01002952 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002953 RecordCmdDrawTypeArm(*cb_node, draw_count, caller);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002954 }
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002955 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
2956 RecordCmdDrawTypeNVIDIA(*cb_node);
2957 }
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002958
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002959 if (cb_node->render_pass_state.drawTouchAttachments) {
2960 for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) {
2961 RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002962 }
2963 // No need to touch the same attachments over and over.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002964 cb_node->render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002965 }
2966}
2967
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002968void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) {
2969 auto& render_pass_state = cb_node.render_pass_state;
LawG4b21485c2022-02-28 13:46:48 +00002970 // Each TBDR vendor requires a depth pre-pass draw call to have a minimum number of vertices/indices before it counts towards
2971 // depth prepass warnings First find the lowest enabled draw count
2972 uint32_t lowestEnabledMinDrawCount = 0;
2973 lowestEnabledMinDrawCount = VendorCheckEnabled(kBPVendorArm) * kDepthPrePassMinDrawCountArm;
2974 if (VendorCheckEnabled(kBPVendorIMG) && kDepthPrePassMinDrawCountIMG < lowestEnabledMinDrawCount)
2975 lowestEnabledMinDrawCount = kDepthPrePassMinDrawCountIMG;
2976
2977 if (draw_count >= lowestEnabledMinDrawCount) {
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002978 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2979 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002980 }
2981}
2982
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03002983void BestPractices::RecordCmdDrawTypeNVIDIA(bp_state::CommandBuffer& cmd_state) {
2984 assert(VendorCheckEnabled(kBPVendorNVIDIA));
2985
2986 if (cmd_state.nv.depth_test_enable && cmd_state.nv.zcull_direction != bp_state::CommandBufferStateNV::ZcullDirection::Unknown) {
2987 RecordSetScopeZcullDirection(cmd_state, cmd_state.nv.zcull_direction);
2988 RecordZcullDraw(cmd_state);
2989 }
2990}
2991
Camden5b184be2019-08-13 07:50:19 -06002992bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002993 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002994 bool skip = false;
2995
2996 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002997 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2998 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002999 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06003000 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06003001
3002 return skip;
3003}
3004
Sam Walls0961ec02020-03-31 16:39:15 +01003005void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3006 uint32_t firstVertex, uint32_t firstInstance) {
3007 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
3008 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
3009}
3010
Camden5b184be2019-08-13 07:50:19 -06003011bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003012 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06003013 bool skip = false;
3014
3015 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003016 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
3017 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06003018 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07003019 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
3020
Attilio Provenzano02859b22020-02-27 14:17:28 +00003021 // Check if we reached the limit for small indexed draw calls.
3022 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003023 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003024 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02003025 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
LawG4ff42d722022-03-01 10:28:25 +00003026 (VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG))) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02003027 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
LawG4ff42d722022-03-01 10:28:25 +00003028 "%s %s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00003029 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
3030 "You can try batching drawcalls or instancing when applicable.",
LawG4ff42d722022-03-01 10:28:25 +00003031 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), kMaxSmallIndexedDrawcalls,
3032 kSmallIndexedDrawcallIndices);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003033 }
3034
Sam Walls8e77e4f2020-03-16 20:47:40 +00003035 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003036 ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Sam Walls8e77e4f2020-03-16 20:47:40 +00003037 }
3038
3039 return skip;
3040}
3041
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003042bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount,
Sam Walls8e77e4f2020-03-16 20:47:40 +00003043 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
3044 bool skip = false;
3045
3046 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Sam Walls8e77e4f2020-03-16 20:47:40 +00003047
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003048 const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get();
3049 if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00003050
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003051 const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003052 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00003053 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
3054 const void* ib_mem = ib_mem_state.p_driver_data;
3055 bool primitive_restart_enable = false;
3056
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003057 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003058 const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003059 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00003060
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003061 const auto* ia_state = pipeline_state ? pipeline_state->InputAssemblyState() : nullptr;
3062 if (ia_state) {
3063 primitive_restart_enable = ia_state->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003064 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00003065
3066 // no point checking index buffer if the memory is nonexistant/unmapped, or if there is no graphics pipeline bound to this CB
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003067 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00003068 uint32_t scan_stride;
3069 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
3070 scan_stride = sizeof(uint8_t);
3071 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
3072 scan_stride = sizeof(uint16_t);
3073 } else {
3074 scan_stride = sizeof(uint32_t);
3075 }
3076
3077 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
3078 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
3079
3080 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
3081 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
3082 // irrespective of whether or not they're part of the draw call.
3083
3084 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
3085 uint32_t min_index = ~0u;
3086 // start with maximum as 0 and adjust to indices in the buffer
3087 uint32_t max_index = 0u;
3088
3089 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
3090 // for the given index buffer
3091 uint32_t vertex_shade_count = 0;
3092
3093 PostTransformLRUCacheModel post_transform_cache;
3094
3095 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
3096 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
3097 // target architecture.
3098 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
3099 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
3100 post_transform_cache.resize(32);
3101
3102 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
3103 uint32_t scan_index;
3104 uint32_t primitive_restart_value;
3105 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
3106 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
3107 primitive_restart_value = 0xFF;
3108 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
3109 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
3110 primitive_restart_value = 0xFFFF;
3111 } else {
3112 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
3113 primitive_restart_value = 0xFFFFFFFF;
3114 }
3115
3116 max_index = std::max(max_index, scan_index);
3117 min_index = std::min(min_index, scan_index);
3118
3119 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
3120 bool in_cache = post_transform_cache.query_cache(scan_index);
3121 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
3122 if (!in_cache) vertex_shade_count++;
3123 }
3124 }
3125
3126 // if the max and min values were not set, then we either have no indices, or all primitive restarts, exit...
Sam Walls61b06892020-07-23 16:20:50 +01003127 // 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
3128 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00003129
3130 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07003131 skip |=
3132 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
3133 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
3134 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
3135 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
3136 "maximum would be loaded, and possibly shaded, whether or not they are used.",
3137 VendorSpecificTag(kBPVendorArm),
3138 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00003139 return skip;
3140 }
3141
3142 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
3143 // each bit of the n-th bucket contains the inclusion information for indices (n*n_buckets) to ((n+1)*n_buckets)
Sam Walls61b06892020-07-23 16:20:50 +01003144 const size_t refs_per_bucket = 64;
3145 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
3146
3147 const uint32_t n_indices = max_index - min_index + 1;
3148 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
3149 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
3150
3151 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
3152 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00003153
3154 // To avoid using too much memory, we run over the indices again.
3155 // Knowing the size from the last scan allows us to record index usage with bitsets
3156 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
3157 uint32_t scan_index;
3158 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
3159 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
3160 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
3161 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
3162 } else {
3163 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
3164 }
3165 // keep track of the set of all indices used to reference vertices in the draw call
3166 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01003167 size_t bitset_bucket_index = index_offset / refs_per_bucket;
3168 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00003169 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
3170 }
3171
3172 uint32_t vertex_reference_count = 0;
3173 for (const auto& bitset : vertex_reference_buckets) {
3174 vertex_reference_count += static_cast<uint32_t>(bitset.count());
3175 }
3176
3177 // low index buffer utilization implies that: of the vertices available to the draw call, not all are utilized
Mark Young0ec6b062020-11-19 15:32:17 -07003178 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00003179 // low hit rate (high miss rate) implies the order of indices in the draw call may be possible to improve
Mark Young0ec6b062020-11-19 15:32:17 -07003180 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00003181
3182 if (utilization < 0.5f) {
3183 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
3184 "%s The indices which were specified for the draw call only utilise approximately "
3185 "%.02f%% of the bound vertex buffer.",
3186 VendorSpecificTag(kBPVendorArm), utilization);
3187 }
3188
3189 if (cache_hit_rate <= 0.5f) {
3190 skip |=
3191 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
3192 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
3193 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
3194 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
3195 "recently shaded vertices.",
3196 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
3197 }
3198 }
3199
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07003200 return skip;
3201}
3202
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003203bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
3204 const VkCommandBuffer* pCommandBuffers) const {
3205 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003206 const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003207 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003208 const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003209 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003210 continue;
3211 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003212 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003213 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003214 if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) {
3215 skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003216 }
3217 }
3218 }
Nadav Gevaf0808442021-05-21 13:51:25 -04003219
3220 if (VendorCheckEnabled(kBPVendorAMD)) {
3221 if (commandBufferCount > 0) {
3222 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
3223 "%s Performance warning: Use of secondary command buffers is not recommended. ",
3224 VendorSpecificTag(kBPVendorAMD));
3225 }
3226 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003227 return skip;
3228}
3229
3230void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
3231 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003232 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
3233
3234 auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3235 if (!primary) {
3236 return;
3237 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003238
3239 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003240 auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]);
3241 if (!secondary) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003242 continue;
3243 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003244
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003245 for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) {
3246 if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
3247 RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment,
3248 early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003249 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003250 RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003251 }
3252 }
3253
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003254 for (auto& touch : secondary->render_pass_state.touchesAttachments) {
3255 RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003256 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02003257
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003258 primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare;
3259 primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003260 }
3261
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003262}
3263
Rodrigo Locatti7d716e12022-03-09 19:15:17 -03003264bool BestPractices::PreCallValidateCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer,
3265 const VkAccelerationStructureInfoNV* pInfo,
3266 VkBuffer instanceData, VkDeviceSize instanceOffset,
3267 VkBool32 update, VkAccelerationStructureNV dst,
3268 VkAccelerationStructureNV src, VkBuffer scratch,
3269 VkDeviceSize scratchOffset) const {
3270 return ValidateBuildAccelerationStructure(commandBuffer);
3271}
3272
3273bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresIndirectKHR(
3274 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
3275 const VkDeviceAddress* pIndirectDeviceAddresses, const uint32_t* pIndirectStrides,
3276 const uint32_t* const* ppMaxPrimitiveCounts) const {
3277 return ValidateBuildAccelerationStructure(commandBuffer);
3278}
3279
3280bool BestPractices::PreCallValidateCmdBuildAccelerationStructuresKHR(
3281 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
3282 const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) const {
3283 return ValidateBuildAccelerationStructure(commandBuffer);
3284}
3285
3286bool BestPractices::ValidateBuildAccelerationStructure(VkCommandBuffer commandBuffer) const {
3287 bool skip = false;
3288 auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
3289 assert(cb_node);
3290
3291 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3292 if ((cb_node->GetQueueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) {
3293 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_AccelerationStructure_NotAsync,
3294 "%s Performance warning: Prefer building acceleration structures on an asynchronous "
3295 "compute queue, instead of using the universal graphics queue.",
3296 VendorSpecificTag(kBPVendorNVIDIA));
3297 }
3298 }
3299
3300 return skip;
3301}
3302
Rodrigo Locatti66b23352022-03-15 17:28:32 -03003303bool BestPractices::ValidateBindMemory(VkDevice device, VkDeviceMemory memory) const {
3304 bool skip = false;
3305
3306 if (VendorCheckEnabled(kBPVendorNVIDIA) && device_extensions.vk_ext_pageable_device_local_memory) {
3307 auto mem_info = std::static_pointer_cast<const bp_state::DeviceMemory>(Get<DEVICE_MEMORY_STATE>(memory));
3308 if (!mem_info->dynamic_priority) {
3309 skip |=
3310 LogPerformanceWarning(device, kVUID_BestPractices_BindMemory_NoPriority,
3311 "%s Use vkSetDeviceMemoryPriorityEXT to provide the OS with information on which allocations "
3312 "should stay in memory and which should be demoted first when video memory is limited. The "
3313 "highest priority should be given to GPU-written resources like color attachments, depth "
3314 "attachments, storage images, and buffers written from the GPU.",
3315 VendorSpecificTag(kBPVendorNVIDIA));
3316 }
3317 }
3318
3319 return skip;
3320}
3321
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003322void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
3323 auto& state = cb_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003324 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003325 auto itr =
3326 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
3327 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003328
3329 if (itr != state.touchesAttachments.end()) {
3330 itr->aspects |= aspects;
3331 } else {
3332 state.touchesAttachments.push_back({ fb_attachment, aspects });
3333 }
3334}
3335
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003336void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003337 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
3338 const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003339 auto& state = cmd_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003340 // If we observe a full clear before any other access to a frame buffer attachment,
3341 // we have candidate for redundant clear attachments.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003342 auto itr =
3343 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
3344 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003345
3346 uint32_t new_aspects = aspects;
3347 if (itr != state.touchesAttachments.end()) {
3348 new_aspects = aspects & ~itr->aspects;
3349 itr->aspects |= aspects;
3350 } else {
3351 state.touchesAttachments.push_back({ fb_attachment, aspects });
3352 }
3353
3354 if (new_aspects == 0) {
3355 return;
3356 }
3357
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003358 if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003359 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
3360 // CmdExecuteCommands.
3361 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
3362 std::vector<VkClearRect>{pRects, pRects + rectCount} });
3363 }
3364}
3365
3366void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
3367 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
3368 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003369 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects);
3370
3371 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3372 auto* rp_state = cmd_state->activeRenderPass.get();
3373 auto* fb_state = cmd_state->activeFramebuffer.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003374 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
3375
3376 if (rectCount == 0 || !rp_state) {
3377 return;
3378 }
3379
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003380 if (!is_secondary && !fb_state && !rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003381 return;
3382 }
3383
3384 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003385 const bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003386
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003387 if (rp_state->UsesDynamicRendering()) {
3388 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03003389 auto pColorAttachments = rp_state->dynamic_rendering_begin_rendering_info.pColorAttachments;
3390
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003391 for (uint32_t i = 0; i < attachmentCount; i++) {
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03003392 auto& clear_attachment = pClearAttachments[i];
3393
3394 if (clear_attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003395 RecordResetScopeZcullDirection(*cmd_state);
3396 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03003397 if ((clear_attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
3398 clear_attachment.colorAttachment != VK_ATTACHMENT_UNUSED &&
3399 pColorAttachments) {
3400 const auto& attachment = pColorAttachments[clear_attachment.colorAttachment];
3401 if (attachment.imageView) {
3402 auto image_view_state = Get<IMAGE_VIEW_STATE>(attachment.imageView);
3403 const VkFormat format = image_view_state->create_info.format;
3404 RecordClearColor(format, clear_attachment.clearValue.color);
3405 }
3406 }
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003407 }
3408 }
3409
3410 // TODO: Implement other best practices for dynamic rendering
3411
3412 } else {
ziga-lunarg885c6542022-03-07 01:08:25 +01003413 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
3414 for (uint32_t i = 0; i < attachmentCount; i++) {
3415 auto& attachment = pClearAttachments[i];
3416 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
3417 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003418
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003419 if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
3420 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3421 RecordResetScopeZcullDirection(*cmd_state);
3422 }
3423 }
ziga-lunarg885c6542022-03-07 01:08:25 +01003424 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
3425 if (subpass.pDepthStencilAttachment) {
3426 fb_attachment = subpass.pDepthStencilAttachment->attachment;
3427 }
3428 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
3429 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003430 }
ziga-lunarg885c6542022-03-07 01:08:25 +01003431 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
3432 if (full_clear) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003433 RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment,
ziga-lunarg885c6542022-03-07 01:08:25 +01003434 aspects, rectCount, pRects);
3435 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003436 RecordAttachmentAccess(*cmd_state, fb_attachment, aspects);
ziga-lunarg885c6542022-03-07 01:08:25 +01003437 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03003438 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3439 const VkFormat format = rp_state->createInfo.pAttachments[fb_attachment].format;
3440 RecordClearColor(format, attachment.clearValue.color);
3441 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003442 }
3443 }
3444 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003445}
3446
Attilio Provenzano02859b22020-02-27 14:17:28 +00003447void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3448 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
3449 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
3450 firstInstance);
3451
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003452 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003453 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
3454 cmd_state->small_indexed_draw_call_count++;
3455 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003456
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003457 ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00003458}
3459
Sam Walls0961ec02020-03-31 16:39:15 +01003460void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3461 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
3462 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
3463 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
3464}
3465
Camden5b184be2019-08-13 07:50:19 -06003466bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003467 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06003468 bool skip = false;
3469
3470 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003471 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
3472 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06003473 }
3474
Rodrigo Locatti8419cde2022-03-30 18:45:13 -03003475 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
3476
Camden5b184be2019-08-13 07:50:19 -06003477 return skip;
3478}
3479
Sam Walls0961ec02020-03-31 16:39:15 +01003480void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3481 uint32_t count, uint32_t stride) {
3482 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
3483 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
3484}
3485
Camden5b184be2019-08-13 07:50:19 -06003486bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003487 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06003488 bool skip = false;
3489
3490 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003491 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
3492 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06003493 }
3494
Rodrigo Locatti8419cde2022-03-30 18:45:13 -03003495 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
3496
Camden5b184be2019-08-13 07:50:19 -06003497 return skip;
3498}
3499
Sam Walls0961ec02020-03-31 16:39:15 +01003500void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3501 uint32_t count, uint32_t stride) {
3502 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
3503 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
3504}
3505
Rodrigo Locatti467344a2022-03-30 18:48:13 -03003506bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3507 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3508 uint32_t maxDrawCount, uint32_t stride) const {
3509 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
3510
3511 return skip;
3512}
3513
3514void BestPractices::PostCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3515 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3516 uint32_t maxDrawCount, uint32_t stride) {
3517 StateTracker::PostCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3518 maxDrawCount, stride);
3519 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCount()");
3520}
3521
3522bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3523 VkDeviceSize offset, VkBuffer countBuffer,
3524 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3525 uint32_t stride) const {
3526 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountAMD");
3527
3528 return skip;
3529}
3530
3531void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3532 VkDeviceSize offset, VkBuffer countBuffer,
3533 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3534 uint32_t stride) {
3535 StateTracker::PostCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3536 maxDrawCount, stride);
3537 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountAMD()");
3538}
3539
3540bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3541 VkDeviceSize offset, VkBuffer countBuffer,
3542 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3543 uint32_t stride) const {
3544 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR");
3545
3546 return skip;
3547}
3548
3549void BestPractices::PostCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3550 VkDeviceSize offset, VkBuffer countBuffer,
3551 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3552 uint32_t stride) {
3553 StateTracker::PostCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3554 maxDrawCount, stride);
3555 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndexedIndirectCountKHR()");
3556}
3557
3558bool BestPractices::PreCallValidateCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount,
3559 uint32_t firstInstance, VkBuffer counterBuffer,
3560 VkDeviceSize counterBufferOffset, uint32_t counterOffset,
3561 uint32_t vertexStride) const {
3562 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectByteCountEXT");
3563
3564 return skip;
3565}
3566
3567void BestPractices::PostCallRecordCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount,
3568 uint32_t firstInstance, VkBuffer counterBuffer,
3569 VkDeviceSize counterBufferOffset, uint32_t counterOffset,
3570 uint32_t vertexStride) {
3571 StateTracker::PostCallRecordCmdDrawIndirectByteCountEXT(commandBuffer, instanceCount, firstInstance, counterBuffer,
3572 counterBufferOffset, counterOffset, vertexStride);
3573 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectByteCountEXT()");
3574}
3575
3576bool BestPractices::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3577 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3578 uint32_t stride) const {
3579 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCount");
3580
3581 return skip;
3582}
3583
3584void BestPractices::PostCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3585 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3586 uint32_t stride) {
3587 StateTracker::PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3588 stride);
3589 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCount()");
3590}
3591
3592bool BestPractices::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3593 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3594 uint32_t maxDrawCount, uint32_t stride) const {
3595 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountAMD");
3596
3597 return skip;
3598}
3599
3600void BestPractices::PostCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3601 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3602 uint32_t maxDrawCount, uint32_t stride) {
3603 StateTracker::PostCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3604 stride);
3605 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountAMD()");
3606}
3607
3608bool BestPractices::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3609 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3610 uint32_t maxDrawCount, uint32_t stride) const {
3611 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirectCountKHR");
3612
3613 return skip;
3614}
3615
3616void BestPractices::PostCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3617 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3618 uint32_t maxDrawCount, uint32_t stride) {
3619 StateTracker::PostCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3620 stride);
3621 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawIndirectCountKHR()");
3622}
3623
3624bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
3625 VkDeviceSize offset, VkBuffer countBuffer,
3626 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3627 uint32_t stride) const {
3628 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectCountNV");
3629
3630 return skip;
3631}
3632
3633void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
3634 VkDeviceSize offset, VkBuffer countBuffer,
3635 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3636 uint32_t stride) {
3637 StateTracker::PostCallRecordCmdDrawMeshTasksIndirectCountNV(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3638 maxDrawCount, stride);
3639 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectCountNV()");
3640}
3641
3642bool BestPractices::PreCallValidateCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3643 uint32_t drawCount, uint32_t stride) const {
3644 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksIndirectNV");
3645
3646 return skip;
3647}
3648
3649void BestPractices::PostCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3650 uint32_t drawCount, uint32_t stride) {
3651 StateTracker::PostCallRecordCmdDrawMeshTasksIndirectNV(commandBuffer, buffer, offset, drawCount, stride);
3652 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksIndirectNV()");
3653}
3654
3655bool BestPractices::PreCallValidateCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) const {
3656 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMeshTasksNV");
3657
3658 return skip;
3659}
3660
3661void BestPractices::PostCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) {
3662 StateTracker::PostCallRecordCmdDrawMeshTasksNV(commandBuffer, taskCount, firstTask);
3663 RecordCmdDrawType(commandBuffer, 0, "vkCmdDrawMeshTasksNV()");
3664}
3665
3666bool BestPractices::PreCallValidateCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
3667 const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount,
3668 uint32_t firstInstance, uint32_t stride,
3669 const int32_t* pVertexOffset) const {
3670 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiIndexedEXT");
3671
3672 return skip;
3673}
3674
3675void BestPractices::PostCallRecordCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
3676 const VkMultiDrawIndexedInfoEXT* pIndexInfo, uint32_t instanceCount,
3677 uint32_t firstInstance, uint32_t stride, const int32_t* pVertexOffset) {
3678 StateTracker::PostCallRecordCmdDrawMultiIndexedEXT(commandBuffer, drawCount, pIndexInfo, instanceCount, firstInstance, stride,
3679 pVertexOffset);
3680 uint32_t count = 0;
3681 for (uint32_t i = 0; i < drawCount; ++i) {
3682 count += pIndexInfo[i].indexCount;
3683 }
3684 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiIndexedEXT()");
3685}
3686
3687bool BestPractices::PreCallValidateCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, const VkMultiDrawInfoEXT* pVertexInfo,
3688 uint32_t instanceCount, uint32_t firstInstance, uint32_t stride) const {
3689 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawMultiEXT");
3690
3691 return skip;
3692}
3693
3694void BestPractices::PostCallRecordCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount,
3695 const VkMultiDrawInfoEXT* pVertexInfo, uint32_t instanceCount,
3696 uint32_t firstInstance, uint32_t stride) {
3697 StateTracker::PostCallRecordCmdDrawMultiEXT(commandBuffer, drawCount, pVertexInfo, instanceCount, firstInstance, stride);
3698 uint32_t count = 0;
3699 for (uint32_t i = 0; i < drawCount; ++i) {
3700 count += pVertexInfo[i].vertexCount;
3701 }
3702 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawMultiEXT()");
3703}
3704
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003705void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) {
3706 for (auto descriptor_set : cb_state.validated_descriptor_sets) {
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003707 for (const auto& binding : *descriptor_set) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003708 // For bindless scenarios, we should not attempt to track descriptor set state.
3709 // It is highly uncertain which resources are actually bound.
3710 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003711 if (binding->binding_flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
3712 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003713 continue;
3714 }
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01003715
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003716 for (uint32_t i = 0; i < binding->count; ++i) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003717 VkImageView image_view{VK_NULL_HANDLE};
3718
Jeremy Gebben1b9fdb82022-06-15 15:31:32 -06003719 auto descriptor = binding->GetDescriptor(i);
ziga-lunarg33d806c2022-05-05 17:00:52 +02003720 if (!descriptor) {
3721 continue;
3722 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003723 switch (descriptor->GetClass()) {
3724 case cvdescriptorset::DescriptorClass::Image: {
3725 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
3726 image_view = image_descriptor->GetImageView();
3727 }
3728 break;
3729 }
3730 case cvdescriptorset::DescriptorClass::ImageSampler: {
3731 if (const auto image_sampler_descriptor =
3732 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
3733 image_view = image_sampler_descriptor->GetImageView();
3734 }
3735 break;
3736 }
3737 default:
3738 break;
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01003739 }
3740
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003741 if (image_view) {
3742 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
3743 QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(),
3744 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003745 }
3746 }
3747 }
3748 }
3749}
3750
3751void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3752 uint32_t firstVertex, uint32_t firstInstance) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003753 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3754 ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003755}
3756
3757void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3758 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003759 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3760 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003761}
3762
3763void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3764 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003765 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3766 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003767}
3768
Camden5b184be2019-08-13 07:50:19 -06003769bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003770 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06003771 bool skip = false;
3772
3773 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003774 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
3775 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
3776 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
3777 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06003778 }
3779
3780 return skip;
3781}
Camden83a9c372019-08-14 11:41:38 -06003782
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003783bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
3784 bool skip = false;
3785 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3786 skip |= ValidateCmdEndRenderPass(commandBuffer);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003787 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3788 skip |= ValidateZcullScope(commandBuffer);
3789 }
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003790 return skip;
3791}
3792
3793bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
3794 bool skip = false;
3795 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3796 skip |= ValidateCmdEndRenderPass(commandBuffer);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003797 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3798 skip |= ValidateZcullScope(commandBuffer);
3799 }
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003800 return skip;
3801}
3802
Sam Walls0961ec02020-03-31 16:39:15 +01003803bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3804 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01003805 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003806 skip |= ValidateCmdEndRenderPass(commandBuffer);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03003807 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3808 skip |= ValidateZcullScope(commandBuffer);
3809 }
3810 return skip;
3811}
3812
3813bool BestPractices::PreCallValidateCmdEndRendering(VkCommandBuffer commandBuffer) const {
3814 bool skip = false;
3815 skip |= StateTracker::PreCallValidateCmdEndRendering(commandBuffer);
3816 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3817 skip |= ValidateZcullScope(commandBuffer);
3818 }
3819 return skip;
3820}
3821
3822bool BestPractices::PreCallValidateCmdEndRenderingKHR(VkCommandBuffer commandBuffer) const {
3823 bool skip = false;
3824 skip |= StateTracker::PreCallValidateCmdEndRenderingKHR(commandBuffer);
3825 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
3826 skip |= ValidateZcullScope(commandBuffer);
3827 }
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02003828 return skip;
3829}
3830
3831bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3832 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003833 const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01003834
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003835 if (cmd == nullptr) return skip;
3836 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01003837
LawG4b21485c2022-02-28 13:46:48 +00003838 // Does the number of draw calls classified as depth only surpass the vendor limit for a specified vendor
3839 bool depth_only_arm = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
3840 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
3841 bool depth_only_img = render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsIMG &&
3842 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsIMG;
3843
3844 // Only send the warning when the vendor is enabled and a depth prepass is detected
LawG498ec4502022-04-05 09:08:25 +01003845 bool uses_depth =
3846 (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
LawG45507e142022-04-08 09:36:54 +01003847 ((depth_only_arm && VendorCheckEnabled(kBPVendorArm)) || (depth_only_img && VendorCheckEnabled(kBPVendorIMG)));
LawG4b21485c2022-02-28 13:46:48 +00003848
Sam Walls0961ec02020-03-31 16:39:15 +01003849 if (uses_depth) {
3850 skip |= LogPerformanceWarning(
3851 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
LawG4015be1c2022-03-01 10:37:52 +00003852 "%s %s: Depth pre-passes may be in use. In general, this is not recommended in tile-based deferred "
LawG4b21485c2022-02-28 13:46:48 +00003853 "renderering architectures; such as those in Arm Mali or PowerVR GPUs. Since they can remove geometry "
3854 "hidden by other opaque geometry. Mali has Forward Pixel Killing (FPK), PowerVR has Hiden Surface "
3855 "Remover (HSR) in which case, using depth pre-passes for hidden surface removal may worsen performance.",
3856 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG));
Sam Walls0961ec02020-03-31 16:39:15 +01003857 }
3858
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003859 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
3860
LawG40da9c3d2022-03-01 09:51:01 +00003861 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorIMG)) && rp) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003862 // If we use an attachment on-tile, we should access it in some way. Otherwise,
3863 // it is redundant to have it be part of the render pass.
3864 // Only consider it redundant if it will actually consume bandwidth, i.e.
3865 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
3866 // as is using pure input attachments.
3867 // CLEAR -> STORE might be considered a "useful" thing to do, but
3868 // the optimal thing to do is to defer the clear until you're actually
3869 // going to render to the image.
3870
3871 uint32_t num_attachments = rp->createInfo.attachmentCount;
3872 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02003873 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
3874 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003875 continue;
3876 }
3877
3878 auto& attachment = rp->createInfo.pAttachments[i];
3879
3880 VkImageAspectFlags bandwidth_aspects = 0;
3881
3882 if (!FormatIsStencilOnly(attachment.format) &&
3883 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
3884 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
3885 if (FormatHasDepth(attachment.format)) {
3886 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
3887 } else {
3888 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
3889 }
3890 }
3891
3892 if (FormatHasStencil(attachment.format) &&
3893 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
3894 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
3895 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
3896 }
3897
3898 if (!bandwidth_aspects) {
3899 continue;
3900 }
3901
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003902 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003903 [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003904 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003905 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003906 untouched_aspects &= ~itr->aspects;
3907 }
3908
3909 if (untouched_aspects) {
3910 skip |= LogPerformanceWarning(
3911 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
LawG4015be1c2022-03-01 10:37:52 +00003912 "%s %s: Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003913 "was never accessed by a pipeline or clear command. "
LawG40da9c3d2022-03-01 09:51:01 +00003914 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the "
LawG4015be1c2022-03-01 10:37:52 +00003915 "render pass if the attachments are not intended to be accessed.",
LawG40da9c3d2022-03-01 09:51:01 +00003916 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorIMG), i, attachment.format, untouched_aspects);
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02003917 }
3918 }
3919 }
3920
Sam Walls0961ec02020-03-31 16:39:15 +01003921 return skip;
3922}
3923
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003924void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003925 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3926 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003927}
3928
3929void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003930 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
3931 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003932}
3933
Camden Stocker9c051442019-11-06 14:28:43 -08003934bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
3935 const char* api_name) const {
3936 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003937 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08003938
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003939 if (bp_pd_state) {
3940 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
3941 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
3942 "Potential problem with calling %s() without first retrieving properties from "
3943 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
3944 api_name);
3945 }
Camden Stocker9c051442019-11-06 14:28:43 -08003946 }
3947
3948 return skip;
3949}
3950
Camden83a9c372019-08-14 11:41:38 -06003951bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003952 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06003953 bool skip = false;
3954
Camden Stocker9c051442019-11-06 14:28:43 -08003955 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06003956
Camden Stocker9c051442019-11-06 14:28:43 -08003957 return skip;
3958}
3959
3960bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
3961 uint32_t planeIndex,
3962 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
3963 bool skip = false;
3964
3965 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
3966
3967 return skip;
3968}
3969
3970bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
3971 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
3972 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
3973 bool skip = false;
3974
3975 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06003976
3977 return skip;
3978}
Camden05de2d42019-08-19 10:23:56 -06003979
3980bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003981 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06003982 bool skip = false;
3983
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003984 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06003985
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003986 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06003987 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003988 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07003989 skip |=
3990 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
3991 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
3992 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06003993 }
Camden05de2d42019-08-19 10:23:56 -06003994
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06003995 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
3996 skip |= LogWarning(
3997 device, kVUID_BestPractices_Swapchain_InvalidCount,
3998 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04003999 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06004000 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
4001 }
4002 }
4003
Camden05de2d42019-08-19 10:23:56 -06004004 return skip;
4005}
4006
4007// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06004008bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004009 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004010 const CALL_STATE call_state,
4011 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06004012 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004013 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
4014 if (UNCALLED == call_state) {
4015 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06004016 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004017 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
4018 "recommended "
4019 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
4020 caller_name, caller_name);
4021 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06004022 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
4023 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004024 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
4025 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
4026 ". It is recommended to instead receive all the properties by calling %s with "
4027 "pQueueFamilyPropertyCount that was "
4028 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06004029 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004030 caller_name);
Camden05de2d42019-08-19 10:23:56 -06004031 }
4032
4033 return skip;
4034}
4035
Jeff Bolz5c801d12019-10-09 10:38:45 -05004036bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
4037 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06004038 bool skip = false;
4039
4040 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004041 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06004042 if (!as_state->memory_requirements_checked) {
4043 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
4044 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
4045 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004046 skip |= LogWarning(
4047 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06004048 "vkBindAccelerationStructureMemoryNV(): "
4049 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
4050 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
4051 }
4052 }
4053
4054 return skip;
4055}
4056
Camden05de2d42019-08-19 10:23:56 -06004057bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
4058 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05004059 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004060 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004061 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004062 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004063 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
4064 "vkGetPhysicalDeviceQueueFamilyProperties()");
4065 }
4066 return false;
Camden05de2d42019-08-19 10:23:56 -06004067}
4068
Mike Schuchardt2df08912020-12-15 16:28:09 -08004069bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
4070 uint32_t* pQueueFamilyPropertyCount,
4071 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004072 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004073 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004074 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004075 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
4076 "vkGetPhysicalDeviceQueueFamilyProperties2()");
4077 }
4078 return false;
Camden05de2d42019-08-19 10:23:56 -06004079}
4080
Jeff Bolz5c801d12019-10-09 10:38:45 -05004081bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08004082 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004083 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004084 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004085 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07004086 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
4087 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
4088 }
4089 return false;
Camden05de2d42019-08-19 10:23:56 -06004090}
4091
4092bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
4093 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05004094 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06004095 if (!pSurfaceFormats) return false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004096 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06004097 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06004098 bool skip = false;
4099 if (call_state == UNCALLED) {
4100 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
4101 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004102 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
4103 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
4104 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06004105 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06004106 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004107 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
4108 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
4109 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
4110 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06004111 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06004112 }
4113 }
4114 return skip;
4115}
Camden Stocker23cc47d2019-09-03 14:53:57 -06004116
4117bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05004118 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004119 bool skip = false;
4120
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004121 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
4122 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06004123 // Store sparse binding image_state and after binding is complete make sure that any requiring metadata have it bound
Jeremy Gebbencbf22862021-03-03 12:01:22 -07004124 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004125 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
4126 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07004127 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06004128 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004129 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
4130 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04004131 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004132 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004133 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004134 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06004135 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004136 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
4137 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
4138 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004139 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004140 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
4141 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004142 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004143 }
4144 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004145 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004146 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004147 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004148 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
4149 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004150 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004151 }
4152 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004153 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
4154 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04004155 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004156 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004157 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004158 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06004159 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004160 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
4161 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
4162 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004163 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004164 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
4165 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004166 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004167 }
4168 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004169 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004170 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004171 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004172 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
4173 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004174 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004175 }
4176 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
4177 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004178 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004179 }
4180 }
4181 }
4182 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004183 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
4184 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06004185 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004186 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07004187 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
4188 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004189 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06004190 }
4191 }
4192 }
4193
Rodrigo Locatti7ab778d2022-03-09 18:57:15 -03004194 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4195 auto queue_state = Get<QUEUE_STATE>(queue);
4196 if (queue_state && queue_state->queueFamilyProperties.queueFlags != (VK_QUEUE_TRANSFER_BIT | VK_QUEUE_SPARSE_BINDING_BIT)) {
4197 skip |= LogPerformanceWarning(queue, kVUID_BestPractices_QueueBindSparse_NotAsync,
4198 "vkQueueBindSparse() issued on queue %s. All binds should happen on an asynchronous copy "
4199 "queue to hide the OS scheduling and submit costs.",
4200 report_data->FormatHandle(queue).c_str());
4201 }
4202 }
4203
Camden Stocker23cc47d2019-09-03 14:53:57 -06004204 return skip;
4205}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004206
Mark Lobodzinski84101d72020-04-24 09:43:48 -06004207void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
4208 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07004209 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07004210 return;
4211 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004212
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004213 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
4214 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
4215 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
4216 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04004217 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004218 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004219 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07004220 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05004221 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
4222 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
4223 image_state->sparse_metadata_bound = true;
4224 }
4225 }
4226 }
4227 }
4228}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004229
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004230bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06004231 const VkClearRect* pRects) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004232 if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004233 // We don't know the accurate render area in a secondary,
4234 // so assume we clear the entire frame buffer.
4235 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
4236 return true;
4237 }
4238
4239 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
4240 for (uint32_t i = 0; i < rectCount; i++) {
4241 auto& rect = pRects[i];
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004242 auto& render_area = cmd.activeRenderPassBeginInfo.renderArea;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004243 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
4244 return true;
4245 }
4246 }
4247
4248 return false;
4249}
4250
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004251bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment,
4252 VkImageAspectFlags aspects, bool secondary) const {
4253 const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004254 bool skip = false;
4255
4256 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
4257 return skip;
4258 }
4259
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004260 const auto& rp_state = cmd.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004261
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004262 auto attachment_itr =
4263 std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
4264 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004265
4266 // Only report aspects which haven't been touched yet.
4267 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06004268 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004269 new_aspects &= ~attachment_itr->aspects;
4270 }
4271
4272 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
sjfricke52defd42022-08-08 16:37:46 +09004273 if (!cmd.has_draw_cmd) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004274 skip |= LogPerformanceWarning(
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004275 cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02004276 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
4277 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004278 report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004279 }
4280
4281 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
4282 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
4283 skip |= LogPerformanceWarning(
4284 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
4285 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
4286 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
4287 "it is more efficient.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004288 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004289 }
4290
4291 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
4292 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004293 skip |=
4294 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
4295 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
4296 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
4297 "it is more efficient.",
4298 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004299
4300 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4301 skip |= ValidateZcullScope(cmd.commandBuffer());
4302 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004303 }
4304
4305 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
4306 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004307 skip |=
4308 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
4309 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
4310 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
4311 "it is more efficient.",
4312 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004313 }
4314
4315 return skip;
4316}
4317
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004318bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06004319 const VkClearAttachment* pAttachments, uint32_t rectCount,
4320 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004321 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004322 const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004323 if (!cb_node) return skip;
4324
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02004325 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
4326 // Defer checks to ExecuteCommands.
4327 return skip;
4328 }
4329
4330 // Only care about full clears, partial clears might have legitimate uses.
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004331 const bool is_full_clear = ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004332
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004333 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
4334 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06004335 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004336 if (rp) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004337 if (rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) {
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004338 const auto pColorAttachments = rp->dynamic_rendering_begin_rendering_info.pColorAttachments;
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004339
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004340 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4341 for (uint32_t i = 0; i < attachmentCount; i++) {
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004342 const auto& attachment = pAttachments[i];
4343 if (attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004344 skip |= ValidateZcullScope(commandBuffer);
4345 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004346 if ((attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) && attachment.colorAttachment != VK_ATTACHMENT_UNUSED) {
4347 const auto& color_attachment = pColorAttachments[attachment.colorAttachment];
4348 if (color_attachment.imageView) {
4349 auto image_view_state = Get<IMAGE_VIEW_STATE>(color_attachment.imageView);
4350 const VkFormat format = image_view_state->create_info.format;
4351 skip |= ValidateClearColor(commandBuffer, format, attachment.clearValue.color);
4352 }
4353 }
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004354 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004355 }
4356
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004357 if (is_full_clear) {
4358 // TODO: Implement ValidateClearAttachment for dynamic rendering
4359 }
4360
4361 } else {
4362 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
4363
4364 if (is_full_clear) {
4365 for (uint32_t i = 0; i < attachmentCount; i++) {
4366 const auto& attachment = pAttachments[i];
4367
4368 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
4369 uint32_t color_attachment = attachment.colorAttachment;
4370 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
4371 skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false);
4372 }
4373
4374 if (subpass.pDepthStencilAttachment &&
4375 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
4376 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
4377 skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false);
4378 }
4379 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004380 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004381 if (VendorCheckEnabled(kBPVendorNVIDIA) && rp->createInfo.pAttachments) {
4382 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; ++attachment_idx) {
4383 const auto& attachment = pAttachments[attachment_idx];
4384
4385 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
4386 const uint32_t fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
4387 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
4388 const VkFormat format = rp->createInfo.pAttachments[fb_attachment].format;
4389 skip |= ValidateClearColor(commandBuffer, format, attachment.clearValue.color);
4390 }
4391 }
4392 }
4393 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00004394 }
4395 }
4396
Nadav Gevaf0808442021-05-21 13:51:25 -04004397 if (VendorCheckEnabled(kBPVendorAMD)) {
4398 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
4399 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
4400 bool black_check = false;
4401 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
4402 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
4403 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
4404 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
4405 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
4406
4407 bool white_check = false;
4408 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
4409 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
4410 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
4411 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
4412 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
4413
4414 if (black_check && white_check) {
4415 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
4416 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
4417 "Consider changing to one of the following:"
4418 "RGBA(0, 0, 0, 0) "
4419 "RGBA(0, 0, 0, 1) "
4420 "RGBA(1, 1, 1, 0) "
4421 "RGBA(1, 1, 1, 1)",
4422 VendorSpecificTag(kBPVendorAMD), attachment_idx);
4423 }
4424 } else {
4425 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
4426 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
4427 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
4428 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
4429 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
4430 "attachment %" PRId32 " is not a fast clear value."
4431 "Consider changing to one of the following:"
4432 "D=0.0f, S=0"
4433 "D=1.0f, S=0",
4434 VendorSpecificTag(kBPVendorAMD), attachment_idx);
4435 }
4436 }
4437 }
4438 }
4439
Camden Stockerf55721f2019-09-09 11:04:49 -06004440 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07004441}
Attilio Provenzano02859b22020-02-27 14:17:28 +00004442
4443bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4444 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4445 const VkImageResolve* pRegions) const {
4446 bool skip = false;
4447
4448 skip |= VendorCheckEnabled(kBPVendorArm) &&
4449 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
4450 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
4451 "This is a very slow and extremely bandwidth intensive path. "
4452 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
4453 VendorSpecificTag(kBPVendorArm));
4454
4455 return skip;
4456}
4457
Jeff Leger178b1e52020-10-05 12:22:23 -04004458bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4459 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
4460 bool skip = false;
4461
4462 skip |= VendorCheckEnabled(kBPVendorArm) &&
4463 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
4464 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
4465 "This is a very slow and extremely bandwidth intensive path. "
4466 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
4467 VendorSpecificTag(kBPVendorArm));
4468
4469 return skip;
4470}
4471
Tony-LunarGd36f5f32022-01-20 11:49:59 -07004472bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
4473 const VkResolveImageInfo2* pResolveImageInfo) const {
4474 bool skip = false;
4475
4476 skip |= VendorCheckEnabled(kBPVendorArm) &&
4477 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
4478 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
4479 "This is a very slow and extremely bandwidth intensive path. "
4480 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
4481 VendorSpecificTag(kBPVendorArm));
4482
4483 return skip;
4484}
4485
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004486void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4487 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4488 const VkImageResolve* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004489 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004490 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004491 auto src = Get<bp_state::Image>(srcImage);
4492 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004493
4494 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004495 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
4496 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004497 }
4498}
4499
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01004500void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4501 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004502 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004503 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004504 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
4505 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01004506 uint32_t regionCount = pResolveImageInfo->regionCount;
4507
4508 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004509 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
4510 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01004511 }
4512}
4513
Tony-LunarGd36f5f32022-01-20 11:49:59 -07004514void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
4515 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004516 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07004517 auto& funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004518 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
4519 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07004520 uint32_t regionCount = pResolveImageInfo->regionCount;
4521
4522 for (uint32_t i = 0; i < regionCount; i++) {
4523 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
4524 pResolveImageInfo->pRegions[i].srcSubresource);
4525 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
4526 pResolveImageInfo->pRegions[i].dstSubresource);
4527 }
4528}
4529
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004530void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4531 const VkClearColorValue* pColor, uint32_t rangeCount,
4532 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004533 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004534 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004535 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004536
4537 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004538 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004539 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004540
4541 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4542 RecordClearColor(dst->createInfo.format, *pColor);
4543 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004544}
4545
4546void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4547 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
4548 const VkImageSubresourceRange* pRanges) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004549 ValidationStateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount,
4550 pRanges);
4551
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004552 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004553 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004554 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004555
4556 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004557 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004558 }
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004559 for (uint32_t i = 0; i < rangeCount; i++) {
4560 RecordResetZcullDirection(*cb, image, pRanges[i]);
4561 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004562}
4563
4564void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4565 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4566 const VkImageCopy* pRegions) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004567 ValidationStateTracker::PreCallRecordCmdCopyImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
4568 regionCount, pRegions);
4569
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004570 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004571 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004572 auto src = Get<bp_state::Image>(srcImage);
4573 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004574
4575 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004576 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
4577 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004578 }
4579}
4580
4581void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4582 VkImageLayout dstImageLayout, uint32_t regionCount,
4583 const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004584 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004585 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004586 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004587
4588 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004589 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004590 }
4591}
4592
4593void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4594 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004595 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004596 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004597 auto src = Get<bp_state::Image>(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004598
4599 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004600 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004601 }
4602}
4603
4604void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4605 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4606 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004607 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01004608 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004609 auto src = Get<bp_state::Image>(srcImage);
4610 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004611
4612 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02004613 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
4614 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01004615 }
4616}
4617
Attilio Provenzano02859b22020-02-27 14:17:28 +00004618bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
4619 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
4620 bool skip = false;
4621
4622 if (VendorCheckEnabled(kBPVendorArm)) {
4623 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
4624 skip |= LogPerformanceWarning(
4625 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
4626 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
4627 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
4628 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06004629 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00004630 }
4631
4632 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
4633 skip |= LogPerformanceWarning(
4634 device, kVUID_BestPractices_CreateSampler_LodClamping,
4635 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
4636 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
4637 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
4638 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
4639 }
4640
4641 if (pCreateInfo->mipLodBias != 0.0f) {
4642 skip |=
4643 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
4644 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
4645 "descriptors being created and may cause reduced performance.",
4646 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
4647 }
4648
4649 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
4650 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
4651 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
4652 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
4653 skip |= LogPerformanceWarning(
4654 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
4655 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
4656 "This will lead to less efficient descriptors being created and may cause reduced performance. "
4657 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
4658 VendorSpecificTag(kBPVendorArm));
4659 }
4660
4661 if (pCreateInfo->unnormalizedCoordinates) {
4662 skip |= LogPerformanceWarning(
4663 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
4664 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
4665 "descriptors being created and may cause reduced performance.",
4666 VendorSpecificTag(kBPVendorArm));
4667 }
4668
4669 if (pCreateInfo->anisotropyEnable) {
4670 skip |= LogPerformanceWarning(
4671 device, kVUID_BestPractices_CreateSampler_Anisotropy,
4672 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
4673 "and may cause reduced performance.",
4674 VendorSpecificTag(kBPVendorArm));
4675 }
4676 }
4677
4678 return skip;
4679}
Sam Walls8e77e4f2020-03-16 20:47:40 +00004680
Nadav Gevaf0808442021-05-21 13:51:25 -04004681void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
4682 const VkGraphicsPipelineCreateInfo* pCreateInfos,
4683 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
4684 void* cgpl_state) {
4685 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
4686 pPipelines);
4687 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004688 num_pso_ += createInfoCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04004689}
4690
4691bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
4692 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
4693 const VkCopyDescriptorSet* pDescriptorCopies) const {
4694 bool skip = false;
4695 if (VendorCheckEnabled(kBPVendorAMD)) {
4696 if (descriptorCopyCount > 0) {
4697 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
4698 "%s Performance warning: copying descriptor sets is not recommended",
4699 VendorSpecificTag(kBPVendorAMD));
4700 }
4701 }
4702
4703 return skip;
4704}
4705
4706bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
4707 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
4708 const VkAllocationCallbacks* pAllocator,
4709 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
4710 bool skip = false;
4711 if (VendorCheckEnabled(kBPVendorAMD)) {
4712 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
4713 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
4714 "vkUpdateDescriptorSet instead",
4715 VendorSpecificTag(kBPVendorAMD));
4716 }
4717
4718 return skip;
4719}
4720
4721bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4722 const VkClearColorValue* pColor, uint32_t rangeCount,
4723 const VkImageSubresourceRange* pRanges) const {
4724 bool skip = false;
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004725
4726 auto dst = Get<bp_state::Image>(image);
4727
Nadav Gevaf0808442021-05-21 13:51:25 -04004728 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08004729 skip |= LogPerformanceWarning(
4730 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04004731 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
4732 "vkCmdClearAttachments instead",
4733 VendorSpecificTag(kBPVendorAMD));
4734 }
Rodrigo Locattie4c08a02022-04-04 18:12:18 -03004735 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4736 skip |= ValidateClearColor(commandBuffer, dst->createInfo.format, *pColor);
4737 }
Nadav Gevaf0808442021-05-21 13:51:25 -04004738
4739 return skip;
4740}
4741
4742bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4743 VkImageLayout imageLayout,
4744 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
4745 const VkImageSubresourceRange* pRanges) const {
4746 bool skip = false;
4747 if (VendorCheckEnabled(kBPVendorAMD)) {
4748 skip |= LogPerformanceWarning(
4749 device, kVUID_BestPractices_ClearAttachment_ClearImage,
4750 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
4751 "vkCmdClearAttachments instead",
4752 VendorSpecificTag(kBPVendorAMD));
4753 }
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004754 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4755 for (uint32_t i = 0; i < rangeCount; i++) {
4756 skip |= ValidateZcull(commandBuffer, image, pRanges[i]);
4757 }
4758 }
Nadav Gevaf0808442021-05-21 13:51:25 -04004759
4760 return skip;
4761}
4762
4763bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
4764 const VkAllocationCallbacks* pAllocator,
4765 VkPipelineLayout* pPipelineLayout) const {
4766 bool skip = false;
4767 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004768 uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2;
Nadav Gevaf0808442021-05-21 13:51:25 -04004769 // Descriptor sets cost 1 DWORD each.
4770 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
4771 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
4772 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
4773 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
4774 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004775 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004776 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size;
Nadav Gevaf0808442021-05-21 13:51:25 -04004777 }
4778
4779 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
4780 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
4781 }
4782
4783 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
4784 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
4785 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
4786 "Descriptor sets cost 1 DWORD each. "
4787 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
4788 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
4789 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
4790 VendorSpecificTag(kBPVendorAMD));
4791 }
4792 }
4793
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004794 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4795 bool has_separate_sampler = false;
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004796 size_t fast_space_usage = 0;
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004797
4798 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; ++i) {
4799 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
4800 for (const auto& binding : descriptor_set_layout_state->GetBindings()) {
4801 if (binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) {
4802 has_separate_sampler = true;
4803 }
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004804
4805 if ((descriptor_set_layout_state->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) == 0U) {
4806 size_t descriptor_type_size = 0;
4807
4808 switch (binding.descriptorType) {
4809 case VK_DESCRIPTOR_TYPE_SAMPLER:
4810 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
4811 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
4812 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
4813 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
4814 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
4815 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
4816 descriptor_type_size = 4;
4817 break;
4818 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
4819 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
4820 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
4821 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
4822 descriptor_type_size = 8;
4823 break;
4824 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
4825 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
4826 case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE:
4827 descriptor_type_size = 16;
4828 break;
4829 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
4830 descriptor_type_size = 1;
4831 default:
4832 // Unknown type.
4833 break;
4834 }
4835
4836 size_t descriptor_size = descriptor_type_size * binding.descriptorCount;
4837 fast_space_usage += descriptor_size;
4838 }
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004839 }
4840 }
4841
4842 if (has_separate_sampler) {
4843 skip |= LogPerformanceWarning(
4844 device, kVUID_BestPractices_CreatePipelineLayout_SeparateSampler,
4845 "%s Consider using combined image samplers instead of separate samplers for marginally better performance.",
4846 VendorSpecificTag(kBPVendorNVIDIA));
4847 }
Rodrigo Locatti12f6ffc2022-03-15 18:29:11 -03004848
4849 if (fast_space_usage > kPipelineLayoutFastDescriptorSpaceNVIDIA) {
4850 skip |= LogPerformanceWarning(
4851 device, kVUID_BestPractices_CreatePipelinesLayout_LargePipelineLayout,
4852 "%s Pipeline layout size is too large, prefer using pipeline-specific descriptor set layouts. "
4853 "Aim for consuming less than %" PRIu32 " bytes to allow fast reads for all non-bindless descriptors. "
4854 "Samplers, textures, texel buffers, and combined image samplers consume 4 bytes each. "
4855 "Uniform buffers and acceleration structures consume 8 bytes. "
4856 "Storage buffers consume 16 bytes. "
4857 "Push constants do not consume space.",
4858 VendorSpecificTag(kBPVendorNVIDIA), kPipelineLayoutFastDescriptorSpaceNVIDIA);
4859 }
Rodrigo Locatti65b33832022-03-15 17:57:30 -03004860 }
4861
Nadav Gevaf0808442021-05-21 13:51:25 -04004862 return skip;
4863}
4864
4865bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4866 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4867 const VkImageCopy* pRegions) const {
4868 bool skip = false;
4869 std::stringstream src_image_hex;
4870 std::stringstream dst_image_hex;
4871 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
4872 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
4873
4874 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004875 auto src_state = Get<IMAGE_STATE>(srcImage);
4876 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04004877
4878 if (src_state && dst_state) {
4879 VkImageTiling src_Tiling = src_state->createInfo.tiling;
4880 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
4881 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
4882 skip |=
4883 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
4884 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
4885 "image (vkCmdCopyImageToBuffer) "
4886 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
4887 "copies when converting between linear and optimal images",
4888 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
4889 }
4890 }
4891 }
4892
4893 return skip;
4894}
4895
4896bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
4897 VkPipeline pipeline) const {
4898 bool skip = false;
4899
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03004900 auto cb = Get<bp_state::CommandBuffer>(commandBuffer);
4901
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004902 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004903 if (IsPipelineUsedInFrame(pipeline)) {
Nadav Gevaf0808442021-05-21 13:51:25 -04004904 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004905 "%s %s Performance warning: Pipeline %s was bound twice in the frame. "
4906 "Keep pipeline state changes to a minimum, for example, by sorting draw calls by pipeline.",
4907 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA),
4908 report_data->FormatHandle(pipeline).c_str());
Nadav Gevaf0808442021-05-21 13:51:25 -04004909 }
4910 }
Rodrigo Locattia5eaf6e2022-04-01 18:05:23 -03004911 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4912 const auto& tgm = cb->nv.tess_geometry_mesh;
4913 if (tgm.num_switches >= kNumBindPipelineTessGeometryMeshSwitchesThresholdNVIDIA && !tgm.threshold_signaled) {
4914 LogPerformanceWarning(commandBuffer, kVUID_BestPractices_BindPipeline_SwitchTessGeometryMesh,
4915 "%s Avoid switching between pipelines with and without tessellation, geometry, task, "
4916 "and/or mesh shaders. Group draw calls using these shader stages together.",
4917 VendorSpecificTag(kBPVendorNVIDIA));
4918 // Do not set 'skip' so the number of switches gets properly counted after the message.
4919 }
4920 }
4921
Nadav Gevaf0808442021-05-21 13:51:25 -04004922 return skip;
4923}
4924
4925void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
4926 VkFence fence, VkResult result) {
4927 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004928 num_queue_submissions_ += submitCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04004929}
4930
4931bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
4932 bool skip = false;
4933
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004934 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004935 auto num = num_queue_submissions_.load();
4936 if (num > kNumberOfSubmissionWarningLimitAMD) {
4937 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004938 "%s %s Performance warning: command buffers submitted %" PRId32
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004939 " times this frame. Submitting command buffers has a CPU "
4940 "and GPU overhead. Submit fewer times to incur less overhead.",
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004941 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04004942 }
4943 }
4944
4945 return skip;
4946}
4947
4948void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
4949 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
4950 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
4951 uint32_t bufferMemoryBarrierCount,
4952 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
4953 uint32_t imageMemoryBarrierCount,
4954 const VkImageMemoryBarrier* pImageMemoryBarriers) {
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004955 ValidationStateTracker::PostCallRecordCmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
4956 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4957 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4958
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004959 num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount);
Rodrigo Locatti0b16cda2022-04-01 17:59:53 -03004960
4961 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
4962 RecordCmdPipelineBarrierImageBarrier(commandBuffer, pImageMemoryBarriers[i]);
4963 }
4964}
4965
4966void BestPractices::PostCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
4967 ValidationStateTracker::PostCallRecordCmdPipelineBarrier2(commandBuffer, pDependencyInfo);
4968
4969 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) {
4970 RecordCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]);
4971 }
4972}
4973
4974void BestPractices::PostCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
4975 ValidationStateTracker::PostCallRecordCmdPipelineBarrier2KHR(commandBuffer, pDependencyInfo);
4976
4977 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; ++i) {
4978 RecordCmdPipelineBarrierImageBarrier(commandBuffer, pDependencyInfo->pImageMemoryBarriers[i]);
4979 }
4980}
4981
4982template <typename ImageMemoryBarrier>
4983void BestPractices::RecordCmdPipelineBarrierImageBarrier(VkCommandBuffer commandBuffer, const ImageMemoryBarrier& barrier) {
4984 auto cb = Get<bp_state::CommandBuffer>(commandBuffer);
4985 assert(cb);
4986
4987 if (VendorCheckEnabled(kBPVendorNVIDIA)) {
4988 RecordResetZcullDirection(*cb, barrier.image, barrier.subresourceRange);
4989 }
Nadav Gevaf0808442021-05-21 13:51:25 -04004990}
4991
4992bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
4993 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
4994 bool skip = false;
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004995 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07004996 if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04004997 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03004998 "%s %s Performance warning: High number of vkSemaphore objects created. "
Nadav Gevaf0808442021-05-21 13:51:25 -04004999 "Minimize the amount of queue synchronization that is used. "
5000 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
Rodrigo Locatti494e4482022-03-30 16:37:40 -03005001 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04005002 }
5003 }
5004
5005 return skip;
5006}
5007
5008bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
5009 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
5010 bool skip = false;
Rodrigo Locatti494e4482022-03-30 16:37:40 -03005011 if (VendorCheckEnabled(kBPVendorAMD) || VendorCheckEnabled(kBPVendorNVIDIA)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005012 if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04005013 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
Rodrigo Locatti494e4482022-03-30 16:37:40 -03005014 "%s %s Performance warning: High number of VkFence objects created."
Nadav Gevaf0808442021-05-21 13:51:25 -04005015 "Minimize the amount of CPU-GPU synchronization that is used. "
Rodrigo Locatti494e4482022-03-30 16:37:40 -03005016 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
5017 VendorSpecificTag(kBPVendorAMD), VendorSpecificTag(kBPVendorNVIDIA));
Nadav Gevaf0808442021-05-21 13:51:25 -04005018 }
5019 }
5020
5021 return skip;
5022}
5023
Sam Walls8e77e4f2020-03-16 20:47:40 +00005024void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
5025
5026bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
5027 // look for a cache hit
5028 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
5029 if (hit != _entries.end()) {
5030 // mark the cache hit as being most recently used
5031 hit->age = iteration++;
5032 return true;
5033 }
5034
5035 // if there's no cache hit, we need to model the entry being inserted into the cache
5036 CacheEntry new_entry = {value, iteration};
5037 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
5038 // if there is still space left in the cache, use the next available slot
5039 *(_entries.begin() + iteration) = new_entry;
5040 } else {
5041 // otherwise replace the least recently used cache entry
5042 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
5043 *lru = new_entry;
5044 }
5045 iteration++;
5046 return false;
5047}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005048
5049bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
5050 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005051 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005052 bool skip = false;
5053 if (swapchain_data && swapchain_data->images.size() == 0) {
5054 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
5055 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
5056 "vkGetSwapchainImagesKHR after swapchain creation.");
5057 }
5058 return skip;
5059}
5060
Nathaniel Cesario56a96652020-12-30 13:23:42 -07005061void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
5062 if (no_pointer) {
5063 if (UNCALLED == call_state) {
5064 call_state = QUERY_COUNT;
5065 }
5066 } else { // Save queue family properties
5067 call_state = QUERY_DETAILS;
5068 }
5069}
5070
Nathaniel Cesariof121d122020-10-08 13:09:46 -06005071void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5072 uint32_t* pQueueFamilyPropertyCount,
5073 VkQueueFamilyProperties* pQueueFamilyProperties) {
5074 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
5075 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005076 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005077 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07005078 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
5079 nullptr == pQueueFamilyProperties);
5080 }
5081}
5082
5083void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
5084 uint32_t* pQueueFamilyPropertyCount,
5085 VkQueueFamilyProperties2* pQueueFamilyProperties) {
5086 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
5087 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005088 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07005089 if (bp_pd_state) {
5090 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
5091 nullptr == pQueueFamilyProperties);
5092 }
5093}
5094
5095void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
5096 uint32_t* pQueueFamilyPropertyCount,
5097 VkQueueFamilyProperties2* pQueueFamilyProperties) {
5098 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
5099 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005100 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07005101 if (bp_pd_state) {
5102 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
5103 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005104 }
5105}
5106
Nathaniel Cesariof121d122020-10-08 13:09:46 -06005107void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
5108 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005109 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005110 if (bp_pd_state) {
5111 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
5112 }
5113}
5114
Nathaniel Cesariof121d122020-10-08 13:09:46 -06005115void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
5116 VkPhysicalDeviceFeatures2* pFeatures) {
5117 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005118 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005119 if (bp_pd_state) {
5120 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
5121 }
5122}
5123
Nathaniel Cesariof121d122020-10-08 13:09:46 -06005124void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
5125 VkPhysicalDeviceFeatures2* pFeatures) {
5126 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005127 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005128 if (bp_pd_state) {
5129 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
5130 }
5131}
5132
5133void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
5134 VkSurfaceKHR surface,
5135 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
5136 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005137 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005138 if (bp_pd_state) {
5139 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
5140 }
5141}
5142
5143void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
5144 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
5145 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005146 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005147 if (bp_pd_state) {
5148 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
5149 }
5150}
5151
5152void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
5153 VkSurfaceKHR surface,
5154 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
5155 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005156 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005157 if (bp_pd_state) {
5158 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
5159 }
5160}
5161
5162void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
5163 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
5164 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005165 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005166 if (bp_pd_data) {
5167 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
5168
5169 if (*pPresentModeCount) {
5170 if (call_state < QUERY_COUNT) {
5171 call_state = QUERY_COUNT;
5172 }
5173 }
5174 if (pPresentModes) {
5175 if (call_state < QUERY_DETAILS) {
5176 call_state = QUERY_DETAILS;
5177 }
5178 }
5179 }
5180}
5181
5182void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
5183 uint32_t* pSurfaceFormatCount,
5184 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005185 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005186 if (bp_pd_data) {
5187 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
5188
5189 if (*pSurfaceFormatCount) {
5190 if (call_state < QUERY_COUNT) {
5191 call_state = QUERY_COUNT;
5192 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06005193 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005194 }
5195 if (pSurfaceFormats) {
5196 if (call_state < QUERY_DETAILS) {
5197 call_state = QUERY_DETAILS;
5198 }
5199 }
5200 }
5201}
5202
5203void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
5204 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
5205 uint32_t* pSurfaceFormatCount,
5206 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005207 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005208 if (bp_pd_data) {
5209 if (*pSurfaceFormatCount) {
5210 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
5211 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
5212 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06005213 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005214 }
5215 if (pSurfaceFormats) {
5216 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
5217 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
5218 }
5219 }
5220 }
5221}
5222
5223void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
5224 uint32_t* pPropertyCount,
5225 VkDisplayPlanePropertiesKHR* pProperties,
5226 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005227 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005228 if (bp_pd_data) {
5229 if (*pPropertyCount) {
5230 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
5231 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
5232 }
5233 }
5234 if (pProperties) {
5235 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
5236 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
5237 }
5238 }
5239 }
5240}
5241
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005242void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
5243 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
5244 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005245 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06005246 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
5247 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
5248 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005249 }
5250 }
5251}
5252
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01005253void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
5254 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
5255
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06005256 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01005257 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02005258 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01005259 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07005260 auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01005261 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07005262 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01005263 }
5264 }
5265 }
5266}