blob: 1a06f6a2305a49aa78139b6b290a856a014f736a [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
Camden3231dcc2019-08-05 11:28:57 -060020#include "best_practices.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
40bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -050041 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -060042 bool skip = false;
43
44 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
45 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -080046 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070047 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
48 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060049 }
Camden Stocker11ecf512020-01-21 16:06:49 -080050
51 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeprecatedExtensionNames)) {
52 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_DeprecatedExtension,
53 "vkCreateInstance(): Attempting to enable Deprecated Extension %s at CreateInstance time.",
54 pCreateInfo->ppEnabledExtensionNames[i]);
55 }
Camden5b184be2019-08-13 07:50:19 -060056 }
57
58 return skip;
59}
60
61void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
62 VkInstance* pInstance) {
63 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
64}
65
66bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -050067 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -060068 bool skip = false;
69
70 // get API version of physical device passed when creating device.
71 VkPhysicalDeviceProperties physical_device_properties{};
72 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -050073 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -060074
75 // check api versions and warn if instance api Version is higher than version on device.
76 if (instance_api_version > device_api_version) {
77 std::string inst_api_name = GetAPIVersionName(instance_api_version);
78 std::string dev_api_name = GetAPIVersionName(device_api_version);
79
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070080 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
81 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
82 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -060083 }
84
85 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
86 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -080087 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070088 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
89 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060090 }
Camden Stocker11ecf512020-01-21 16:06:49 -080091 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeprecatedExtensionNames)) {
92 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_DeprecatedExtension,
93 "vkCreateDevice(): Attempting to enable Deprecated Extension %s at CreateDevice time.",
94 pCreateInfo->ppEnabledExtensionNames[i]);
95 }
Camden5b184be2019-08-13 07:50:19 -060096 }
97
Camden83a9c372019-08-14 11:41:38 -060098 auto pd_state = GetPhysicalDeviceState(physicalDevice);
Corta48da1d2019-09-20 18:59:07 +020099 if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700100 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
101 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600102 }
103
Camden5b184be2019-08-13 07:50:19 -0600104 return skip;
105}
106
107bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500108 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600109 bool skip = false;
110
111 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
112 std::stringstream bufferHex;
113 bufferHex << "0x" << std::hex << HandleToUint64(pBuffer);
114
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700115 skip |= LogWarning(
116 device, kVUID_BestPractices_SharingModeExclusive,
117 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
118 "(queueFamilyIndexCount of %" PRIu32 ").",
119 bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600120 }
121
122 return skip;
123}
124
125bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500126 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600127 bool skip = false;
128
129 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
130 std::stringstream imageHex;
131 imageHex << "0x" << std::hex << HandleToUint64(pImage);
132
133 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700134 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
135 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
136 "(queueFamilyIndexCount of %" PRIu32 ").",
137 imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600138 }
139
140 return skip;
141}
142
143bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500144 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600145 bool skip = false;
146
Camden83a9c372019-08-14 11:41:38 -0600147 auto physical_device_state = GetPhysicalDeviceState();
148
149 if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700150 skip |= LogWarning(
151 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600152 "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
153 }
154
155 if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700156 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
157 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
158 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600159 }
160
161 if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700162 skip |= LogWarning(
163 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
164 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
Camden83a9c372019-08-14 11:41:38 -0600165 }
166
Camden5b184be2019-08-13 07:50:19 -0600167 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700168 skip |=
169 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
170 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while "
171 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
172 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600173 }
174
175 return skip;
176}
177
178bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
179 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500180 const VkAllocationCallbacks* pAllocator,
181 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600182 bool skip = false;
183
184 for (uint32_t i = 0; i < swapchainCount; i++) {
185 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700186 skip |= LogWarning(
187 device, kVUID_BestPractices_SharingModeExclusive,
188 "Warning: A shared swapchain (index %" PRIu32
189 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
190 "queues (queueFamilyIndexCount of %" PRIu32 ").",
191 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600192 }
193 }
194
195 return skip;
196}
197
198bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500199 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600200 bool skip = false;
201
202 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
203 VkFormat format = pCreateInfo->pAttachments[i].format;
204 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
205 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
206 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700207 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
208 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
209 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
210 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
211 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600212 }
213 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700214 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
215 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
216 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
217 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
218 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600219 }
220 }
221 }
222
223 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
224 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
225 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
226 }
227
228 return skip;
229}
230
231bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500232 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600233 bool skip = false;
234
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500235 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700236 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
237 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600238 }
239
Camden83a9c372019-08-14 11:41:38 -0600240 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
241
242 return skip;
243}
244
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500245void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
246 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
247 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700248 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
249
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500250 if (VK_SUCCESS == result) {
251 num_mem_objects++;
252 }
253}
254
Jeff Bolz5c801d12019-10-09 10:38:45 -0500255bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
256 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700257 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600258 bool skip = false;
259
Camden Stocker9738af92019-10-16 13:54:03 -0700260 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600261
262 for (auto& obj : mem_info->obj_bindings) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700263 skip |= LogWarning(device, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
264 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem).c_str());
Camden83a9c372019-08-14 11:41:38 -0600265 }
266
Camden5b184be2019-08-13 07:50:19 -0600267 return skip;
268}
269
270void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
271 if (memory != VK_NULL_HANDLE) {
272 num_mem_objects--;
273 }
274}
275
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500276bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600277 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500278 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600279
sfricke-samsunge2441192019-11-06 14:07:57 -0800280 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700281 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
282 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
283 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600284 }
285
286 return skip;
287}
288
289bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500290 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600291 bool skip = false;
292 const char* api_name = "BindBufferMemory()";
293
294 skip |= ValidateBindBufferMemory(buffer, api_name);
295
296 return skip;
297}
298
299bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500300 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600301 char api_name[64];
302 bool skip = false;
303
304 for (uint32_t i = 0; i < bindInfoCount; i++) {
305 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
306 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
307 }
308
309 return skip;
310}
Camden Stockerb603cc82019-09-03 10:09:02 -0600311
312bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500313 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600314 char api_name[64];
315 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600316
Camden Stocker8b798ab2019-09-03 10:33:28 -0600317 for (uint32_t i = 0; i < bindInfoCount; i++) {
318 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
319 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
320 }
321
322 return skip;
323}
324
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500325bool BestPractices::ValidateBindImageMemory(VkImage image, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600326 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500327 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600328
sfricke-samsunge2441192019-11-06 14:07:57 -0800329 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700330 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
331 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
332 api_name, report_data->FormatHandle(image).c_str());
Camden Stocker8b798ab2019-09-03 10:33:28 -0600333 }
334
335 return skip;
336}
337
338bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500339 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600340 bool skip = false;
341 const char* api_name = "vkBindImageMemory()";
342
343 skip |= ValidateBindImageMemory(image, api_name);
344
345 return skip;
346}
347
348bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500349 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600350 char api_name[64];
351 bool skip = false;
352
353 for (uint32_t i = 0; i < bindInfoCount; i++) {
354 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
355 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
356 }
357
358 return skip;
359}
360
361bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500362 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600363 char api_name[64];
364 bool skip = false;
365
366 for (uint32_t i = 0; i < bindInfoCount; i++) {
367 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
368 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
369 }
370
371 return skip;
372}
Camden83a9c372019-08-14 11:41:38 -0600373
Camden5b184be2019-08-13 07:50:19 -0600374bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
375 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600376 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500377 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600378 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
379 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600380
381 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700382 skip |= LogPerformanceWarning(
383 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
384 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
385 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600386 }
387
388 return skip;
389}
390
391bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
392 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600393 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500394 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600395 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
396 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600397
398 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700399 skip |= LogPerformanceWarning(
400 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
401 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
402 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600403 }
404
405 return skip;
406}
407
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500408bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600409 bool skip = false;
410
411 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700412 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
413 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600414 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700415 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
416 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600417 }
418
419 return skip;
420}
421
Jeff Bolz5c801d12019-10-09 10:38:45 -0500422bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
423 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600424 bool skip = false;
425
426 for (uint32_t submit = 0; submit < submitCount; submit++) {
427 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
428 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
429 }
430 }
431
432 return skip;
433}
434
Attilio Provenzano746e43e2020-02-27 11:23:50 +0000435bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
436 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
437 bool skip = false;
438
439 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
440 skip |= LogPerformanceWarning(
441 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
442 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
443 "pool instead.");
444 }
445
446 return skip;
447}
448
449bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
450 const VkCommandBufferBeginInfo* pBeginInfo) const {
451 bool skip = false;
452
453 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
454 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
455 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
456 }
457
458 return skip;
459}
460
Jeff Bolz5c801d12019-10-09 10:38:45 -0500461bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600462 bool skip = false;
463
464 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
465
466 return skip;
467}
468
Jeff Bolz5c801d12019-10-09 10:38:45 -0500469bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
470 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600471 bool skip = false;
472
473 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
474
475 return skip;
476}
477
478bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
479 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
480 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
481 uint32_t bufferMemoryBarrierCount,
482 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
483 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500484 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600485 bool skip = false;
486
487 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
488 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
489
490 return skip;
491}
492
493bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
494 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
495 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
496 uint32_t bufferMemoryBarrierCount,
497 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
498 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500499 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600500 bool skip = false;
501
502 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
503 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
504
505 return skip;
506}
507
508bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500509 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600510 bool skip = false;
511
512 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
513
514 return skip;
515}
516
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700517// Generic function to handle validation for all CmdDraw* type functions
518bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
519 bool skip = false;
520 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
521 if (cb_state) {
522 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
523 const PIPELINE_STATE* pipeline_state = nullptr;
524 if (last_bound_it != cb_state->lastBound.cend()) {
525 pipeline_state = last_bound_it->second.pipeline_state;
526 }
527 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
528 // Verify vertex binding
529 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
530 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700531 skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
532 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
533 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
534 report_data->FormatHandle(pipeline_state->pipeline).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700535 }
536 }
537 }
538 return skip;
539}
540
Camden5b184be2019-08-13 07:50:19 -0600541bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500542 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600543 bool skip = false;
544
545 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700546 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
547 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700548 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600549 }
550
551 return skip;
552}
553
554bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500555 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600556 bool skip = false;
557
558 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700559 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
560 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600561 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700562 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
563
564 return skip;
565}
566
567bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
568 VkDeviceSize offset, VkBuffer countBuffer,
569 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
570 uint32_t stride) const {
571 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600572
573 return skip;
574}
575
576bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500577 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600578 bool skip = false;
579
580 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700581 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
582 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700583 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600584 }
585
586 return skip;
587}
588
589bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500590 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600591 bool skip = false;
592
593 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700594 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
595 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700596 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600597 }
598
599 return skip;
600}
601
602bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500603 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600604 bool skip = false;
605
606 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700607 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
608 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
609 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
610 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600611 }
612
613 return skip;
614}
Camden83a9c372019-08-14 11:41:38 -0600615
Camden Stocker9c051442019-11-06 14:28:43 -0800616bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
617 const char* api_name) const {
618 bool skip = false;
619 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
620
621 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700622 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
623 "Potential problem with calling %s() without first retrieving properties from "
624 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
625 api_name);
Camden Stocker9c051442019-11-06 14:28:43 -0800626 }
627
628 return skip;
629}
630
Camden83a9c372019-08-14 11:41:38 -0600631bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500632 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600633 bool skip = false;
634
Camden Stocker9c051442019-11-06 14:28:43 -0800635 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600636
Camden Stocker9c051442019-11-06 14:28:43 -0800637 return skip;
638}
639
640bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
641 uint32_t planeIndex,
642 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
643 bool skip = false;
644
645 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
646
647 return skip;
648}
649
650bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
651 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
652 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
653 bool skip = false;
654
655 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600656
657 return skip;
658}
Camden05de2d42019-08-19 10:23:56 -0600659
660bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500661 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600662 bool skip = false;
663
664 auto swapchain_state = GetSwapchainState(swapchain);
665
666 if (swapchain_state && pSwapchainImages) {
667 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
668 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700669 skip |=
670 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
671 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
672 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -0600673 }
674 }
675
676 return skip;
677}
678
679// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700680bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state,
681 uint32_t requested_queue_family_property_count,
682 bool qfp_null, const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -0600683 bool skip = false;
684 if (!qfp_null) {
685 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
686 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700687 skip |= LogWarning(
688 pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount,
Camden05de2d42019-08-19 10:23:56 -0600689 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
690 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
691 caller_name, caller_name);
692 // Then verify that pCount that is passed in on second call matches what was returned
693 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700694 skip |= LogWarning(
695 pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch,
Camden05de2d42019-08-19 10:23:56 -0600696 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
697 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
698 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
699 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
700 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
701 }
702 }
703
704 return skip;
705}
706
Jeff Bolz5c801d12019-10-09 10:38:45 -0500707bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
708 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600709 bool skip = false;
710
711 for (uint32_t i = 0; i < bindInfoCount; i++) {
712 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
713 if (!as_state->memory_requirements_checked) {
714 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
715 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
716 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700717 skip |= LogWarning(
718 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -0600719 "vkBindAccelerationStructureMemoryNV(): "
720 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
721 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
722 }
723 }
724
725 return skip;
726}
727
Camden05de2d42019-08-19 10:23:56 -0600728bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
729 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500730 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600731 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
732 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700733 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600734 (nullptr == pQueueFamilyProperties),
735 "vkGetPhysicalDeviceQueueFamilyProperties()");
736}
737
Jeff Bolz5c801d12019-10-09 10:38:45 -0500738bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
739 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
740 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600741 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
742 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700743 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600744 (nullptr == pQueueFamilyProperties),
745 "vkGetPhysicalDeviceQueueFamilyProperties2()");
746}
747
Jeff Bolz5c801d12019-10-09 10:38:45 -0500748bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
749 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
750 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600751 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
752 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700753 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600754 (nullptr == pQueueFamilyProperties),
755 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
756}
757
758bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
759 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500760 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600761 if (!pSurfaceFormats) return false;
762 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
763 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
764 bool skip = false;
765 if (call_state == UNCALLED) {
766 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
767 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700768 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
769 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
770 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -0600771 } else {
772 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400773 if (*pSurfaceFormatCount > prev_format_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700774 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
775 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
776 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
777 "when pSurfaceFormatCount was NULL.",
778 *pSurfaceFormatCount, prev_format_count);
Camden05de2d42019-08-19 10:23:56 -0600779 }
780 }
781 return skip;
782}
Camden Stocker23cc47d2019-09-03 14:53:57 -0600783
784bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500785 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600786 bool skip = false;
787
788 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
789 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
790 // 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 -0500791 std::unordered_set<const IMAGE_STATE*> sparse_images;
792 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
793 // in RecordQueueBindSparse.
794 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -0600795 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
796 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
797 const auto& image_bind = bindInfo.pImageBinds[i];
798 auto image_state = GetImageState(image_bind.image);
799 if (!image_state)
800 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
801 sparse_images.insert(image_state);
802 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
803 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
804 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700805 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
806 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
807 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
808 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600809 }
810 }
811 if (!image_state->memory_requirements_checked) {
812 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700813 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
814 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
815 "vkGetImageMemoryRequirements() to retrieve requirements.",
816 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600817 }
818 }
819 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
820 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
821 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
822 if (!image_state)
823 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
824 sparse_images.insert(image_state);
825 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
826 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
827 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700828 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
829 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
830 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
831 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600832 }
833 }
834 if (!image_state->memory_requirements_checked) {
835 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700836 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
837 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
838 "vkGetImageMemoryRequirements() to retrieve requirements.",
839 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600840 }
841 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
842 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500843 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -0600844 }
845 }
846 }
847 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500848 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
849 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600850 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700851 skip |= LogWarning(sparse_image_state->image, kVUID_Core_MemTrack_InvalidState,
852 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
853 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
854 report_data->FormatHandle(sparse_image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600855 }
856 }
857 }
858
859 return skip;
860}
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500861
862void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
863 VkFence fence, VkResult result) {
864 if (result != VK_SUCCESS) return;
865
866 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
867 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
868 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
869 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
870 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
871 if (!image_state)
872 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
873 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
874 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
875 image_state->sparse_metadata_bound = true;
876 }
877 }
878 }
879 }
880}
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700881
882bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -0600883 const VkClearAttachment* pAttachments, uint32_t rectCount,
884 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700885 bool skip = false;
886 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
887 if (!cb_node) return skip;
888
Camden Stockerf55721f2019-09-09 11:04:49 -0600889 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700890 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
891 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
892 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
893 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
894 // CmdClearAttachments.
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700895 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
896 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
897 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
898 report_data->FormatHandle(commandBuffer).c_str());
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700899 }
900
Camden Stockerf55721f2019-09-09 11:04:49 -0600901 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700902}