blob: aba816bcfbaf2051783ba790a35cddb8c98f50dc [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 Stockerb6dee342019-08-22 15:56:15 -060046 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
47 kVUID_BestPractices_CreateInstance_ExtensionMismatch,
48 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
49 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060050 }
51 }
52
53 return skip;
54}
55
56void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
57 VkInstance* pInstance) {
58 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
59}
60
61bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -050062 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -060063 bool skip = false;
64
65 // get API version of physical device passed when creating device.
66 VkPhysicalDeviceProperties physical_device_properties{};
67 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -050068 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -060069
70 // check api versions and warn if instance api Version is higher than version on device.
71 if (instance_api_version > device_api_version) {
72 std::string inst_api_name = GetAPIVersionName(instance_api_version);
73 std::string dev_api_name = GetAPIVersionName(device_api_version);
74
Camden Stockerb6dee342019-08-22 15:56:15 -060075 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
76 kVUID_BestPractices_CreateDevice_API_Mismatch,
77 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
78 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -060079 }
80
81 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
82 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stockerb6dee342019-08-22 15:56:15 -060083 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
84 kVUID_BestPractices_CreateInstance_ExtensionMismatch,
85 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
86 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060087 }
88 }
89
Camden83a9c372019-08-14 11:41:38 -060090 auto pd_state = GetPhysicalDeviceState(physicalDevice);
Corta48da1d2019-09-20 18:59:07 +020091 if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Camden Stockerb6dee342019-08-22 15:56:15 -060092 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
93 kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
94 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -060095 }
96
Camden5b184be2019-08-13 07:50:19 -060097 return skip;
98}
99
100bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500101 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600102 bool skip = false;
103
104 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
105 std::stringstream bufferHex;
106 bufferHex << "0x" << std::hex << HandleToUint64(pBuffer);
107
108 skip |=
Camden Stockerb6dee342019-08-22 15:56:15 -0600109 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
110 kVUID_BestPractices_SharingModeExclusive,
Camden5b184be2019-08-13 07:50:19 -0600111 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
112 "(queueFamilyIndexCount of %" PRIu32 ").",
113 bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
114 }
115
116 return skip;
117}
118
119bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500120 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600121 bool skip = false;
122
123 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
124 std::stringstream imageHex;
125 imageHex << "0x" << std::hex << HandleToUint64(pImage);
126
127 skip |=
Camden Stockerb6dee342019-08-22 15:56:15 -0600128 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
129 kVUID_BestPractices_SharingModeExclusive,
Camden5b184be2019-08-13 07:50:19 -0600130 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
131 "(queueFamilyIndexCount of %" PRIu32 ").",
132 imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
133 }
134
135 return skip;
136}
137
138bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500139 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600140 bool skip = false;
141
Camden83a9c372019-08-14 11:41:38 -0600142 auto physical_device_state = GetPhysicalDeviceState();
143
144 if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
145 skip |= log_msg(
Camden Stockerb6dee342019-08-22 15:56:15 -0600146 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
147 kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600148 "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
149 }
150
151 if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600152 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
153 kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
154 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
155 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600156 }
157
158 if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
159 skip |=
Camden Stockerb6dee342019-08-22 15:56:15 -0600160 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
161 kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600162 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
163 }
164
Camden5b184be2019-08-13 07:50:19 -0600165 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600166 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
167 kVUID_BestPractices_SharingModeExclusive,
168 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while "
169 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
170 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600171 }
172
173 return skip;
174}
175
176bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
177 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500178 const VkAllocationCallbacks* pAllocator,
179 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600180 bool skip = false;
181
182 for (uint32_t i = 0; i < swapchainCount; i++) {
183 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600184 skip |=
185 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
186 kVUID_BestPractices_SharingModeExclusive,
187 "Warning: A shared swapchain (index %" PRIu32
188 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
189 "queues (queueFamilyIndexCount of %" PRIu32 ").",
190 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600191 }
192 }
193
194 return skip;
195}
196
197bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500198 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600199 bool skip = false;
200
201 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
202 VkFormat format = pCreateInfo->pAttachments[i].format;
203 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
204 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
205 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
206 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600207 kVUID_BestPractices_RenderPass_Attatchment,
Camden5b184be2019-08-13 07:50:19 -0600208 "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.");
212 }
213 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
214 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600215 kVUID_BestPractices_RenderPass_Attatchment,
Camden5b184be2019-08-13 07:50:19 -0600216 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
217 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
218 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
219 "image truely is undefined at the start of the render pass.");
220 }
221 }
222 }
223
224 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
225 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
226 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
227 }
228
229 return skip;
230}
231
232bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500233 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600234 bool skip = false;
235
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500236 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Camden5b184be2019-08-13 07:50:19 -0600237 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600238 kVUID_BestPractices_AllocateMemory_TooManyObjects,
239 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600240 }
241
Camden83a9c372019-08-14 11:41:38 -0600242 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
243
244 return skip;
245}
246
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500247void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
248 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
249 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700250 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
251
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500252 if (VK_SUCCESS == result) {
253 num_mem_objects++;
254 }
255}
256
Jeff Bolz5c801d12019-10-09 10:38:45 -0500257bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
258 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700259 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600260 bool skip = false;
261
Camden Stocker9738af92019-10-16 13:54:03 -0700262 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600263
264 for (auto& obj : mem_info->obj_bindings) {
265 skip |= log_msg(report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, get_debug_report_enum[obj.type], 0, layer_name.c_str(),
266 "VK Object %s still has a reference to mem obj %s.", report_data->FormatHandle(obj).c_str(),
267 report_data->FormatHandle(mem_info->mem).c_str());
268 }
269
Camden5b184be2019-08-13 07:50:19 -0600270 return skip;
271}
272
273void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
274 if (memory != VK_NULL_HANDLE) {
275 num_mem_objects--;
276 }
277}
278
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500279bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600280 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500281 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600282
sfricke-samsunge2441192019-11-06 14:07:57 -0800283 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Camden Stockerb603cc82019-09-03 10:09:02 -0600284 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
285 kVUID_BestPractices_BufferMemReqNotCalled,
286 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
287 api_name, report_data->FormatHandle(buffer).c_str());
288 }
289
290 return skip;
291}
292
293bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500294 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600295 bool skip = false;
296 const char* api_name = "BindBufferMemory()";
297
298 skip |= ValidateBindBufferMemory(buffer, api_name);
299
300 return skip;
301}
302
303bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500304 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600305 char api_name[64];
306 bool skip = false;
307
308 for (uint32_t i = 0; i < bindInfoCount; i++) {
309 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
310 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
311 }
312
313 return skip;
314}
Camden Stockerb603cc82019-09-03 10:09:02 -0600315
316bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500317 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600318 char api_name[64];
319 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600320
Camden Stocker8b798ab2019-09-03 10:33:28 -0600321 for (uint32_t i = 0; i < bindInfoCount; i++) {
322 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
323 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
324 }
325
326 return skip;
327}
328
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500329bool BestPractices::ValidateBindImageMemory(VkImage image, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600330 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500331 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600332
sfricke-samsunge2441192019-11-06 14:07:57 -0800333 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600334 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
335 kVUID_BestPractices_ImageMemReqNotCalled,
336 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.", api_name,
337 report_data->FormatHandle(image).c_str());
338 }
339
340 return skip;
341}
342
343bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500344 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600345 bool skip = false;
346 const char* api_name = "vkBindImageMemory()";
347
348 skip |= ValidateBindImageMemory(image, api_name);
349
350 return skip;
351}
352
353bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500354 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600355 char api_name[64];
356 bool skip = false;
357
358 for (uint32_t i = 0; i < bindInfoCount; i++) {
359 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
360 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
361 }
362
363 return skip;
364}
365
366bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500367 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600368 char api_name[64];
369 bool skip = false;
370
371 for (uint32_t i = 0; i < bindInfoCount; i++) {
372 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
373 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
374 }
375
376 return skip;
377}
Camden83a9c372019-08-14 11:41:38 -0600378
Camden5b184be2019-08-13 07:50:19 -0600379bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
380 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600381 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500382 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600383 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
384 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600385
386 if ((createInfoCount > 1) && (!pipelineCache)) {
387 skip |=
388 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600389 kVUID_BestPractices_CreatePipelines_MultiplePipelines,
Camden5b184be2019-08-13 07:50:19 -0600390 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
391 "pipeline cache, which may help with performance");
392 }
393
394 return skip;
395}
396
397bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
398 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600399 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500400 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600401 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
402 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600403
404 if ((createInfoCount > 1) && (!pipelineCache)) {
405 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600406 kVUID_BestPractices_CreatePipelines_MultiplePipelines,
Camden5b184be2019-08-13 07:50:19 -0600407 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
408 "pipeline cache, which may help with performance");
409 }
410
411 return skip;
412}
413
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500414bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600415 bool skip = false;
416
417 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600418 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
419 kVUID_BestPractices_PipelineStageFlags,
420 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600421 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600422 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
423 kVUID_BestPractices_PipelineStageFlags,
424 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600425 }
426
427 return skip;
428}
429
Jeff Bolz5c801d12019-10-09 10:38:45 -0500430bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
431 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600432 bool skip = false;
433
434 for (uint32_t submit = 0; submit < submitCount; submit++) {
435 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
436 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
437 }
438 }
439
440 return skip;
441}
442
Jeff Bolz5c801d12019-10-09 10:38:45 -0500443bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600444 bool skip = false;
445
446 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
447
448 return skip;
449}
450
Jeff Bolz5c801d12019-10-09 10:38:45 -0500451bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
452 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600453 bool skip = false;
454
455 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
456
457 return skip;
458}
459
460bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
461 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
462 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
463 uint32_t bufferMemoryBarrierCount,
464 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
465 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500466 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600467 bool skip = false;
468
469 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
470 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
471
472 return skip;
473}
474
475bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
476 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
477 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
478 uint32_t bufferMemoryBarrierCount,
479 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
480 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500481 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600482 bool skip = false;
483
484 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
485 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
486
487 return skip;
488}
489
490bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500491 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600492 bool skip = false;
493
494 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
495
496 return skip;
497}
498
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700499// Generic function to handle validation for all CmdDraw* type functions
500bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
501 bool skip = false;
502 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
503 if (cb_state) {
504 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
505 const PIPELINE_STATE* pipeline_state = nullptr;
506 if (last_bound_it != cb_state->lastBound.cend()) {
507 pipeline_state = last_bound_it->second.pipeline_state;
508 }
509 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
510 // Verify vertex binding
511 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
512 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
513 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
514 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, HandleToUint64(cb_state->commandBuffer),
515 kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
516 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
517 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
518 report_data->FormatHandle(pipeline_state->pipeline).c_str());
519 }
520 }
521 }
522 return skip;
523}
524
Camden5b184be2019-08-13 07:50:19 -0600525bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500526 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600527 bool skip = false;
528
529 if (instanceCount == 0) {
530 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600531 kVUID_BestPractices_CmdDraw_InstanceCountZero,
532 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700533 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600534 }
535
536 return skip;
537}
538
539bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500540 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600541 bool skip = false;
542
543 if (instanceCount == 0) {
544 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600545 kVUID_BestPractices_CmdDraw_InstanceCountZero,
546 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600547 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700548 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
549
550 return skip;
551}
552
553bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
554 VkDeviceSize offset, VkBuffer countBuffer,
555 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
556 uint32_t stride) const {
557 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600558
559 return skip;
560}
561
562bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500563 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600564 bool skip = false;
565
566 if (drawCount == 0) {
567 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600568 kVUID_BestPractices_CmdDraw_DrawCountZero,
569 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700570 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600571 }
572
573 return skip;
574}
575
576bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(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) {
581 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Camden Stockerb6dee342019-08-22 15:56:15 -0600582 kVUID_BestPractices_CmdDraw_DrawCountZero,
583 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700584 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600585 }
586
587 return skip;
588}
589
590bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500591 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600592 bool skip = false;
593
594 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600595 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
596 kVUID_BestPractices_CmdDispatch_GroupCountZero,
597 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
598 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
599 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600600 }
601
602 return skip;
603}
Camden83a9c372019-08-14 11:41:38 -0600604
Camden Stocker9c051442019-11-06 14:28:43 -0800605bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
606 const char* api_name) const {
607 bool skip = false;
608 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
609
610 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
611 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
612 HandleToUint64(physicalDevice), kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
613 "Potential problem with calling %s() without first retrieving properties from "
614 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
615 api_name);
616 }
617
618 return skip;
619}
620
Camden83a9c372019-08-14 11:41:38 -0600621bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500622 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600623 bool skip = false;
624
Camden Stocker9c051442019-11-06 14:28:43 -0800625 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600626
Camden Stocker9c051442019-11-06 14:28:43 -0800627 return skip;
628}
629
630bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
631 uint32_t planeIndex,
632 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
633 bool skip = false;
634
635 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
636
637 return skip;
638}
639
640bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
641 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
642 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
643 bool skip = false;
644
645 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600646
647 return skip;
648}
Camden05de2d42019-08-19 10:23:56 -0600649
650bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500651 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600652 bool skip = false;
653
654 auto swapchain_state = GetSwapchainState(swapchain);
655
656 if (swapchain_state && pSwapchainImages) {
657 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
658 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
659 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
660 HandleToUint64(device), kVUID_Core_Swapchain_PriorCount,
661 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
662 "been seen for pSwapchainImages.");
663 }
664 }
665
666 return skip;
667}
668
669// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
670static bool ValidateCommonGetPhysicalDeviceQueueFamilyProperties(debug_report_data* report_data,
671 const PHYSICAL_DEVICE_STATE* pd_state,
672 uint32_t requested_queue_family_property_count, bool qfp_null,
673 const char* caller_name) {
674 bool skip = false;
675 if (!qfp_null) {
676 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
677 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
678 skip |= log_msg(
679 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
680 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_MissingQueryCount,
681 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
682 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
683 caller_name, caller_name);
684 // Then verify that pCount that is passed in on second call matches what was returned
685 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
686 skip |= log_msg(
687 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
688 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_CountMismatch,
689 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
690 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
691 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
692 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
693 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
694 }
695 }
696
697 return skip;
698}
699
Jeff Bolz5c801d12019-10-09 10:38:45 -0500700bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
701 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600702 bool skip = false;
703
704 for (uint32_t i = 0; i < bindInfoCount; i++) {
705 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
706 if (!as_state->memory_requirements_checked) {
707 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
708 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
709 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
710 skip |= log_msg(
711 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0,
712 kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
713 "vkBindAccelerationStructureMemoryNV(): "
714 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
715 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
716 }
717 }
718
719 return skip;
720}
721
Camden05de2d42019-08-19 10:23:56 -0600722bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
723 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500724 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600725 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
726 assert(physical_device_state);
727 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
728 (nullptr == pQueueFamilyProperties),
729 "vkGetPhysicalDeviceQueueFamilyProperties()");
730}
731
Jeff Bolz5c801d12019-10-09 10:38:45 -0500732bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
733 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
734 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600735 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
736 assert(physical_device_state);
737 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
738 (nullptr == pQueueFamilyProperties),
739 "vkGetPhysicalDeviceQueueFamilyProperties2()");
740}
741
Jeff Bolz5c801d12019-10-09 10:38:45 -0500742bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
743 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
744 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600745 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
746 assert(physical_device_state);
747 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
748 (nullptr == pQueueFamilyProperties),
749 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
750}
751
752bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
753 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500754 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600755 if (!pSurfaceFormats) return false;
756 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
757 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
758 bool skip = false;
759 if (call_state == UNCALLED) {
760 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
761 // previously call this function with a NULL value of pSurfaceFormats:
762 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
763 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_MustQueryCount,
764 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
765 "positive value has been seen for pSurfaceFormats.");
766 } else {
767 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400768 if (*pSurfaceFormatCount > prev_format_count) {
Camden05de2d42019-08-19 10:23:56 -0600769 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
770 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_CountMismatch,
771 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
772 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
773 "when pSurfaceFormatCount was NULL.",
774 *pSurfaceFormatCount, prev_format_count);
775 }
776 }
777 return skip;
778}
Camden Stocker23cc47d2019-09-03 14:53:57 -0600779
780bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500781 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600782 bool skip = false;
783
784 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
785 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
786 // 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 -0500787 std::unordered_set<const IMAGE_STATE*> sparse_images;
788 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
789 // in RecordQueueBindSparse.
790 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -0600791 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
792 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
793 const auto& image_bind = bindInfo.pImageBinds[i];
794 auto image_state = GetImageState(image_bind.image);
795 if (!image_state)
796 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
797 sparse_images.insert(image_state);
798 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
799 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
800 // For now just warning if sparse image binding occurs without calling to get reqs first
801 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
802 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
803 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
804 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
805 report_data->FormatHandle(image_state->image).c_str());
806 }
807 }
808 if (!image_state->memory_requirements_checked) {
809 // For now just warning if sparse image binding occurs without calling to get reqs first
810 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
811 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
812 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
813 "vkGetImageMemoryRequirements() to retrieve requirements.",
814 report_data->FormatHandle(image_state->image).c_str());
815 }
816 }
817 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
818 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
819 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
820 if (!image_state)
821 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
822 sparse_images.insert(image_state);
823 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
824 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
825 // For now just warning if sparse image binding occurs without calling to get reqs first
826 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
827 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
828 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
829 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
830 report_data->FormatHandle(image_state->image).c_str());
831 }
832 }
833 if (!image_state->memory_requirements_checked) {
834 // For now just warning if sparse image binding occurs without calling to get reqs first
835 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
836 HandleToUint64(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());
840 }
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
851 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
852 HandleToUint64(sparse_image_state->image), kVUID_Core_MemTrack_InvalidState,
853 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
854 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
855 report_data->FormatHandle(sparse_image_state->image).c_str());
856 }
857 }
858 }
859
860 return skip;
861}
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500862
863void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
864 VkFence fence, VkResult result) {
865 if (result != VK_SUCCESS) return;
866
867 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
868 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
869 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
870 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
871 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
872 if (!image_state)
873 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
874 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
875 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
876 image_state->sparse_metadata_bound = true;
877 }
878 }
879 }
880 }
881}
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700882
883bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -0600884 const VkClearAttachment* pAttachments, uint32_t rectCount,
885 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700886 bool skip = false;
887 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
888 if (!cb_node) return skip;
889
Camden Stockerf55721f2019-09-09 11:04:49 -0600890 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700891 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
892 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
893 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
894 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
895 // CmdClearAttachments.
896 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski427beff2019-12-20 11:26:20 -0700897 HandleToUint64(commandBuffer), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700898 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
899 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
900 report_data->FormatHandle(commandBuffer).c_str());
901 }
902
Camden Stockerf55721f2019-09-09 11:04:49 -0600903 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700904}