blob: 7df810f252b54e44b0955bfec8352698a74a1ecb [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) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700237 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
238 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600239 }
240
Camden83a9c372019-08-14 11:41:38 -0600241 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
242
243 return skip;
244}
245
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500246void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
247 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
248 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700249 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
250
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500251 if (VK_SUCCESS == result) {
252 num_mem_objects++;
253 }
254}
255
Jeff Bolz5c801d12019-10-09 10:38:45 -0500256bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
257 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700258 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600259 bool skip = false;
260
Camden Stocker9738af92019-10-16 13:54:03 -0700261 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600262
263 for (auto& obj : mem_info->obj_bindings) {
Mark Lobodzinskic8a6d052020-01-29 15:39:16 -0700264 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, get_debug_report_enum[obj.type], 0, layer_name.c_str(),
Camden83a9c372019-08-14 11:41:38 -0600265 "VK Object %s still has a reference to mem obj %s.", report_data->FormatHandle(obj).c_str(),
266 report_data->FormatHandle(mem_info->mem).c_str());
267 }
268
Camden5b184be2019-08-13 07:50:19 -0600269 return skip;
270}
271
272void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
273 if (memory != VK_NULL_HANDLE) {
274 num_mem_objects--;
275 }
276}
277
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500278bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600279 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500280 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600281
sfricke-samsunge2441192019-11-06 14:07:57 -0800282 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Camden Stockerb603cc82019-09-03 10:09:02 -0600283 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
284 kVUID_BestPractices_BufferMemReqNotCalled,
285 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
286 api_name, report_data->FormatHandle(buffer).c_str());
287 }
288
289 return skip;
290}
291
292bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500293 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600294 bool skip = false;
295 const char* api_name = "BindBufferMemory()";
296
297 skip |= ValidateBindBufferMemory(buffer, api_name);
298
299 return skip;
300}
301
302bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500303 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600304 char api_name[64];
305 bool skip = false;
306
307 for (uint32_t i = 0; i < bindInfoCount; i++) {
308 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
309 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
310 }
311
312 return skip;
313}
Camden Stockerb603cc82019-09-03 10:09:02 -0600314
315bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500316 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600317 char api_name[64];
318 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600319
Camden Stocker8b798ab2019-09-03 10:33:28 -0600320 for (uint32_t i = 0; i < bindInfoCount; i++) {
321 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
322 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, api_name);
323 }
324
325 return skip;
326}
327
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500328bool BestPractices::ValidateBindImageMemory(VkImage image, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600329 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500330 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600331
sfricke-samsunge2441192019-11-06 14:07:57 -0800332 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600333 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
334 kVUID_BestPractices_ImageMemReqNotCalled,
335 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.", api_name,
336 report_data->FormatHandle(image).c_str());
337 }
338
339 return skip;
340}
341
342bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500343 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600344 bool skip = false;
345 const char* api_name = "vkBindImageMemory()";
346
347 skip |= ValidateBindImageMemory(image, api_name);
348
349 return skip;
350}
351
352bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500353 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600354 char api_name[64];
355 bool skip = false;
356
357 for (uint32_t i = 0; i < bindInfoCount; i++) {
358 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
359 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
360 }
361
362 return skip;
363}
364
365bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500366 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600367 char api_name[64];
368 bool skip = false;
369
370 for (uint32_t i = 0; i < bindInfoCount; i++) {
371 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
372 skip |= ValidateBindImageMemory(pBindInfos[i].image, api_name);
373 }
374
375 return skip;
376}
Camden83a9c372019-08-14 11:41:38 -0600377
Camden5b184be2019-08-13 07:50:19 -0600378bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
379 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600380 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500381 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600382 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
383 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600384
385 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700386 skip |= LogPerformanceWarning(
387 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
388 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
389 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600390 }
391
392 return skip;
393}
394
395bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
396 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600397 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500398 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600399 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
400 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600401
402 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700403 skip |= LogPerformanceWarning(
404 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
405 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
406 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600407 }
408
409 return skip;
410}
411
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500412bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600413 bool skip = false;
414
415 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600416 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
417 kVUID_BestPractices_PipelineStageFlags,
418 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600419 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600420 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
421 kVUID_BestPractices_PipelineStageFlags,
422 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600423 }
424
425 return skip;
426}
427
Jeff Bolz5c801d12019-10-09 10:38:45 -0500428bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
429 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600430 bool skip = false;
431
432 for (uint32_t submit = 0; submit < submitCount; submit++) {
433 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
434 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
435 }
436 }
437
438 return skip;
439}
440
Jeff Bolz5c801d12019-10-09 10:38:45 -0500441bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600442 bool skip = false;
443
444 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
445
446 return skip;
447}
448
Jeff Bolz5c801d12019-10-09 10:38:45 -0500449bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
450 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600451 bool skip = false;
452
453 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
454
455 return skip;
456}
457
458bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
459 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
460 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
461 uint32_t bufferMemoryBarrierCount,
462 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
463 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500464 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600465 bool skip = false;
466
467 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
468 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
469
470 return skip;
471}
472
473bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
474 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
475 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
476 uint32_t bufferMemoryBarrierCount,
477 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
478 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500479 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600480 bool skip = false;
481
482 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
483 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
484
485 return skip;
486}
487
488bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500489 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600490 bool skip = false;
491
492 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
493
494 return skip;
495}
496
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700497// Generic function to handle validation for all CmdDraw* type functions
498bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
499 bool skip = false;
500 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
501 if (cb_state) {
502 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
503 const PIPELINE_STATE* pipeline_state = nullptr;
504 if (last_bound_it != cb_state->lastBound.cend()) {
505 pipeline_state = last_bound_it->second.pipeline_state;
506 }
507 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
508 // Verify vertex binding
509 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
510 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700511 skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
512 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
513 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
514 report_data->FormatHandle(pipeline_state->pipeline).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700515 }
516 }
517 }
518 return skip;
519}
520
Camden5b184be2019-08-13 07:50:19 -0600521bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500522 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600523 bool skip = false;
524
525 if (instanceCount == 0) {
526 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 -0600527 kVUID_BestPractices_CmdDraw_InstanceCountZero,
528 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700529 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600530 }
531
532 return skip;
533}
534
535bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500536 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600537 bool skip = false;
538
539 if (instanceCount == 0) {
540 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 -0600541 kVUID_BestPractices_CmdDraw_InstanceCountZero,
542 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600543 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700544 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
545
546 return skip;
547}
548
549bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
550 VkDeviceSize offset, VkBuffer countBuffer,
551 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
552 uint32_t stride) const {
553 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600554
555 return skip;
556}
557
558bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500559 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600560 bool skip = false;
561
562 if (drawCount == 0) {
563 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 -0600564 kVUID_BestPractices_CmdDraw_DrawCountZero,
565 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700566 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600567 }
568
569 return skip;
570}
571
572bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500573 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600574 bool skip = false;
575
576 if (drawCount == 0) {
577 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 -0600578 kVUID_BestPractices_CmdDraw_DrawCountZero,
579 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700580 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600581 }
582
583 return skip;
584}
585
586bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500587 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600588 bool skip = false;
589
590 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600591 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
592 kVUID_BestPractices_CmdDispatch_GroupCountZero,
593 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
594 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
595 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600596 }
597
598 return skip;
599}
Camden83a9c372019-08-14 11:41:38 -0600600
Camden Stocker9c051442019-11-06 14:28:43 -0800601bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
602 const char* api_name) const {
603 bool skip = false;
604 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
605
606 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
607 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
608 HandleToUint64(physicalDevice), kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
609 "Potential problem with calling %s() without first retrieving properties from "
610 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
611 api_name);
612 }
613
614 return skip;
615}
616
Camden83a9c372019-08-14 11:41:38 -0600617bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500618 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600619 bool skip = false;
620
Camden Stocker9c051442019-11-06 14:28:43 -0800621 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600622
Camden Stocker9c051442019-11-06 14:28:43 -0800623 return skip;
624}
625
626bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
627 uint32_t planeIndex,
628 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
629 bool skip = false;
630
631 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
632
633 return skip;
634}
635
636bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
637 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
638 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
639 bool skip = false;
640
641 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600642
643 return skip;
644}
Camden05de2d42019-08-19 10:23:56 -0600645
646bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500647 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600648 bool skip = false;
649
650 auto swapchain_state = GetSwapchainState(swapchain);
651
652 if (swapchain_state && pSwapchainImages) {
653 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
654 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
655 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
656 HandleToUint64(device), kVUID_Core_Swapchain_PriorCount,
657 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
658 "been seen for pSwapchainImages.");
659 }
660 }
661
662 return skip;
663}
664
665// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
666static bool ValidateCommonGetPhysicalDeviceQueueFamilyProperties(debug_report_data* report_data,
667 const PHYSICAL_DEVICE_STATE* pd_state,
668 uint32_t requested_queue_family_property_count, bool qfp_null,
669 const char* caller_name) {
670 bool skip = false;
671 if (!qfp_null) {
672 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
673 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
674 skip |= log_msg(
675 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
676 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_MissingQueryCount,
677 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
678 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
679 caller_name, caller_name);
680 // Then verify that pCount that is passed in on second call matches what was returned
681 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
682 skip |= log_msg(
683 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
684 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_CountMismatch,
685 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
686 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
687 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
688 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
689 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
690 }
691 }
692
693 return skip;
694}
695
Jeff Bolz5c801d12019-10-09 10:38:45 -0500696bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
697 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600698 bool skip = false;
699
700 for (uint32_t i = 0; i < bindInfoCount; i++) {
701 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
702 if (!as_state->memory_requirements_checked) {
703 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
704 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
705 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
706 skip |= log_msg(
707 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0,
708 kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
709 "vkBindAccelerationStructureMemoryNV(): "
710 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
711 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
712 }
713 }
714
715 return skip;
716}
717
Camden05de2d42019-08-19 10:23:56 -0600718bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
719 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500720 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600721 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
722 assert(physical_device_state);
723 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
724 (nullptr == pQueueFamilyProperties),
725 "vkGetPhysicalDeviceQueueFamilyProperties()");
726}
727
Jeff Bolz5c801d12019-10-09 10:38:45 -0500728bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
729 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
730 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600731 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
732 assert(physical_device_state);
733 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
734 (nullptr == pQueueFamilyProperties),
735 "vkGetPhysicalDeviceQueueFamilyProperties2()");
736}
737
Jeff Bolz5c801d12019-10-09 10:38:45 -0500738bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
739 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
740 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600741 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
742 assert(physical_device_state);
743 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
744 (nullptr == pQueueFamilyProperties),
745 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
746}
747
748bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
749 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500750 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600751 if (!pSurfaceFormats) return false;
752 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
753 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
754 bool skip = false;
755 if (call_state == UNCALLED) {
756 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
757 // previously call this function with a NULL value of pSurfaceFormats:
758 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
759 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_MustQueryCount,
760 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
761 "positive value has been seen for pSurfaceFormats.");
762 } else {
763 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400764 if (*pSurfaceFormatCount > prev_format_count) {
Camden05de2d42019-08-19 10:23:56 -0600765 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
766 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_CountMismatch,
767 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
768 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
769 "when pSurfaceFormatCount was NULL.",
770 *pSurfaceFormatCount, prev_format_count);
771 }
772 }
773 return skip;
774}
Camden Stocker23cc47d2019-09-03 14:53:57 -0600775
776bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500777 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600778 bool skip = false;
779
780 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
781 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
782 // 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 -0500783 std::unordered_set<const IMAGE_STATE*> sparse_images;
784 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
785 // in RecordQueueBindSparse.
786 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -0600787 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
788 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
789 const auto& image_bind = bindInfo.pImageBinds[i];
790 auto image_state = GetImageState(image_bind.image);
791 if (!image_state)
792 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
793 sparse_images.insert(image_state);
794 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
795 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
796 // For now just warning if sparse image binding occurs without calling to get reqs first
797 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
798 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
799 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
800 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
801 report_data->FormatHandle(image_state->image).c_str());
802 }
803 }
804 if (!image_state->memory_requirements_checked) {
805 // For now just warning if sparse image binding occurs without calling to get reqs first
806 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
807 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
808 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
809 "vkGetImageMemoryRequirements() to retrieve requirements.",
810 report_data->FormatHandle(image_state->image).c_str());
811 }
812 }
813 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
814 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
815 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
816 if (!image_state)
817 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
818 sparse_images.insert(image_state);
819 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
820 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
821 // For now just warning if sparse image binding occurs without calling to get reqs first
822 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
823 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
824 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
825 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
826 report_data->FormatHandle(image_state->image).c_str());
827 }
828 }
829 if (!image_state->memory_requirements_checked) {
830 // For now just warning if sparse image binding occurs without calling to get reqs first
831 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
832 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
833 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
834 "vkGetImageMemoryRequirements() to retrieve requirements.",
835 report_data->FormatHandle(image_state->image).c_str());
836 }
837 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
838 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500839 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -0600840 }
841 }
842 }
843 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500844 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
845 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600846 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
847 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
848 HandleToUint64(sparse_image_state->image), kVUID_Core_MemTrack_InvalidState,
849 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
850 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
851 report_data->FormatHandle(sparse_image_state->image).c_str());
852 }
853 }
854 }
855
856 return skip;
857}
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500858
859void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
860 VkFence fence, VkResult result) {
861 if (result != VK_SUCCESS) return;
862
863 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
864 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
865 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
866 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
867 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
868 if (!image_state)
869 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
870 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
871 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
872 image_state->sparse_metadata_bound = true;
873 }
874 }
875 }
876 }
877}
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700878
879bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -0600880 const VkClearAttachment* pAttachments, uint32_t rectCount,
881 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700882 bool skip = false;
883 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
884 if (!cb_node) return skip;
885
Camden Stockerf55721f2019-09-09 11:04:49 -0600886 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700887 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
888 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
889 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
890 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
891 // CmdClearAttachments.
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700892 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
893 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
894 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
895 report_data->FormatHandle(commandBuffer).c_str());
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700896 }
897
Camden Stockerf55721f2019-09-09 11:04:49 -0600898 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700899}