blob: 2d89a7f972dced2ddbe839dc416e45aa0eea4a81 [file] [log] [blame]
Camdeneaa86ea2019-07-26 11:00:09 -06001/* Copyright (c) 2015-2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2019 Valve Corporation
3 * Copyright (c) 2015-2019 LunarG, Inc.
4 *
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 {
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) {
264 skip |= log_msg(report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, get_debug_report_enum[obj.type], 0, layer_name.c_str(),
265 "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)) {
386 skip |=
387 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 -0600388 kVUID_BestPractices_CreatePipelines_MultiplePipelines,
Camden5b184be2019-08-13 07:50:19 -0600389 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
390 "pipeline cache, which may help with performance");
391 }
392
393 return skip;
394}
395
396bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
397 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600398 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500399 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600400 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
401 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600402
403 if ((createInfoCount > 1) && (!pipelineCache)) {
404 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 -0600405 kVUID_BestPractices_CreatePipelines_MultiplePipelines,
Camden5b184be2019-08-13 07:50:19 -0600406 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
407 "pipeline cache, which may help with performance");
408 }
409
410 return skip;
411}
412
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500413bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600414 bool skip = false;
415
416 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600417 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
418 kVUID_BestPractices_PipelineStageFlags,
419 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600420 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600421 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
422 kVUID_BestPractices_PipelineStageFlags,
423 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600424 }
425
426 return skip;
427}
428
Jeff Bolz5c801d12019-10-09 10:38:45 -0500429bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
430 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600431 bool skip = false;
432
433 for (uint32_t submit = 0; submit < submitCount; submit++) {
434 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
435 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
436 }
437 }
438
439 return skip;
440}
441
Jeff Bolz5c801d12019-10-09 10:38:45 -0500442bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600443 bool skip = false;
444
445 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
446
447 return skip;
448}
449
Jeff Bolz5c801d12019-10-09 10:38:45 -0500450bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
451 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600452 bool skip = false;
453
454 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
455
456 return skip;
457}
458
459bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
460 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
461 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
462 uint32_t bufferMemoryBarrierCount,
463 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
464 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500465 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600466 bool skip = false;
467
468 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
469 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
470
471 return skip;
472}
473
474bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
475 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
476 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
477 uint32_t bufferMemoryBarrierCount,
478 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
479 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500480 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600481 bool skip = false;
482
483 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
484 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
485
486 return skip;
487}
488
489bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500490 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600491 bool skip = false;
492
493 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
494
495 return skip;
496}
497
498bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500499 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600500 bool skip = false;
501
502 if (instanceCount == 0) {
503 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 -0600504 kVUID_BestPractices_CmdDraw_InstanceCountZero,
505 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600506 }
507
508 return skip;
509}
510
511bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500512 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600513 bool skip = false;
514
515 if (instanceCount == 0) {
516 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 -0600517 kVUID_BestPractices_CmdDraw_InstanceCountZero,
518 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600519 }
520
521 return skip;
522}
523
524bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500525 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600526 bool skip = false;
527
528 if (drawCount == 0) {
529 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 -0600530 kVUID_BestPractices_CmdDraw_DrawCountZero,
531 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600532 }
533
534 return skip;
535}
536
537bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500538 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600539 bool skip = false;
540
541 if (drawCount == 0) {
542 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 -0600543 kVUID_BestPractices_CmdDraw_DrawCountZero,
544 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600545 }
546
547 return skip;
548}
549
550bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500551 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600552 bool skip = false;
553
554 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600555 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
556 kVUID_BestPractices_CmdDispatch_GroupCountZero,
557 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
558 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
559 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600560 }
561
562 return skip;
563}
Camden83a9c372019-08-14 11:41:38 -0600564
565bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500566 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600567 bool skip = false;
568
569 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
570
571 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState != QUERY_DETAILS) {
Camden Stockerb6dee342019-08-22 15:56:15 -0600572 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
573 kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
574 "vkGetDisplayPlaneSupportedDisplaysKHR() called before getting diplay plane properties from "
575 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600576 }
577
578 return skip;
579}
Camden05de2d42019-08-19 10:23:56 -0600580
581bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500582 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600583 bool skip = false;
584
585 auto swapchain_state = GetSwapchainState(swapchain);
586
587 if (swapchain_state && pSwapchainImages) {
588 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
589 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
590 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
591 HandleToUint64(device), kVUID_Core_Swapchain_PriorCount,
592 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
593 "been seen for pSwapchainImages.");
594 }
595 }
596
597 return skip;
598}
599
600// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
601static bool ValidateCommonGetPhysicalDeviceQueueFamilyProperties(debug_report_data* report_data,
602 const PHYSICAL_DEVICE_STATE* pd_state,
603 uint32_t requested_queue_family_property_count, bool qfp_null,
604 const char* caller_name) {
605 bool skip = false;
606 if (!qfp_null) {
607 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
608 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
609 skip |= log_msg(
610 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
611 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_MissingQueryCount,
612 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
613 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
614 caller_name, caller_name);
615 // Then verify that pCount that is passed in on second call matches what was returned
616 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
617 skip |= log_msg(
618 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
619 HandleToUint64(pd_state->phys_device), kVUID_Core_DevLimit_CountMismatch,
620 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
621 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
622 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
623 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
624 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
625 }
626 }
627
628 return skip;
629}
630
Jeff Bolz5c801d12019-10-09 10:38:45 -0500631bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
632 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600633 bool skip = false;
634
635 for (uint32_t i = 0; i < bindInfoCount; i++) {
636 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
637 if (!as_state->memory_requirements_checked) {
638 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
639 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
640 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
641 skip |= log_msg(
642 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0,
643 kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
644 "vkBindAccelerationStructureMemoryNV(): "
645 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
646 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
647 }
648 }
649
650 return skip;
651}
652
Camden05de2d42019-08-19 10:23:56 -0600653bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
654 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500655 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600656 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
657 assert(physical_device_state);
658 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
659 (nullptr == pQueueFamilyProperties),
660 "vkGetPhysicalDeviceQueueFamilyProperties()");
661}
662
Jeff Bolz5c801d12019-10-09 10:38:45 -0500663bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
664 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
665 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600666 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
667 assert(physical_device_state);
668 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
669 (nullptr == pQueueFamilyProperties),
670 "vkGetPhysicalDeviceQueueFamilyProperties2()");
671}
672
Jeff Bolz5c801d12019-10-09 10:38:45 -0500673bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
674 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
675 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600676 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
677 assert(physical_device_state);
678 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(report_data, physical_device_state, *pQueueFamilyPropertyCount,
679 (nullptr == pQueueFamilyProperties),
680 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
681}
682
683bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
684 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500685 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600686 if (!pSurfaceFormats) return false;
687 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
688 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
689 bool skip = false;
690 if (call_state == UNCALLED) {
691 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
692 // previously call this function with a NULL value of pSurfaceFormats:
693 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
694 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_MustQueryCount,
695 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
696 "positive value has been seen for pSurfaceFormats.");
697 } else {
698 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400699 if (*pSurfaceFormatCount > prev_format_count) {
Camden05de2d42019-08-19 10:23:56 -0600700 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
701 HandleToUint64(physicalDevice), kVUID_Core_DevLimit_CountMismatch,
702 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
703 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
704 "when pSurfaceFormatCount was NULL.",
705 *pSurfaceFormatCount, prev_format_count);
706 }
707 }
708 return skip;
709}
Camden Stocker23cc47d2019-09-03 14:53:57 -0600710
711bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500712 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600713 bool skip = false;
714
715 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
716 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
717 // 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 -0500718 std::unordered_set<const IMAGE_STATE*> sparse_images;
719 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
720 // in RecordQueueBindSparse.
721 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -0600722 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
723 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
724 const auto& image_bind = bindInfo.pImageBinds[i];
725 auto image_state = GetImageState(image_bind.image);
726 if (!image_state)
727 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
728 sparse_images.insert(image_state);
729 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
730 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
731 // For now just warning if sparse image binding occurs without calling to get reqs first
732 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
733 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
734 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
735 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
736 report_data->FormatHandle(image_state->image).c_str());
737 }
738 }
739 if (!image_state->memory_requirements_checked) {
740 // For now just warning if sparse image binding occurs without calling to get reqs first
741 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
742 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
743 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
744 "vkGetImageMemoryRequirements() to retrieve requirements.",
745 report_data->FormatHandle(image_state->image).c_str());
746 }
747 }
748 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
749 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
750 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
751 if (!image_state)
752 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
753 sparse_images.insert(image_state);
754 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
755 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
756 // For now just warning if sparse image binding occurs without calling to get reqs first
757 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
758 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
759 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
760 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
761 report_data->FormatHandle(image_state->image).c_str());
762 }
763 }
764 if (!image_state->memory_requirements_checked) {
765 // For now just warning if sparse image binding occurs without calling to get reqs first
766 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
767 HandleToUint64(image_state->image), kVUID_Core_MemTrack_InvalidState,
768 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
769 "vkGetImageMemoryRequirements() to retrieve requirements.",
770 report_data->FormatHandle(image_state->image).c_str());
771 }
772 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
773 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500774 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -0600775 }
776 }
777 }
778 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500779 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
780 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600781 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
782 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
783 HandleToUint64(sparse_image_state->image), kVUID_Core_MemTrack_InvalidState,
784 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
785 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
786 report_data->FormatHandle(sparse_image_state->image).c_str());
787 }
788 }
789 }
790
791 return skip;
792}
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500793
794void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
795 VkFence fence, VkResult result) {
796 if (result != VK_SUCCESS) return;
797
798 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
799 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
800 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
801 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
802 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
803 if (!image_state)
804 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
805 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
806 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
807 image_state->sparse_metadata_bound = true;
808 }
809 }
810 }
811 }
812}
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700813
814bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -0600815 const VkClearAttachment* pAttachments, uint32_t rectCount,
816 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700817 bool skip = false;
818 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
819 if (!cb_node) return skip;
820
Camden Stockerf55721f2019-09-09 11:04:49 -0600821 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700822 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
823 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
824 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
825 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
826 // CmdClearAttachments.
827 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
828 HandleToUint64(commandBuffer), kVUID_Core_DrawState_ClearCmdBeforeDraw,
829 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
830 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
831 report_data->FormatHandle(commandBuffer).c_str());
832 }
833
Camden Stockerf55721f2019-09-09 11:04:49 -0600834 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -0700835}