blob: d554e333775f15b2bc7b55af58f7479b6c5d7131 [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
Mark Lobodzinski6167e102020-02-24 17:03:55 -070040const char* DepReasonToString(ExtDeprecationReason reason) {
41 switch (reason) {
42 case kExtPromoted:
43 return "promoted to";
44 break;
45 case kExtObsoleted:
46 return "obsoleted by";
47 break;
48 case kExtDeprecated:
49 return "deprecated by";
50 break;
51 default:
52 return "";
53 break;
54 }
55}
56
57bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
58 const char* vuid) const {
59 bool skip = false;
60 auto dep_info_it = deprecated_extensions.find(extension_name);
61 if (dep_info_it != deprecated_extensions.end()) {
62 auto dep_info = dep_info_it->second;
63 if ((dep_info.target.compare("VK_VERSION_1_1") && (version >= VK_VERSION_1_1)) ||
64 (dep_info.target.compare("VK_VERSION_1_2") && (version >= VK_VERSION_1_2))) {
65 skip |=
66 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
67 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
68 } else if (!dep_info.target.find("VK_VERSION")) {
69 if (dep_info.target.length() == 0) {
70 skip |= LogWarning(instance, vuid,
71 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
72 "without replacement.",
73 api_name, extension_name);
74 } else {
75 skip |= LogWarning(instance, vuid,
76 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
77 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
78 }
79 }
80 }
81 return skip;
82}
83
Camden5b184be2019-08-13 07:50:19 -060084bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -050085 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -060086 bool skip = false;
87
88 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
89 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -080090 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070091 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
92 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060093 }
Mark Lobodzinski6167e102020-02-24 17:03:55 -070094 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i],
95 pCreateInfo->pApplicationInfo->apiVersion,
96 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Camden5b184be2019-08-13 07:50:19 -060097 }
98
99 return skip;
100}
101
102void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
103 VkInstance* pInstance) {
104 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
105}
106
107bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500108 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600109 bool skip = false;
110
111 // get API version of physical device passed when creating device.
112 VkPhysicalDeviceProperties physical_device_properties{};
113 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500114 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600115
116 // check api versions and warn if instance api Version is higher than version on device.
117 if (instance_api_version > device_api_version) {
118 std::string inst_api_name = GetAPIVersionName(instance_api_version);
119 std::string dev_api_name = GetAPIVersionName(device_api_version);
120
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700121 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
122 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
123 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600124 }
125
126 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
127 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800128 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700129 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
130 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600131 }
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700132 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], instance_api_version,
133 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Camden5b184be2019-08-13 07:50:19 -0600134 }
135
Camden83a9c372019-08-14 11:41:38 -0600136 auto pd_state = GetPhysicalDeviceState(physicalDevice);
Corta48da1d2019-09-20 18:59:07 +0200137 if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700138 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
139 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600140 }
141
Camden5b184be2019-08-13 07:50:19 -0600142 return skip;
143}
144
145bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500146 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600147 bool skip = false;
148
149 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
150 std::stringstream bufferHex;
151 bufferHex << "0x" << std::hex << HandleToUint64(pBuffer);
152
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700153 skip |= LogWarning(
154 device, kVUID_BestPractices_SharingModeExclusive,
155 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
156 "(queueFamilyIndexCount of %" PRIu32 ").",
157 bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600158 }
159
160 return skip;
161}
162
163bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500164 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600165 bool skip = false;
166
167 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
168 std::stringstream imageHex;
169 imageHex << "0x" << std::hex << HandleToUint64(pImage);
170
171 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700172 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
173 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
174 "(queueFamilyIndexCount of %" PRIu32 ").",
175 imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600176 }
177
178 return skip;
179}
180
181bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500182 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600183 bool skip = false;
184
Camden83a9c372019-08-14 11:41:38 -0600185 auto physical_device_state = GetPhysicalDeviceState();
186
187 if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700188 skip |= LogWarning(
189 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600190 "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
191 }
192
193 if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700194 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
195 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
196 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600197 }
198
199 if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700200 skip |= LogWarning(
201 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
202 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
Camden83a9c372019-08-14 11:41:38 -0600203 }
204
Camden5b184be2019-08-13 07:50:19 -0600205 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700206 skip |=
207 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
208 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while "
209 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
210 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600211 }
212
213 return skip;
214}
215
216bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
217 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500218 const VkAllocationCallbacks* pAllocator,
219 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600220 bool skip = false;
221
222 for (uint32_t i = 0; i < swapchainCount; i++) {
223 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700224 skip |= LogWarning(
225 device, kVUID_BestPractices_SharingModeExclusive,
226 "Warning: A shared swapchain (index %" PRIu32
227 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
228 "queues (queueFamilyIndexCount of %" PRIu32 ").",
229 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600230 }
231 }
232
233 return skip;
234}
235
236bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500237 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600238 bool skip = false;
239
240 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
241 VkFormat format = pCreateInfo->pAttachments[i].format;
242 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
243 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
244 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700245 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
246 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
247 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
248 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
249 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600250 }
251 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700252 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
253 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
254 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
255 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
256 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600257 }
258 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000259
260 const auto& attachment = pCreateInfo->pAttachments[i];
261 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
262 bool access_requires_memory =
263 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
264
265 if (FormatHasStencil(format)) {
266 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
267 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
268 }
269
270 if (access_requires_memory) {
271 skip |= LogPerformanceWarning(
272 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
273 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
274 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
275 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
276 i, static_cast<uint32_t>(attachment.samples));
277 }
278 }
Camden5b184be2019-08-13 07:50:19 -0600279 }
280
281 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
282 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
283 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
284 }
285
286 return skip;
287}
288
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000289bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
290 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
291 bool skip = false;
292
293 // Check for non-transient attachments that should be transient and vice versa
294 auto rp_state = GetRenderPassState(pCreateInfo->renderPass);
295 if (rp_state) {
296 const VkRenderPassCreateInfo2* rpci = rp_state->createInfo.ptr();
297 const VkImageView* image_views = pCreateInfo->pAttachments;
298
299 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
300 auto& attachment = rpci->pAttachments[i];
301 bool attachment_should_be_transient =
302 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
303
304 if (FormatHasStencil(attachment.format)) {
305 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
306 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
307 }
308
309 auto view_state = GetImageViewState(image_views[i]);
310 if (view_state) {
311 auto& ivci = view_state->create_info;
312 auto& ici = GetImageState(ivci.image)->createInfo;
313
314 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
315
316 // The check for an image that should not be transient applies to all GPUs
317 if (!attachment_should_be_transient && image_is_transient) {
318 skip |= LogPerformanceWarning(
319 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
320 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
321 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
322 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
323 i);
324 }
325
326 bool supports_lazy = false;
327 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
328 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
329 supports_lazy = true;
330 }
331 }
332
333 // The check for an image that should be transient only applies to GPUs supporting
334 // lazily allocated memory
335 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
336 skip |= LogPerformanceWarning(
337 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
338 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
339 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
340 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
341 i);
342 }
343 }
344 }
345 }
346
347 return skip;
348}
349
Camden5b184be2019-08-13 07:50:19 -0600350bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500351 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600352 bool skip = false;
353
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500354 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700355 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
356 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600357 }
358
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000359 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
360 skip |= LogPerformanceWarning(
361 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
362 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %llu. This is a very small allocation (current "
363 "threshold is %llu bytes). "
364 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
365 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
366 }
367
Camden83a9c372019-08-14 11:41:38 -0600368 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
369
370 return skip;
371}
372
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500373void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
374 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
375 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700376 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700377 if (result != VK_SUCCESS) {
378 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
379 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
380 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR};
381 static std::vector<VkResult> success_codes = {};
382 ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes);
383 return;
384 }
385 num_mem_objects++;
386}
Camden Stocker9738af92019-10-16 13:54:03 -0700387
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700388void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& success_codes,
389 const std::vector<VkResult>& error_codes) const {
390 auto error = std::find(error_codes.begin(), error_codes.end(), result);
391 if (error != error_codes.end()) {
392 LogWarning(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
393 return;
394 }
395 auto success = std::find(success_codes.begin(), success_codes.end(), result);
396 if (success != success_codes.end()) {
397 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned non-success return code %s.", api_name,
398 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500399 }
400}
401
Jeff Bolz5c801d12019-10-09 10:38:45 -0500402bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
403 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700404 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600405 bool skip = false;
406
Camden Stocker9738af92019-10-16 13:54:03 -0700407 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600408
409 for (auto& obj : mem_info->obj_bindings) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700410 skip |= LogWarning(device, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
411 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem).c_str());
Camden83a9c372019-08-14 11:41:38 -0600412 }
413
Camden5b184be2019-08-13 07:50:19 -0600414 return skip;
415}
416
417void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
418 if (memory != VK_NULL_HANDLE) {
419 num_mem_objects--;
420 }
421}
422
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000423bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600424 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500425 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600426
sfricke-samsunge2441192019-11-06 14:07:57 -0800427 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700428 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
429 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
430 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600431 }
432
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000433 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
434
435 if (mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
436 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
437 skip |= LogPerformanceWarning(
438 device, kVUID_BestPractices_SmallDedicatedAllocation,
439 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
440 "The required size of the allocation is %llu, but smaller buffers like this should be sub-allocated from "
441 "larger memory blocks. (Current threshold is %llu bytes.)",
442 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
443 }
444
Camden Stockerb603cc82019-09-03 10:09:02 -0600445 return skip;
446}
447
448bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500449 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600450 bool skip = false;
451 const char* api_name = "BindBufferMemory()";
452
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000453 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600454
455 return skip;
456}
457
458bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500459 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600460 char api_name[64];
461 bool skip = false;
462
463 for (uint32_t i = 0; i < bindInfoCount; i++) {
464 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000465 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600466 }
467
468 return skip;
469}
Camden Stockerb603cc82019-09-03 10:09:02 -0600470
471bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500472 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600473 char api_name[64];
474 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600475
Camden Stocker8b798ab2019-09-03 10:33:28 -0600476 for (uint32_t i = 0; i < bindInfoCount; i++) {
477 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000478 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600479 }
480
481 return skip;
482}
483
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000484bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600485 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500486 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600487
sfricke-samsunge2441192019-11-06 14:07:57 -0800488 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700489 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
490 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
491 api_name, report_data->FormatHandle(image).c_str());
Camden Stocker8b798ab2019-09-03 10:33:28 -0600492 }
493
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000494 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
495
496 if (mem_state->alloc_info.allocationSize == image_state->requirements.size &&
497 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
498 skip |= LogPerformanceWarning(
499 device, kVUID_BestPractices_SmallDedicatedAllocation,
500 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
501 "The required size of the allocation is %llu, but smaller images like this should be sub-allocated from "
502 "larger memory blocks. (Current threshold is %llu bytes.)",
503 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
504 }
505
506 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
507 // make sure this type is actually used.
508 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
509 // (i.e.most tile - based renderers)
510 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
511 bool supports_lazy = false;
512 uint32_t suggested_type = 0;
513
514 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
515 if ((1u << i) & image_state->requirements.memoryTypeBits) {
516 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
517 supports_lazy = true;
518 suggested_type = i;
519 break;
520 }
521 }
522 }
523
524 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
525
526 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
527 skip |= LogPerformanceWarning(
528 device, kVUID_BestPractices_NonLazyTransientImage,
529 "%s: Attempting to bind memory type % u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
530 "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
531 "%llu bytes of physical memory.",
532 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements.size);
533 }
534 }
535
Camden Stocker8b798ab2019-09-03 10:33:28 -0600536 return skip;
537}
538
539bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500540 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600541 bool skip = false;
542 const char* api_name = "vkBindImageMemory()";
543
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000544 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600545
546 return skip;
547}
548
549bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500550 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600551 char api_name[64];
552 bool skip = false;
553
554 for (uint32_t i = 0; i < bindInfoCount; i++) {
555 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000556 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600557 }
558
559 return skip;
560}
561
562bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500563 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600564 char api_name[64];
565 bool skip = false;
566
567 for (uint32_t i = 0; i < bindInfoCount; i++) {
568 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000569 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600570 }
571
572 return skip;
573}
Camden83a9c372019-08-14 11:41:38 -0600574
Camden5b184be2019-08-13 07:50:19 -0600575bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
576 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600577 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500578 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600579 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
580 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600581
582 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700583 skip |= LogPerformanceWarning(
584 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
585 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
586 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600587 }
588
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000589 for (uint32_t i = 0; i < createInfoCount; i++) {
590 auto& createInfo = pCreateInfos[i];
591
592 auto& vertexInput = *createInfo.pVertexInputState;
593 uint32_t count = 0;
594 for (uint32_t j = 0; j < vertexInput.vertexBindingDescriptionCount; j++) {
595 if (vertexInput.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
596 count++;
597 }
598 }
599
600 if (count > kMaxInstancedVertexBuffers) {
601 skip |= LogPerformanceWarning(
602 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
603 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
604 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
605 count, kMaxInstancedVertexBuffers);
606 }
607 }
608
Camden5b184be2019-08-13 07:50:19 -0600609 return skip;
610}
611
612bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
613 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600614 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500615 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600616 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
617 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600618
619 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700620 skip |= LogPerformanceWarning(
621 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
622 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
623 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600624 }
625
626 return skip;
627}
628
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500629bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600630 bool skip = false;
631
632 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700633 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
634 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600635 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700636 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
637 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600638 }
639
640 return skip;
641}
642
Jeff Bolz5c801d12019-10-09 10:38:45 -0500643bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
644 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600645 bool skip = false;
646
647 for (uint32_t submit = 0; submit < submitCount; submit++) {
648 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
649 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
650 }
651 }
652
653 return skip;
654}
655
Attilio Provenzano746e43e2020-02-27 11:23:50 +0000656bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
657 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
658 bool skip = false;
659
660 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
661 skip |= LogPerformanceWarning(
662 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
663 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
664 "pool instead.");
665 }
666
667 return skip;
668}
669
670bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
671 const VkCommandBufferBeginInfo* pBeginInfo) const {
672 bool skip = false;
673
674 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
675 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
676 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
677 }
678
679 return skip;
680}
681
Jeff Bolz5c801d12019-10-09 10:38:45 -0500682bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600683 bool skip = false;
684
685 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
686
687 return skip;
688}
689
Jeff Bolz5c801d12019-10-09 10:38:45 -0500690bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
691 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600692 bool skip = false;
693
694 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
695
696 return skip;
697}
698
699bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
700 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
701 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
702 uint32_t bufferMemoryBarrierCount,
703 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
704 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500705 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600706 bool skip = false;
707
708 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
709 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
710
711 return skip;
712}
713
714bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
715 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
716 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
717 uint32_t bufferMemoryBarrierCount,
718 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
719 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500720 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600721 bool skip = false;
722
723 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
724 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
725
726 return skip;
727}
728
729bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500730 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600731 bool skip = false;
732
733 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
734
735 return skip;
736}
737
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700738// Generic function to handle validation for all CmdDraw* type functions
739bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
740 bool skip = false;
741 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
742 if (cb_state) {
743 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
744 const PIPELINE_STATE* pipeline_state = nullptr;
745 if (last_bound_it != cb_state->lastBound.cend()) {
746 pipeline_state = last_bound_it->second.pipeline_state;
747 }
748 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
749 // Verify vertex binding
750 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
751 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700752 skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
753 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
754 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
755 report_data->FormatHandle(pipeline_state->pipeline).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700756 }
757 }
758 }
759 return skip;
760}
761
Camden5b184be2019-08-13 07:50:19 -0600762bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500763 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600764 bool skip = false;
765
766 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700767 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
768 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700769 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600770 }
771
772 return skip;
773}
774
775bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500776 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600777 bool skip = false;
778
779 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700780 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
781 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600782 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700783 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
784
785 return skip;
786}
787
788bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
789 VkDeviceSize offset, VkBuffer countBuffer,
790 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
791 uint32_t stride) const {
792 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600793
794 return skip;
795}
796
797bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500798 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600799 bool skip = false;
800
801 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700802 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
803 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700804 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600805 }
806
807 return skip;
808}
809
810bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500811 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600812 bool skip = false;
813
814 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700815 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
816 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700817 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600818 }
819
820 return skip;
821}
822
823bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500824 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600825 bool skip = false;
826
827 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700828 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
829 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
830 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
831 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600832 }
833
834 return skip;
835}
Camden83a9c372019-08-14 11:41:38 -0600836
Camden Stocker9c051442019-11-06 14:28:43 -0800837bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
838 const char* api_name) const {
839 bool skip = false;
840 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
841
842 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700843 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
844 "Potential problem with calling %s() without first retrieving properties from "
845 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
846 api_name);
Camden Stocker9c051442019-11-06 14:28:43 -0800847 }
848
849 return skip;
850}
851
Camden83a9c372019-08-14 11:41:38 -0600852bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500853 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600854 bool skip = false;
855
Camden Stocker9c051442019-11-06 14:28:43 -0800856 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600857
Camden Stocker9c051442019-11-06 14:28:43 -0800858 return skip;
859}
860
861bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
862 uint32_t planeIndex,
863 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
864 bool skip = false;
865
866 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
867
868 return skip;
869}
870
871bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
872 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
873 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
874 bool skip = false;
875
876 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600877
878 return skip;
879}
Camden05de2d42019-08-19 10:23:56 -0600880
881bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500882 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600883 bool skip = false;
884
885 auto swapchain_state = GetSwapchainState(swapchain);
886
887 if (swapchain_state && pSwapchainImages) {
888 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
889 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700890 skip |=
891 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
892 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
893 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -0600894 }
895 }
896
897 return skip;
898}
899
900// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700901bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state,
902 uint32_t requested_queue_family_property_count,
903 bool qfp_null, const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -0600904 bool skip = false;
905 if (!qfp_null) {
906 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
907 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700908 skip |= LogWarning(
909 pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount,
Camden05de2d42019-08-19 10:23:56 -0600910 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
911 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
912 caller_name, caller_name);
913 // Then verify that pCount that is passed in on second call matches what was returned
914 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700915 skip |= LogWarning(
916 pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch,
Camden05de2d42019-08-19 10:23:56 -0600917 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
918 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
919 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
920 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
921 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
922 }
923 }
924
925 return skip;
926}
927
Jeff Bolz5c801d12019-10-09 10:38:45 -0500928bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
929 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600930 bool skip = false;
931
932 for (uint32_t i = 0; i < bindInfoCount; i++) {
933 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
934 if (!as_state->memory_requirements_checked) {
935 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
936 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
937 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700938 skip |= LogWarning(
939 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -0600940 "vkBindAccelerationStructureMemoryNV(): "
941 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
942 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
943 }
944 }
945
946 return skip;
947}
948
Camden05de2d42019-08-19 10:23:56 -0600949bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
950 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500951 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600952 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
953 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700954 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600955 (nullptr == pQueueFamilyProperties),
956 "vkGetPhysicalDeviceQueueFamilyProperties()");
957}
958
Jeff Bolz5c801d12019-10-09 10:38:45 -0500959bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
960 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
961 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600962 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
963 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700964 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600965 (nullptr == pQueueFamilyProperties),
966 "vkGetPhysicalDeviceQueueFamilyProperties2()");
967}
968
Jeff Bolz5c801d12019-10-09 10:38:45 -0500969bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
970 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
971 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600972 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
973 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700974 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600975 (nullptr == pQueueFamilyProperties),
976 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
977}
978
979bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
980 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500981 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600982 if (!pSurfaceFormats) return false;
983 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
984 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
985 bool skip = false;
986 if (call_state == UNCALLED) {
987 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
988 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700989 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
990 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
991 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -0600992 } else {
993 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400994 if (*pSurfaceFormatCount > prev_format_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700995 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
996 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
997 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
998 "when pSurfaceFormatCount was NULL.",
999 *pSurfaceFormatCount, prev_format_count);
Camden05de2d42019-08-19 10:23:56 -06001000 }
1001 }
1002 return skip;
1003}
Camden Stocker23cc47d2019-09-03 14:53:57 -06001004
1005bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001006 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06001007 bool skip = false;
1008
1009 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
1010 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
1011 // 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 -05001012 std::unordered_set<const IMAGE_STATE*> sparse_images;
1013 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
1014 // in RecordQueueBindSparse.
1015 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06001016 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
1017 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
1018 const auto& image_bind = bindInfo.pImageBinds[i];
1019 auto image_state = GetImageState(image_bind.image);
1020 if (!image_state)
1021 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1022 sparse_images.insert(image_state);
1023 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1024 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
1025 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001026 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1027 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
1028 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
1029 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001030 }
1031 }
1032 if (!image_state->memory_requirements_checked) {
1033 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001034 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1035 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
1036 "vkGetImageMemoryRequirements() to retrieve requirements.",
1037 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001038 }
1039 }
1040 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
1041 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
1042 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
1043 if (!image_state)
1044 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1045 sparse_images.insert(image_state);
1046 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1047 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
1048 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001049 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1050 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
1051 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
1052 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001053 }
1054 }
1055 if (!image_state->memory_requirements_checked) {
1056 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001057 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1058 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
1059 "vkGetImageMemoryRequirements() to retrieve requirements.",
1060 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001061 }
1062 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1063 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001064 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -06001065 }
1066 }
1067 }
1068 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001069 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
1070 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06001071 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001072 skip |= LogWarning(sparse_image_state->image, kVUID_Core_MemTrack_InvalidState,
1073 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
1074 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
1075 report_data->FormatHandle(sparse_image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001076 }
1077 }
1078 }
1079
1080 return skip;
1081}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001082
1083void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
1084 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07001085 if (result != VK_SUCCESS) {
1086 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
1087 VK_ERROR_DEVICE_LOST};
1088 static std::vector<VkResult> success_codes = {};
1089 ValidateReturnCodes("vkReleaseFullScreenExclusiveModeEXT", result, error_codes, success_codes);
1090 return;
1091 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001092
1093 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
1094 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
1095 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
1096 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
1097 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
1098 if (!image_state)
1099 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1100 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1101 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
1102 image_state->sparse_metadata_bound = true;
1103 }
1104 }
1105 }
1106 }
1107}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001108
1109bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06001110 const VkClearAttachment* pAttachments, uint32_t rectCount,
1111 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001112 bool skip = false;
1113 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
1114 if (!cb_node) return skip;
1115
Camden Stockerf55721f2019-09-09 11:04:49 -06001116 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001117 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
1118 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1119 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
1120 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1121 // CmdClearAttachments.
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001122 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
1123 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
1124 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1125 report_data->FormatHandle(commandBuffer).c_str());
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001126 }
1127
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001128 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
1129 // as it can be more efficient to just use LOAD_OP_CLEAR
1130 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass;
1131 if (rp) {
1132 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
1133
1134 for (uint32_t i = 0; i < attachmentCount; i++) {
1135 auto& attachment = pAttachments[i];
1136 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
1137 uint32_t color_attachment = attachment.colorAttachment;
1138 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
1139
1140 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1141 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1142 skip |= LogPerformanceWarning(
1143 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1144 "vkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
1145 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1146 "it is more efficient.",
1147 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
1148 }
1149 }
1150 }
1151
1152 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1153 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1154
1155 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1156 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1157 skip |= LogPerformanceWarning(
1158 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1159 "vkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
1160 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1161 "it is more efficient.",
1162 report_data->FormatHandle(commandBuffer).c_str());
1163 }
1164 }
1165 }
1166
1167 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1168 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1169
1170 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1171 if (rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1172 skip |= LogPerformanceWarning(
1173 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1174 "vkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
1175 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1176 "it is more efficient.",
1177 report_data->FormatHandle(commandBuffer).c_str());
1178 }
1179 }
1180 }
1181 }
1182 }
1183
Camden Stockerf55721f2019-09-09 11:04:49 -06001184 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001185}