blob: c82d0229908896efa6d2143dbb1977a299b5277d [file] [log] [blame]
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -07001/* Copyright (c) 2015-2020 The Khronos Group Inc.
2 * Copyright (c) 2015-2020 Valve Corporation
3 * Copyright (c) 2015-2020 LunarG, Inc.
Camdeneaa86ea2019-07-26 11:00:09 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: Camden Stocker <camden@lunarg.com>
18 */
19
Camden3231dcc2019-08-05 11:28:57 -060020#include "best_practices.h"
Camden5b184be2019-08-13 07:50:19 -060021#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060022#include "best_practices_error_enums.h"
Camden5b184be2019-08-13 07:50:19 -060023
24#include <string>
25#include <iomanip>
26
27// get the API name is proper format
Jeff Bolz46c0ea02019-10-09 13:06:29 -050028std::string BestPractices::GetAPIVersionName(uint32_t version) const {
Camden5b184be2019-08-13 07:50:19 -060029 std::stringstream version_name;
30 uint32_t major = VK_VERSION_MAJOR(version);
31 uint32_t minor = VK_VERSION_MINOR(version);
32 uint32_t patch = VK_VERSION_PATCH(version);
33
34 version_name << major << "." << minor << "." << patch << " (0x" << std::setfill('0') << std::setw(8) << std::hex << version
35 << ")";
36
37 return version_name.str();
38}
39
40bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -050041 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -060042 bool skip = false;
43
44 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
45 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -080046 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070047 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
48 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060049 }
Camden Stocker11ecf512020-01-21 16:06:49 -080050
51 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeprecatedExtensionNames)) {
52 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_DeprecatedExtension,
53 "vkCreateInstance(): Attempting to enable Deprecated Extension %s at CreateInstance time.",
54 pCreateInfo->ppEnabledExtensionNames[i]);
55 }
Camden5b184be2019-08-13 07:50:19 -060056 }
57
58 return skip;
59}
60
61void BestPractices::PreCallRecordCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
62 VkInstance* pInstance) {
63 instance_api_version = pCreateInfo->pApplicationInfo->apiVersion;
64}
65
66bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -050067 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -060068 bool skip = false;
69
70 // get API version of physical device passed when creating device.
71 VkPhysicalDeviceProperties physical_device_properties{};
72 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -050073 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -060074
75 // check api versions and warn if instance api Version is higher than version on device.
76 if (instance_api_version > device_api_version) {
77 std::string inst_api_name = GetAPIVersionName(instance_api_version);
78 std::string dev_api_name = GetAPIVersionName(device_api_version);
79
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070080 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
81 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
82 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -060083 }
84
85 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
86 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -080087 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -070088 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
89 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -060090 }
Camden Stocker11ecf512020-01-21 16:06:49 -080091 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeprecatedExtensionNames)) {
92 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_DeprecatedExtension,
93 "vkCreateDevice(): Attempting to enable Deprecated Extension %s at CreateDevice time.",
94 pCreateInfo->ppEnabledExtensionNames[i]);
95 }
Camden5b184be2019-08-13 07:50:19 -060096 }
97
Camden83a9c372019-08-14 11:41:38 -060098 auto pd_state = GetPhysicalDeviceState(physicalDevice);
Corta48da1d2019-09-20 18:59:07 +020099 if ((pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700100 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
101 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600102 }
103
Camden5b184be2019-08-13 07:50:19 -0600104 return skip;
105}
106
107bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500108 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600109 bool skip = false;
110
111 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
112 std::stringstream bufferHex;
113 bufferHex << "0x" << std::hex << HandleToUint64(pBuffer);
114
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700115 skip |= LogWarning(
116 device, kVUID_BestPractices_SharingModeExclusive,
117 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
118 "(queueFamilyIndexCount of %" PRIu32 ").",
119 bufferHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600120 }
121
122 return skip;
123}
124
125bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500126 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600127 bool skip = false;
128
129 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
130 std::stringstream imageHex;
131 imageHex << "0x" << std::hex << HandleToUint64(pImage);
132
133 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700134 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
135 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
136 "(queueFamilyIndexCount of %" PRIu32 ").",
137 imageHex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600138 }
139
140 return skip;
141}
142
143bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500144 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600145 bool skip = false;
146
Camden83a9c372019-08-14 11:41:38 -0600147 auto physical_device_state = GetPhysicalDeviceState();
148
149 if (physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700150 skip |= LogWarning(
151 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
Camden83a9c372019-08-14 11:41:38 -0600152 "vkCreateSwapchainKHR() called before getting surface capabilities from vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
153 }
154
155 if (physical_device_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700156 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
157 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
158 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
Camden83a9c372019-08-14 11:41:38 -0600159 }
160
161 if (physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700162 skip |= LogWarning(
163 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
164 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
Camden83a9c372019-08-14 11:41:38 -0600165 }
166
Camden5b184be2019-08-13 07:50:19 -0600167 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700168 skip |=
169 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
170 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCULSIVE while "
171 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
172 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600173 }
174
175 return skip;
176}
177
178bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
179 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500180 const VkAllocationCallbacks* pAllocator,
181 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600182 bool skip = false;
183
184 for (uint32_t i = 0; i < swapchainCount; i++) {
185 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700186 skip |= LogWarning(
187 device, kVUID_BestPractices_SharingModeExclusive,
188 "Warning: A shared swapchain (index %" PRIu32
189 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
190 "queues (queueFamilyIndexCount of %" PRIu32 ").",
191 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600192 }
193 }
194
195 return skip;
196}
197
198bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500199 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600200 bool skip = false;
201
202 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
203 VkFormat format = pCreateInfo->pAttachments[i].format;
204 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
205 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
206 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700207 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
208 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
209 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
210 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
211 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600212 }
213 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700214 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
215 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
216 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
217 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
218 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600219 }
220 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000221
222 const auto& attachment = pCreateInfo->pAttachments[i];
223 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
224 bool access_requires_memory =
225 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
226
227 if (FormatHasStencil(format)) {
228 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
229 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
230 }
231
232 if (access_requires_memory) {
233 skip |= LogPerformanceWarning(
234 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
235 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
236 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
237 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
238 i, static_cast<uint32_t>(attachment.samples));
239 }
240 }
Camden5b184be2019-08-13 07:50:19 -0600241 }
242
243 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
244 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
245 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
246 }
247
248 return skip;
249}
250
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000251bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
252 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
253 bool skip = false;
254
255 // Check for non-transient attachments that should be transient and vice versa
256 auto rp_state = GetRenderPassState(pCreateInfo->renderPass);
257 if (rp_state) {
258 const VkRenderPassCreateInfo2* rpci = rp_state->createInfo.ptr();
259 const VkImageView* image_views = pCreateInfo->pAttachments;
260
261 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
262 auto& attachment = rpci->pAttachments[i];
263 bool attachment_should_be_transient =
264 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
265
266 if (FormatHasStencil(attachment.format)) {
267 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
268 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
269 }
270
271 auto view_state = GetImageViewState(image_views[i]);
272 if (view_state) {
273 auto& ivci = view_state->create_info;
274 auto& ici = GetImageState(ivci.image)->createInfo;
275
276 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
277
278 // The check for an image that should not be transient applies to all GPUs
279 if (!attachment_should_be_transient && image_is_transient) {
280 skip |= LogPerformanceWarning(
281 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
282 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
283 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
284 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
285 i);
286 }
287
288 bool supports_lazy = false;
289 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
290 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
291 supports_lazy = true;
292 }
293 }
294
295 // The check for an image that should be transient only applies to GPUs supporting
296 // lazily allocated memory
297 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
298 skip |= LogPerformanceWarning(
299 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
300 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
301 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
302 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
303 i);
304 }
305 }
306 }
307 }
308
309 return skip;
310}
311
Camden5b184be2019-08-13 07:50:19 -0600312bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500313 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600314 bool skip = false;
315
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500316 if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700317 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
318 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600319 }
320
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000321 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
322 skip |= LogPerformanceWarning(
323 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
324 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %llu. This is a very small allocation (current "
325 "threshold is %llu bytes). "
326 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
327 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
328 }
329
Camden83a9c372019-08-14 11:41:38 -0600330 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
331
332 return skip;
333}
334
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500335void BestPractices::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
336 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
337 VkResult result) {
Camden Stocker9738af92019-10-16 13:54:03 -0700338 ValidationStateTracker::PostCallRecordAllocateMemory(device, pAllocateInfo, pAllocator, pMemory, result);
339
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500340 if (VK_SUCCESS == result) {
341 num_mem_objects++;
342 }
343}
344
Jeff Bolz5c801d12019-10-09 10:38:45 -0500345bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
346 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700347 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600348 bool skip = false;
349
Camden Stocker9738af92019-10-16 13:54:03 -0700350 const DEVICE_MEMORY_STATE* mem_info = ValidationStateTracker::GetDevMemState(memory);
Camden83a9c372019-08-14 11:41:38 -0600351
352 for (auto& obj : mem_info->obj_bindings) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700353 skip |= LogWarning(device, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
354 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem).c_str());
Camden83a9c372019-08-14 11:41:38 -0600355 }
356
Camden5b184be2019-08-13 07:50:19 -0600357 return skip;
358}
359
360void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
361 if (memory != VK_NULL_HANDLE) {
362 num_mem_objects--;
363 }
364}
365
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000366bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600367 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500368 const BUFFER_STATE* buffer_state = GetBufferState(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600369
sfricke-samsunge2441192019-11-06 14:07:57 -0800370 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700371 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
372 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
373 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600374 }
375
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000376 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
377
378 if (mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
379 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
380 skip |= LogPerformanceWarning(
381 device, kVUID_BestPractices_SmallDedicatedAllocation,
382 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
383 "The required size of the allocation is %llu, but smaller buffers like this should be sub-allocated from "
384 "larger memory blocks. (Current threshold is %llu bytes.)",
385 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
386 }
387
Camden Stockerb603cc82019-09-03 10:09:02 -0600388 return skip;
389}
390
391bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500392 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600393 bool skip = false;
394 const char* api_name = "BindBufferMemory()";
395
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000396 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600397
398 return skip;
399}
400
401bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500402 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600403 char api_name[64];
404 bool skip = false;
405
406 for (uint32_t i = 0; i < bindInfoCount; i++) {
407 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000408 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600409 }
410
411 return skip;
412}
Camden Stockerb603cc82019-09-03 10:09:02 -0600413
414bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500415 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600416 char api_name[64];
417 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600418
Camden Stocker8b798ab2019-09-03 10:33:28 -0600419 for (uint32_t i = 0; i < bindInfoCount; i++) {
420 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000421 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600422 }
423
424 return skip;
425}
426
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000427bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600428 bool skip = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500429 const IMAGE_STATE* image_state = GetImageState(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600430
sfricke-samsunge2441192019-11-06 14:07:57 -0800431 if (!image_state->memory_requirements_checked && !image_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700432 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
433 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
434 api_name, report_data->FormatHandle(image).c_str());
Camden Stocker8b798ab2019-09-03 10:33:28 -0600435 }
436
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000437 const DEVICE_MEMORY_STATE* mem_state = GetDevMemState(memory);
438
439 if (mem_state->alloc_info.allocationSize == image_state->requirements.size &&
440 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
441 skip |= LogPerformanceWarning(
442 device, kVUID_BestPractices_SmallDedicatedAllocation,
443 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
444 "The required size of the allocation is %llu, but smaller images like this should be sub-allocated from "
445 "larger memory blocks. (Current threshold is %llu bytes.)",
446 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
447 }
448
449 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
450 // make sure this type is actually used.
451 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
452 // (i.e.most tile - based renderers)
453 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
454 bool supports_lazy = false;
455 uint32_t suggested_type = 0;
456
457 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
458 if ((1u << i) & image_state->requirements.memoryTypeBits) {
459 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
460 supports_lazy = true;
461 suggested_type = i;
462 break;
463 }
464 }
465 }
466
467 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
468
469 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
470 skip |= LogPerformanceWarning(
471 device, kVUID_BestPractices_NonLazyTransientImage,
472 "%s: Attempting to bind memory type % u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
473 "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
474 "%llu bytes of physical memory.",
475 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements.size);
476 }
477 }
478
Camden Stocker8b798ab2019-09-03 10:33:28 -0600479 return skip;
480}
481
482bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500483 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600484 bool skip = false;
485 const char* api_name = "vkBindImageMemory()";
486
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000487 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600488
489 return skip;
490}
491
492bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500493 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600494 char api_name[64];
495 bool skip = false;
496
497 for (uint32_t i = 0; i < bindInfoCount; i++) {
498 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000499 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600500 }
501
502 return skip;
503}
504
505bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500506 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600507 char api_name[64];
508 bool skip = false;
509
510 for (uint32_t i = 0; i < bindInfoCount; i++) {
511 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000512 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600513 }
514
515 return skip;
516}
Camden83a9c372019-08-14 11:41:38 -0600517
Camden5b184be2019-08-13 07:50:19 -0600518bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
519 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600520 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500521 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600522 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
523 pAllocator, pPipelines, cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600524
525 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700526 skip |= LogPerformanceWarning(
527 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
528 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
529 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600530 }
531
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000532 for (uint32_t i = 0; i < createInfoCount; i++) {
533 auto& createInfo = pCreateInfos[i];
534
535 auto& vertexInput = *createInfo.pVertexInputState;
536 uint32_t count = 0;
537 for (uint32_t j = 0; j < vertexInput.vertexBindingDescriptionCount; j++) {
538 if (vertexInput.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
539 count++;
540 }
541 }
542
543 if (count > kMaxInstancedVertexBuffers) {
544 skip |= LogPerformanceWarning(
545 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
546 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
547 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
548 count, kMaxInstancedVertexBuffers);
549 }
550 }
551
Camden5b184be2019-08-13 07:50:19 -0600552 return skip;
553}
554
555bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
556 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600557 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500558 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600559 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
560 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600561
562 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700563 skip |= LogPerformanceWarning(
564 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
565 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
566 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600567 }
568
569 return skip;
570}
571
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500572bool BestPractices::CheckPipelineStageFlags(std::string api_name, const VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -0600573 bool skip = false;
574
575 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700576 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
577 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600578 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700579 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
580 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600581 }
582
583 return skip;
584}
585
Jeff Bolz5c801d12019-10-09 10:38:45 -0500586bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
587 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -0600588 bool skip = false;
589
590 for (uint32_t submit = 0; submit < submitCount; submit++) {
591 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
592 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
593 }
594 }
595
596 return skip;
597}
598
Attilio Provenzano746e43e2020-02-27 11:23:50 +0000599bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
600 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
601 bool skip = false;
602
603 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
604 skip |= LogPerformanceWarning(
605 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
606 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
607 "pool instead.");
608 }
609
610 return skip;
611}
612
613bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
614 const VkCommandBufferBeginInfo* pBeginInfo) const {
615 bool skip = false;
616
617 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
618 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
619 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
620 }
621
622 return skip;
623}
624
Jeff Bolz5c801d12019-10-09 10:38:45 -0500625bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600626 bool skip = false;
627
628 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
629
630 return skip;
631}
632
Jeff Bolz5c801d12019-10-09 10:38:45 -0500633bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
634 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -0600635 bool skip = false;
636
637 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
638
639 return skip;
640}
641
642bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
643 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
644 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
645 uint32_t bufferMemoryBarrierCount,
646 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
647 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500648 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600649 bool skip = false;
650
651 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
652 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
653
654 return skip;
655}
656
657bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
658 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
659 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
660 uint32_t bufferMemoryBarrierCount,
661 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
662 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500663 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -0600664 bool skip = false;
665
666 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
667 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
668
669 return skip;
670}
671
672bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500673 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -0600674 bool skip = false;
675
676 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", pipelineStage);
677
678 return skip;
679}
680
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700681// Generic function to handle validation for all CmdDraw* type functions
682bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
683 bool skip = false;
684 const CMD_BUFFER_STATE* cb_state = GetCBState(cmd_buffer);
685 if (cb_state) {
686 const auto last_bound_it = cb_state->lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
687 const PIPELINE_STATE* pipeline_state = nullptr;
688 if (last_bound_it != cb_state->lastBound.cend()) {
689 pipeline_state = last_bound_it->second.pipeline_state;
690 }
691 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
692 // Verify vertex binding
693 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
694 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700695 skip |= LogPerformanceWarning(cb_state->commandBuffer, kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
696 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
697 report_data->FormatHandle(cb_state->commandBuffer).c_str(),
698 report_data->FormatHandle(pipeline_state->pipeline).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700699 }
700 }
701 }
702 return skip;
703}
704
Camden5b184be2019-08-13 07:50:19 -0600705bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500706 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600707 bool skip = false;
708
709 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700710 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
711 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700712 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -0600713 }
714
715 return skip;
716}
717
718bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500719 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600720 bool skip = false;
721
722 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700723 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
724 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -0600725 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700726 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
727
728 return skip;
729}
730
731bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
732 VkDeviceSize offset, VkBuffer countBuffer,
733 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
734 uint32_t stride) const {
735 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -0600736
737 return skip;
738}
739
740bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500741 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600742 bool skip = false;
743
744 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700745 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
746 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700747 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600748 }
749
750 return skip;
751}
752
753bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500754 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -0600755 bool skip = false;
756
757 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700758 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
759 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -0700760 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -0600761 }
762
763 return skip;
764}
765
766bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500767 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -0600768 bool skip = false;
769
770 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700771 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
772 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
773 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
774 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -0600775 }
776
777 return skip;
778}
Camden83a9c372019-08-14 11:41:38 -0600779
Camden Stocker9c051442019-11-06 14:28:43 -0800780bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
781 const char* api_name) const {
782 bool skip = false;
783 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
784
785 if (physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700786 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
787 "Potential problem with calling %s() without first retrieving properties from "
788 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
789 api_name);
Camden Stocker9c051442019-11-06 14:28:43 -0800790 }
791
792 return skip;
793}
794
Camden83a9c372019-08-14 11:41:38 -0600795bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500796 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -0600797 bool skip = false;
798
Camden Stocker9c051442019-11-06 14:28:43 -0800799 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -0600800
Camden Stocker9c051442019-11-06 14:28:43 -0800801 return skip;
802}
803
804bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
805 uint32_t planeIndex,
806 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
807 bool skip = false;
808
809 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
810
811 return skip;
812}
813
814bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
815 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
816 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
817 bool skip = false;
818
819 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -0600820
821 return skip;
822}
Camden05de2d42019-08-19 10:23:56 -0600823
824bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500825 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -0600826 bool skip = false;
827
828 auto swapchain_state = GetSwapchainState(swapchain);
829
830 if (swapchain_state && pSwapchainImages) {
831 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
832 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700833 skip |=
834 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
835 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
836 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -0600837 }
838 }
839
840 return skip;
841}
842
843// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700844bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* pd_state,
845 uint32_t requested_queue_family_property_count,
846 bool qfp_null, const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -0600847 bool skip = false;
848 if (!qfp_null) {
849 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
850 if (UNCALLED == pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700851 skip |= LogWarning(
852 pd_state->phys_device, kVUID_Core_DevLimit_MissingQueryCount,
Camden05de2d42019-08-19 10:23:56 -0600853 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is recommended "
854 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
855 caller_name, caller_name);
856 // Then verify that pCount that is passed in on second call matches what was returned
857 } else if (pd_state->queue_family_known_count != requested_queue_family_property_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700858 skip |= LogWarning(
859 pd_state->phys_device, kVUID_Core_DevLimit_CountMismatch,
Camden05de2d42019-08-19 10:23:56 -0600860 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
861 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
862 ". It is recommended to instead receive all the properties by calling %s with pQueueFamilyPropertyCount that was "
863 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
864 caller_name, requested_queue_family_property_count, pd_state->queue_family_known_count, caller_name, caller_name);
865 }
866 }
867
868 return skip;
869}
870
Jeff Bolz5c801d12019-10-09 10:38:45 -0500871bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
872 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -0600873 bool skip = false;
874
875 for (uint32_t i = 0; i < bindInfoCount; i++) {
876 const ACCELERATION_STRUCTURE_STATE* as_state = GetAccelerationStructureState(pBindInfos[i].accelerationStructure);
877 if (!as_state->memory_requirements_checked) {
878 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
879 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
880 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700881 skip |= LogWarning(
882 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -0600883 "vkBindAccelerationStructureMemoryNV(): "
884 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
885 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
886 }
887 }
888
889 return skip;
890}
891
Camden05de2d42019-08-19 10:23:56 -0600892bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
893 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500894 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600895 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
896 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700897 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600898 (nullptr == pQueueFamilyProperties),
899 "vkGetPhysicalDeviceQueueFamilyProperties()");
900}
901
Jeff Bolz5c801d12019-10-09 10:38:45 -0500902bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(
903 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
904 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600905 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
906 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700907 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600908 (nullptr == pQueueFamilyProperties),
909 "vkGetPhysicalDeviceQueueFamilyProperties2()");
910}
911
Jeff Bolz5c801d12019-10-09 10:38:45 -0500912bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
913 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount,
914 VkQueueFamilyProperties2KHR* pQueueFamilyProperties) const {
Camden05de2d42019-08-19 10:23:56 -0600915 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
916 assert(physical_device_state);
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700917 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
Camden05de2d42019-08-19 10:23:56 -0600918 (nullptr == pQueueFamilyProperties),
919 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
920}
921
922bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
923 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500924 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -0600925 if (!pSurfaceFormats) return false;
926 const auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
927 const auto& call_state = physical_device_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
928 bool skip = false;
929 if (call_state == UNCALLED) {
930 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
931 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700932 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
933 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
934 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -0600935 } else {
936 auto prev_format_count = (uint32_t)physical_device_state->surface_formats.size();
Peter Chene191bd72019-09-16 13:04:37 -0400937 if (*pSurfaceFormatCount > prev_format_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700938 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
939 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
940 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
941 "when pSurfaceFormatCount was NULL.",
942 *pSurfaceFormatCount, prev_format_count);
Camden05de2d42019-08-19 10:23:56 -0600943 }
944 }
945 return skip;
946}
Camden Stocker23cc47d2019-09-03 14:53:57 -0600947
948bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500949 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -0600950 bool skip = false;
951
952 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
953 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
954 // 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 -0500955 std::unordered_set<const IMAGE_STATE*> sparse_images;
956 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
957 // in RecordQueueBindSparse.
958 std::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -0600959 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
960 for (uint32_t i = 0; i < bindInfo.imageBindCount; ++i) {
961 const auto& image_bind = bindInfo.pImageBinds[i];
962 auto image_state = GetImageState(image_bind.image);
963 if (!image_state)
964 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
965 sparse_images.insert(image_state);
966 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
967 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
968 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700969 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
970 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
971 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
972 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600973 }
974 }
975 if (!image_state->memory_requirements_checked) {
976 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700977 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
978 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
979 "vkGetImageMemoryRequirements() to retrieve requirements.",
980 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600981 }
982 }
983 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
984 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
985 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
986 if (!image_state)
987 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
988 sparse_images.insert(image_state);
989 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
990 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
991 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700992 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
993 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
994 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
995 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -0600996 }
997 }
998 if (!image_state->memory_requirements_checked) {
999 // For now just warning if sparse image binding occurs without calling to get reqs first
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001000 skip |= LogWarning(image_state->image, kVUID_Core_MemTrack_InvalidState,
1001 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
1002 "vkGetImageMemoryRequirements() to retrieve requirements.",
1003 report_data->FormatHandle(image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001004 }
1005 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1006 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001007 sparse_images_with_metadata.insert(image_state);
Camden Stocker23cc47d2019-09-03 14:53:57 -06001008 }
1009 }
1010 }
1011 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001012 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
1013 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06001014 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001015 skip |= LogWarning(sparse_image_state->image, kVUID_Core_MemTrack_InvalidState,
1016 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
1017 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
1018 report_data->FormatHandle(sparse_image_state->image).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06001019 }
1020 }
1021 }
1022
1023 return skip;
1024}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001025
1026void BestPractices::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
1027 VkFence fence, VkResult result) {
1028 if (result != VK_SUCCESS) return;
1029
1030 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; bindIdx++) {
1031 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
1032 for (uint32_t i = 0; i < bindInfo.imageOpaqueBindCount; ++i) {
1033 const auto& image_opaque_bind = bindInfo.pImageOpaqueBinds[i];
1034 auto image_state = GetImageState(bindInfo.pImageOpaqueBinds[i].image);
1035 if (!image_state)
1036 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
1037 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
1038 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
1039 image_state->sparse_metadata_bound = true;
1040 }
1041 }
1042 }
1043 }
1044}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001045
1046bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06001047 const VkClearAttachment* pAttachments, uint32_t rectCount,
1048 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001049 bool skip = false;
1050 const CMD_BUFFER_STATE* cb_node = GetCBState(commandBuffer);
1051 if (!cb_node) return skip;
1052
Camden Stockerf55721f2019-09-09 11:04:49 -06001053 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001054 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
1055 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1056 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
1057 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1058 // CmdClearAttachments.
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001059 skip |= LogPerformanceWarning(commandBuffer, kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
1060 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds. It is recommended you "
1061 "use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1062 report_data->FormatHandle(commandBuffer).c_str());
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001063 }
1064
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001065 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
1066 // as it can be more efficient to just use LOAD_OP_CLEAR
1067 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass;
1068 if (rp) {
1069 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
1070
1071 for (uint32_t i = 0; i < attachmentCount; i++) {
1072 auto& attachment = pAttachments[i];
1073 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
1074 uint32_t color_attachment = attachment.colorAttachment;
1075 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
1076
1077 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1078 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1079 skip |= LogPerformanceWarning(
1080 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1081 "vkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
1082 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1083 "it is more efficient.",
1084 report_data->FormatHandle(commandBuffer).c_str(), color_attachment);
1085 }
1086 }
1087 }
1088
1089 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1090 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1091
1092 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1093 if (rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1094 skip |= LogPerformanceWarning(
1095 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1096 "vkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
1097 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1098 "it is more efficient.",
1099 report_data->FormatHandle(commandBuffer).c_str());
1100 }
1101 }
1102 }
1103
1104 if (subpass.pDepthStencilAttachment && attachment.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1105 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
1106
1107 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
1108 if (rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
1109 skip |= LogPerformanceWarning(
1110 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
1111 "vkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
1112 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
1113 "it is more efficient.",
1114 report_data->FormatHandle(commandBuffer).c_str());
1115 }
1116 }
1117 }
1118 }
1119 }
1120
Camden Stockerf55721f2019-09-09 11:04:49 -06001121 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07001122}