blob: dc6c723728496faa140d1aac147abfc208793b8a [file] [log] [blame]
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -07001/* Copyright (c) 2015-2020 The Khronos Group Inc.
2 * Copyright (c) 2015-2020 Valve Corporation
3 * Copyright (c) 2015-2020 LunarG, Inc.
Camdeneaa86ea2019-07-26 11:00:09 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: Camden Stocker <camden@lunarg.com>
18 */
19
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070020#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060021#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060022#include "best_practices_error_enums.h"
Camden5b184be2019-08-13 07:50:19 -060023
24#include <string>
25#include <iomanip>
26
27// get the API name is proper format
Jeff Bolz46c0ea02019-10-09 13:06:29 -050028std::string BestPractices::GetAPIVersionName(uint32_t version) const {
Camden5b184be2019-08-13 07:50:19 -060029 std::stringstream version_name;
30 uint32_t major = VK_VERSION_MAJOR(version);
31 uint32_t minor = VK_VERSION_MINOR(version);
32 uint32_t patch = VK_VERSION_PATCH(version);
33
34 version_name << major << "." << minor << "." << patch << " (0x" << std::setfill('0') << std::setw(8) << std::hex << version
35 << ")";
36
37 return version_name.str();
38}
39
Attilio Provenzano19d6a982020-02-27 12:41:41 +000040struct VendorSpecificInfo {
41 bool CHECK_ENABLED::*check;
42 std::string name;
43};
44
45const std::map<BPVendorFlagBits, VendorSpecificInfo> vendor_info = {
46 {kBPVendorArm, {&CHECK_ENABLED::vendor_specific_arm, "Arm"}},
47};
48
49bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
50 for (const auto& vendor : vendor_info) {
51 if (vendors & vendor.first && enabled.*(vendor.second.check)) {
52 return true;
53 }
54 }
55 return false;
56}
57
58const char* VendorSpecificTag(BPVendorFlags vendors) {
59 // Cache built vendor tags in a map
60 static std::unordered_map<BPVendorFlags, std::string> tag_map;
61
62 auto res = tag_map.find(vendors);
63 if (res == tag_map.end()) {
64 // Build the vendor tag string
65 std::stringstream vendor_tag;
66
67 vendor_tag << "[";
68 bool first_vendor = true;
69 for (const auto& vendor : vendor_info) {
70 if (vendors & vendor.first) {
71 if (!first_vendor) {
72 vendor_tag << ", ";
73 }
74 vendor_tag << vendor.second.name;
75 first_vendor = false;
76 }
77 }
78 vendor_tag << "]";
79
80 tag_map[vendors] = vendor_tag.str();
81 res = tag_map.find(vendors);
82 }
83
84 return res->second.c_str();
85}
86
Mark Lobodzinski6167e102020-02-24 17:03:55 -070087const char* DepReasonToString(ExtDeprecationReason reason) {
88 switch (reason) {
89 case kExtPromoted:
90 return "promoted to";
91 break;
92 case kExtObsoleted:
93 return "obsoleted by";
94 break;
95 case kExtDeprecated:
96 return "deprecated by";
97 break;
98 default:
99 return "";
100 break;
101 }
102}
103
104bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
105 const char* vuid) const {
106 bool skip = false;
107 auto dep_info_it = deprecated_extensions.find(extension_name);
108 if (dep_info_it != deprecated_extensions.end()) {
109 auto dep_info = dep_info_it->second;
110 if ((dep_info.target.compare("VK_VERSION_1_1") && (version >= VK_VERSION_1_1)) ||
111 (dep_info.target.compare("VK_VERSION_1_2") && (version >= VK_VERSION_1_2))) {
112 skip |=
113 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
114 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
115 } else if (!dep_info.target.find("VK_VERSION")) {
116 if (dep_info.target.length() == 0) {
117 skip |= LogWarning(instance, vuid,
118 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
119 "without replacement.",
120 api_name, extension_name);
121 } else {
122 skip |= LogWarning(instance, vuid,
123 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
124 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
125 }
126 }
127 }
128 return skip;
129}
130
Camden5b184be2019-08-13 07:50:19 -0600131bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500132 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600133 bool skip = false;
134
135 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
136 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800137 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700138 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
139 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600140 }
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700141 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i],
142 pCreateInfo->pApplicationInfo->apiVersion,
143 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Camden5b184be2019-08-13 07:50:19 -0600144 }
145
146 return skip;
147}
148
149void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
150 VkInstance* pInstance) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700151 ValidationStateTracker::PreCallRecordCreateInstance(pCreateInfo, pAllocator, pInstance);
Camden5b184be2019-08-13 07:50:19 -0600152 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
153}
154
155bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500156 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600157 bool skip = false;
158
159 // get API version of physical device passed when creating device.
160 VkPhysicalDeviceProperties physical_device_properties{};
161 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500162 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600163
164 // check api versions and warn if instance api Version is higher than version on device.
165 if (instance_api_version > device_api_version) {
166 std::string inst_api_name = GetAPIVersionName(instance_api_version);
167 std::string dev_api_name = GetAPIVersionName(device_api_version);
168
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700169 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
170 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
171 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600172 }
173
174 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
175 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800176 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700177 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
178 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600179 }
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700180 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], instance_api_version,
181 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Camden5b184be2019-08-13 07:50:19 -0600182 }
183
Camden83a9c372019-08-14 11:41:38 -0600184 auto pd_state = GetPhysicalDeviceState(physicalDevice);
Corta48da1d2019-09-20 18:59:07 +0200185 if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700186 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
187 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600188 }
189
Camden5b184be2019-08-13 07:50:19 -0600190 return skip;
191}
192
193bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500194 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600195 bool skip = false;
196
197 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
198 std::stringstream bufferHex;
199 bufferHex << "0x" << std::hex << HandleToUint64(pBuffer);
200
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700201 skip |= LogWarning(
202 device, kVUID_BestPractices_SharingModeExclusive,
203 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
204 "(queueFamilyIndexCount of %" PRIu32 ").",
205 bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600206 }
207
208 return skip;
209}
210
211bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500212 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600213 bool skip = false;
214
215 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
216 std::stringstream imageHex;
217 imageHex << "0x" << std::hex << HandleToUint64(pImage);
218
219 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700220 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
221 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
222 "(queueFamilyIndexCount of %" PRIu32 ").",
223 imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600224 }
225
226 return skip;
227}
228
229bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500230 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600231 bool skip = false;
232
Camden83a9c372019-08-14 11:41:38 -0600233 auto physical_device_state = GetPhysicalDeviceState();
234
235 if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700236 skip |= LogWarning(
237 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600238 "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
239 }
240
241 if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700242 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
243 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
244 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600245 }
246
247 if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700248 skip |= LogWarning(
249 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
250 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
Camden83a9c372019-08-14 11:41:38 -0600251 }
252
Camden5b184be2019-08-13 07:50:19 -0600253 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700254 skip |=
255 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
256 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while "
257 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
258 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600259 }
260
261 return skip;
262}
263
264bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
265 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500266 const VkAllocationCallbacks* pAllocator,
267 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600268 bool skip = false;
269
270 for (uint32_t i = 0; i < swapchainCount; i++) {
271 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700272 skip |= LogWarning(
273 device, kVUID_BestPractices_SharingModeExclusive,
274 "Warning: A shared swapchain (index %" PRIu32
275 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
276 "queues (queueFamilyIndexCount of %" PRIu32 ").",
277 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600278 }
279 }
280
281 return skip;
282}
283
284bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500285 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600286 bool skip = false;
287
288 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
289 VkFormat format = pCreateInfo->pAttachments[i].format;
290 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
291 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
292 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700293 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
294 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
295 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
296 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
297 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600298 }
299 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700300 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
301 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
302 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
303 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
304 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600305 }
306 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000307
308 const auto& attachment = pCreateInfo->pAttachments[i];
309 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
310 bool access_requires_memory =
311 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
312
313 if (FormatHasStencil(format)) {
314 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
315 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
316 }
317
318 if (access_requires_memory) {
319 skip |= LogPerformanceWarning(
320 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
321 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
322 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
323 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
324 i, static_cast<uint32_t>(attachment.samples));
325 }
326 }
Camden5b184be2019-08-13 07:50:19 -0600327 }
328
329 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
330 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
331 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
332 }
333
334 return skip;
335}
336
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000337bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
338 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
339 bool skip = false;
340
341 // Check for non-transient attachments that should be transient and vice versa
342 auto rp_state = GetRenderPassState(pCreateInfo->renderPass);
343 if (rp_state) {
344 const VkRenderPassCreateInfo2* rpci = rp_state->createInfo.ptr();
345 const VkImageView* image_views = pCreateInfo->pAttachments;
346
347 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
348 auto& attachment = rpci->pAttachments[i];
349 bool attachment_should_be_transient =
350 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
351
352 if (FormatHasStencil(attachment.format)) {
353 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
354 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
355 }
356
357 auto view_state = GetImageViewState(image_views[i]);
358 if (view_state) {
359 auto& ivci = view_state->create_info;
360 auto& ici = GetImageState(ivci.image)->createInfo;
361
362 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
363
364 // The check for an image that should not be transient applies to all GPUs
365 if (!attachment_should_be_transient && image_is_transient) {
366 skip |= LogPerformanceWarning(
367 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
368 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
369 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
370 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
371 i);
372 }
373
374 bool supports_lazy = false;
375 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
376 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
377 supports_lazy = true;
378 }
379 }
380
381 // The check for an image that should be transient only applies to GPUs supporting
382 // lazily allocated memory
383 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
384 skip |= LogPerformanceWarning(
385 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
386 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
387 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
388 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
389 i);
390 }
391 }
392 }
393 }
394
395 return skip;
396}
397
Camden5b184be2019-08-13 07:50:19 -0600398bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500399 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600400 bool skip = false;
401
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500402 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700403 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
404 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600405 }
406
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000407 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
408 skip |= LogPerformanceWarning(
409 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
410 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %llu. This is a very small allocation (current "
411 "threshold is %llu bytes). "
412 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
413 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
414 }
415
Camden83a9c372019-08-14 11:41:38 -0600416 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
417
418 return skip;
419}
420
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500421void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
422 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
423 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700424 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700425 if (result != VK_SUCCESS) {
426 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
427 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
428 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR};
429 static std::vector<VkResult> success_codes = {};
430 ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes);
431 return;
432 }
433 num_mem_objects++;
434}
Camden Stocker9738af92019-10-16 13:54:03 -0700435
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700436void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& success_codes,
437 const std::vector<VkResult>& error_codes) const {
438 auto error = std::find(error_codes.begin(), error_codes.end(), result);
439 if (error != error_codes.end()) {
440 LogWarning(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
441 return;
442 }
443 auto success = std::find(success_codes.begin(), success_codes.end(), result);
444 if (success != success_codes.end()) {
445 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned non-success return code %s.", api_name,
446 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500447 }
448}
449
Jeff Bolz5c801d12019-10-09 10:38:45 -0500450bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
451 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700452 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600453 bool skip = false;
454
Camden Stocker9738af92019-10-16 13:54:03 -0700455 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600456
457 for (auto& obj : mem_info->obj_bindings) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700458 skip |= LogWarning(device, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
459 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem).c_str());
Camden83a9c372019-08-14 11:41:38 -0600460 }
461
Camden5b184be2019-08-13 07:50:19 -0600462 return skip;
463}
464
465void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -0700466 ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
Camden5b184be2019-08-13 07:50:19 -0600467 if (memory != VK_NULL_HANDLE) {
468 num_mem_objects--;
469 }
470}
471
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000472bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600473 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500474 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600475
sfricke-samsunge2441192019-11-06 14:07:57 -0800476 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700477 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
478 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
479 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600480 }
481
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000482 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
483
484 if (mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
485 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
486 skip |= LogPerformanceWarning(
487 device, kVUID_BestPractices_SmallDedicatedAllocation,
488 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
489 "The required size of the allocation is %llu, but smaller buffers like this should be sub-allocated from "
490 "larger memory blocks. (Current threshold is %llu bytes.)",
491 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
492 }
493
Camden Stockerb603cc82019-09-03 10:09:02 -0600494 return skip;
495}
496
497bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500498 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600499 bool skip = false;
500 const char* api_name = "BindBufferMemory()";
501
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000502 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600503
504 return skip;
505}
506
507bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500508 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600509 char api_name[64];
510 bool skip = false;
511
512 for (uint32_t i = 0; i < bindInfoCount; i++) {
513 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000514 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600515 }
516
517 return skip;
518}
Camden Stockerb603cc82019-09-03 10:09:02 -0600519
520bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500521 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600522 char api_name[64];
523 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600524
Camden Stocker8b798ab2019-09-03 10:33:28 -0600525 for (uint32_t i = 0; i < bindInfoCount; i++) {
526 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000527 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600528 }
529
530 return skip;
531}
532
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000533bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600534 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500535 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600536
sfricke-samsunge2441192019-11-06 14:07:57 -0800537 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700538 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
539 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
540 api_name, report_data->FormatHandle(image).c_str());
Camden Stocker8b798ab2019-09-03 10:33:28 -0600541 }
542
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000543 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
544
545 if (mem_state->alloc_info.allocationSize == image_state->requirements.size &&
546 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
547 skip |= LogPerformanceWarning(
548 device, kVUID_BestPractices_SmallDedicatedAllocation,
549 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
550 "The required size of the allocation is %llu, but smaller images like this should be sub-allocated from "
551 "larger memory blocks. (Current threshold is %llu bytes.)",
552 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
553 }
554
555 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
556 // make sure this type is actually used.
557 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
558 // (i.e.most tile - based renderers)
559 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
560 bool supports_lazy = false;
561 uint32_t suggested_type = 0;
562
563 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
564 if ((1u << i) & image_state->requirements.memoryTypeBits) {
565 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
566 supports_lazy = true;
567 suggested_type = i;
568 break;
569 }
570 }
571 }
572
573 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
574
575 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
576 skip |= LogPerformanceWarning(
577 device, kVUID_BestPractices_NonLazyTransientImage,
578 "%s: Attempting to bind memory type % u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
579 "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
580 "%llu bytes of physical memory.",
581 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements.size);
582 }
583 }
584
Camden Stocker8b798ab2019-09-03 10:33:28 -0600585 return skip;
586}
587
588bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500589 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600590 bool skip = false;
591 const char* api_name = "vkBindImageMemory()";
592
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000593 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600594
595 return skip;
596}
597
598bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500599 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600600 char api_name[64];
601 bool skip = false;
602
603 for (uint32_t i = 0; i < bindInfoCount; i++) {
604 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000605 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600606 }
607
608 return skip;
609}
610
611bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500612 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600613 char api_name[64];
614 bool skip = false;
615
616 for (uint32_t i = 0; i < bindInfoCount; i++) {
617 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000618 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600619 }
620
621 return skip;
622}
Camden83a9c372019-08-14 11:41:38 -0600623
Camden5b184be2019-08-13 07:50:19 -0600624bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
625 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600626 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500627 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600628 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
629 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600630
631 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700632 skip |= LogPerformanceWarning(
633 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
634 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
635 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600636 }
637
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000638 for (uint32_t i = 0; i < createInfoCount; i++) {
639 auto& createInfo = pCreateInfos[i];
640
641 auto& vertexInput = *createInfo.pVertexInputState;
642 uint32_t count = 0;
643 for (uint32_t j = 0; j < vertexInput.vertexBindingDescriptionCount; j++) {
644 if (vertexInput.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
645 count++;
646 }
647 }
648
649 if (count > kMaxInstancedVertexBuffers) {
650 skip |= LogPerformanceWarning(
651 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
652 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
653 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
654 count, kMaxInstancedVertexBuffers);
655 }
656 }
657
Camden5b184be2019-08-13 07:50:19 -0600658 return skip;
659}
660
661bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
662 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600663 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500664 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600665 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
666 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600667
668 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700669 skip |= LogPerformanceWarning(
670 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
671 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
672 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600673 }
674
675 return skip;
676}
677
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500678bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600679 bool skip = false;
680
681 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700682 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
683 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600684 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700685 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
686 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600687 }
688
689 return skip;
690}
691
Jeff Bolz5c801d12019-10-09 10:38:45 -0500692bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
693 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600694 bool skip = false;
695
696 for (uint32_t submit = 0; submit < submitCount; submit++) {
697 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
698 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
699 }
700 }
701
702 return skip;
703}
704
Attilio Provenzano746e43e2020-02-27 11:23:50 +0000705bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
706 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
707 bool skip = false;
708
709 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
710 skip |= LogPerformanceWarning(
711 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
712 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
713 "pool instead.");
714 }
715
716 return skip;
717}
718
719bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
720 const VkCommandBufferBeginInfo* pBeginInfo) const {
721 bool skip = false;
722
723 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
724 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
725 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
726 }
727
728 return skip;
729}
730
Jeff Bolz5c801d12019-10-09 10:38:45 -0500731bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600732 bool skip = false;
733
734 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
735
736 return skip;
737}
738
Jeff Bolz5c801d12019-10-09 10:38:45 -0500739bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
740 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600741 bool skip = false;
742
743 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
744
745 return skip;
746}
747
748bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
749 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
750 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
751 uint32_t bufferMemoryBarrierCount,
752 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
753 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500754 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600755 bool skip = false;
756
757 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
758 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
759
760 return skip;
761}
762
763bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
764 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
765 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
766 uint32_t bufferMemoryBarrierCount,
767 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
768 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500769 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600770 bool skip = false;
771
772 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
773 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
774
775 return skip;
776}
777
778bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500779 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600780 bool skip = false;
781
782 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
783
784 return skip;
785}
786
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700787// Generic function to handle validation for all CmdDraw* type functions
788bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
789 bool skip = false;
790 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
791 if (cb_state) {
792 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
793 const PIPELINE_STATE* pipeline_state = nullptr;
794 if (last_bound_it != cb_state->lastBound.cend()) {
795 pipeline_state = last_bound_it->second.pipeline_state;
796 }
797 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
798 // Verify vertex binding
799 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
800 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700801 skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
802 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
803 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
804 report_data->FormatHandle(pipeline_state->pipeline).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700805 }
806 }
807 }
808 return skip;
809}
810
Camden5b184be2019-08-13 07:50:19 -0600811bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500812 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600813 bool skip = false;
814
815 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700816 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
817 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700818 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600819 }
820
821 return skip;
822}
823
824bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500825 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600826 bool skip = false;
827
828 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700829 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
830 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600831 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700832 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
833
834 return skip;
835}
836
837bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
838 VkDeviceSize offset, VkBuffer countBuffer,
839 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
840 uint32_t stride) const {
841 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600842
843 return skip;
844}
845
846bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500847 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600848 bool skip = false;
849
850 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700851 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
852 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700853 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600854 }
855
856 return skip;
857}
858
859bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500860 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600861 bool skip = false;
862
863 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700864 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
865 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700866 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600867 }
868
869 return skip;
870}
871
872bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500873 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600874 bool skip = false;
875
876 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700877 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
878 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
879 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
880 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600881 }
882
883 return skip;
884}
Camden83a9c372019-08-14 11:41:38 -0600885
Camden Stocker9c051442019-11-06 14:28:43 -0800886bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
887 const char* api_name) const {
888 bool skip = false;
889 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
890
891 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700892 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
893 "Potential problem with calling %s() without first retrieving properties from "
894 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
895 api_name);
Camden Stocker9c051442019-11-06 14:28:43 -0800896 }
897
898 return skip;
899}
900
Camden83a9c372019-08-14 11:41:38 -0600901bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500902 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600903 bool skip = false;
904
Camden Stocker9c051442019-11-06 14:28:43 -0800905 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600906
Camden Stocker9c051442019-11-06 14:28:43 -0800907 return skip;
908}
909
910bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
911 uint32_t planeIndex,
912 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
913 bool skip = false;
914
915 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
916
917 return skip;
918}
919
920bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
921 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
922 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
923 bool skip = false;
924
925 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600926
927 return skip;
928}
Camden05de2d42019-08-19 10:23:56 -0600929
930bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500931 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600932 bool skip = false;
933
934 auto swapchain_state = GetSwapchainState(swapchain);
935
936 if (swapchain_state && pSwapchainImages) {
937 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
938 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700939 skip |=
940 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
941 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
942 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -0600943 }
944 }
945
946 return skip;
947}
948
949// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700950bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state,
951 uint32_t requested_queue_family_property_count,
952 bool qfp_null, const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -0600953 bool skip = false;
954 if (!qfp_null) {
955 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
956 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700957 skip |= LogWarning(
958 pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount,
Camden05de2d42019-08-19 10:23:56 -0600959 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
960 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
961 caller_name, caller_name);
962 // Then verify that pCount that is passed in on second call matches what was returned
963 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700964 skip |= LogWarning(
965 pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch,
Camden05de2d42019-08-19 10:23:56 -0600966 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
967 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
968 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
969 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
970 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
971 }
972 }
973
974 return skip;
975}
976
Jeff Bolz5c801d12019-10-09 10:38:45 -0500977bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
978 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600979 bool skip = false;
980
981 for (uint32_t i = 0; i < bindInfoCount; i++) {
982 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
983 if (!as_state->memory_requirements_checked) {
984 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
985 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
986 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700987 skip |= LogWarning(
988 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -0600989 "vkBindAccelerationStructureMemoryNV(): "
990 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
991 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
992 }
993 }
994
995 return skip;
996}
997
Camden05de2d42019-08-19 10:23:56 -0600998bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
999 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001000 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06001001 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
1002 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001003 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -06001004 (nullptr == pQueueFamilyProperties),
1005 "vkGetPhysicalDeviceQueueFamilyProperties()");
1006}
1007
Jeff Bolz5c801d12019-10-09 10:38:45 -05001008bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
1009 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
1010 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06001011 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
1012 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001013 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -06001014 (nullptr == pQueueFamilyProperties),
1015 "vkGetPhysicalDeviceQueueFamilyProperties2()");
1016}
1017
Jeff Bolz5c801d12019-10-09 10:38:45 -05001018bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
1019 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
1020 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -06001021 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
1022 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001023 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -06001024 (nullptr == pQueueFamilyProperties),
1025 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
1026}
1027
1028bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
1029 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001030 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06001031 if (!pSurfaceFormats) return false;
1032 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
1033 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
1034 bool skip = false;
1035 if (call_state == UNCALLED) {
1036 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
1037 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001038 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
1039 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
1040 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06001041 } else {
1042 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -04001043 if (*pSurfaceFormatCount > prev_format_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001044 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
1045 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
1046 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
1047 "when pSurfaceFormatCount was NULL.",
1048 *pSurfaceFormatCount, prev_format_count);
Camden05de2d42019-08-19 10:23:56 -06001049 }
1050 }
1051 return skip;
1052}
Camden Stocker23cc47d2019-09-03 14:53:57 -06001053
1054bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001055 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06001056 bool skip = false;
1057
1058 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
1059 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
1060 // Store sparse binding image_state and after binding is complete make sure that any requiring metadata have it bound
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001061 std::unordered_set<const IMAGE_STATE*> sparse_images;
1062 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
1063 // in RecordQueueBindSparse.
1064 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06001065 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
1066 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
1067 const auto& image_bind = bindInfo.pImageBinds[i];
1068 auto image_state = GetImageState(image_bind.image);
1069 if (!image_state)
1070 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1071 sparse_images.insert(image_state);
1072 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1073 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
1074 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001075 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1076 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
1077 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
1078 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001079 }
1080 }
1081 if (!image_state->memory_requirements_checked) {
1082 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001083 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1084 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
1085 "vkGetImageMemoryRequirements() to retrieve requirements.",
1086 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001087 }
1088 }
1089 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
1090 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
1091 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
1092 if (!image_state)
1093 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1094 sparse_images.insert(image_state);
1095 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1096 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
1097 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001098 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1099 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
1100 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
1101 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001102 }
1103 }
1104 if (!image_state->memory_requirements_checked) {
1105 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001106 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1107 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
1108 "vkGetImageMemoryRequirements() to retrieve requirements.",
1109 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001110 }
1111 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1112 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001113 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -06001114 }
1115 }
1116 }
1117 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001118 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
1119 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06001120 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001121 skip |= LogWarning(sparse_image_state->image, kVUID_Core_MemTrack_InvalidState,
1122 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
1123 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
1124 report_data->FormatHandle(sparse_image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001125 }
1126 }
1127 }
1128
1129 return skip;
1130}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001131
1132void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
1133 VkFence fence, VkResult result) {
Mark Lobodzinski97484d62020-03-03 11:57:41 -07001134 ValidationStateTracker::PostCallRecordQueueBindSparse(queue, bindInfoCount, pBindInfo, fence, result);
1135
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07001136 if (result != VK_SUCCESS) {
1137 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
1138 VK_ERROR_DEVICE_LOST};
1139 static std::vector<VkResult> success_codes = {};
1140 ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes);
1141 return;
1142 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001143
1144 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
1145 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
1146 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
1147 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
1148 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
1149 if (!image_state)
1150 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1151 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1152 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
1153 image_state->sparse_metadata_bound = true;
1154 }
1155 }
1156 }
1157 }
1158}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001159
1160bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06001161 const VkClearAttachment* pAttachments, uint32_t rectCount,
1162 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001163 bool skip = false;
1164 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
1165 if (!cb_node) return skip;
1166
Camden Stockerf55721f2019-09-09 11:04:49 -06001167 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001168 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
1169 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1170 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
1171 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1172 // CmdClearAttachments.
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001173 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
1174 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
1175 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1176 report_data->FormatHandle(commandBuffer).c_str());
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001177 }
1178
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001179 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
1180 // as it can be more efficient to just use LOAD_OP_CLEAR
1181 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass;
1182 if (rp) {
1183 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
1184
1185 for (uint32_t i = 0; i < attachmentCount; i++) {
1186 auto& attachment = pAttachments[i];
1187 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
1188 uint32_t color_attachment = attachment.colorAttachment;
1189 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
1190
1191 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1192 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1193 skip |= LogPerformanceWarning(
1194 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1195 "vkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
1196 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1197 "it is more efficient.",
1198 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
1199 }
1200 }
1201 }
1202
1203 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1204 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1205
1206 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1207 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1208 skip |= LogPerformanceWarning(
1209 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1210 "vkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
1211 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1212 "it is more efficient.",
1213 report_data->FormatHandle(commandBuffer).c_str());
1214 }
1215 }
1216 }
1217
1218 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1219 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1220
1221 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1222 if (rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1223 skip |= LogPerformanceWarning(
1224 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1225 "vkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
1226 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1227 "it is more efficient.",
1228 report_data->FormatHandle(commandBuffer).c_str());
1229 }
1230 }
1231 }
1232 }
1233 }
1234
Camden Stockerf55721f2019-09-09 11:04:49 -06001235 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001236}