blob: a83a6a5310a052513a84213a76741d206822aa4e [file] [log] [blame]
Dustin Graves1e92cd72016-02-09 14:00:18 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Dustin Graves1e92cd72016-02-09 14:00:18 -07009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Dustin Graves1e92cd72016-02-09 14:00:18 -070011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Dustin Graves1e92cd72016-02-09 14:00:18 -070017 *
18 * Author: Dustin Graves <dustin@lunarg.com>
19 */
20
Mark Lobodzinski739391a2016-03-17 15:08:18 -060021#ifndef PARAMETER_VALIDATION_UTILS_H
22#define PARAMETER_VALIDATION_UTILS_H
Dustin Graves1e92cd72016-02-09 14:00:18 -070023
Dustin Graves58c2f662016-03-08 17:48:20 -070024#include <algorithm>
Dustin Graves29148ff2016-03-23 19:44:00 -060025#include <cstdlib>
Dustin Graves58c2f662016-03-08 17:48:20 -070026#include <string>
Mike Schuchardt47619c82017-05-31 09:14:22 -060027#include <bitset>
Dustin Graves58c2f662016-03-08 17:48:20 -070028
Dustin Graves1e92cd72016-02-09 14:00:18 -070029#include "vulkan/vulkan.h"
Dustin Graves58c2f662016-03-08 17:48:20 -070030#include "vk_enum_string_helper.h"
Dustin Graves1e92cd72016-02-09 14:00:18 -070031#include "vk_layer_logging.h"
Karl Schultza9ef1e52016-10-06 17:53:48 -060032#include "vk_validation_error_messages.h"
Mark Lobodzinski89cdf722017-06-01 15:09:55 -060033#include "vk_extension_helper.h"
Mark Lobodzinski944ec372017-05-30 14:21:21 -060034
Dustin Graves1e92cd72016-02-09 14:00:18 -070035
Dustin Graves8ffbbf62016-07-22 13:19:46 -060036#include "parameter_name.h"
37
Dustin Gravesb83fc2d2016-05-04 12:56:08 -060038namespace parameter_validation {
39
Mark Lobodzinski944ec372017-05-30 14:21:21 -060040struct instance_layer_data {
41 VkInstance instance = VK_NULL_HANDLE;
42
43 debug_report_data *report_data = nullptr;
44 std::vector<VkDebugReportCallbackEXT> logging_callback;
45
46 // The following are for keeping track of the temporary callbacks that can
47 // be used in vkCreateInstance and vkDestroyInstance:
48 uint32_t num_tmp_callbacks = 0;
49 VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos = nullptr;
50 VkDebugReportCallbackEXT *tmp_callbacks = nullptr;
51 InstanceExtensions extensions = {};
Mark Lobodzinski944ec372017-05-30 14:21:21 -060052 VkLayerInstanceDispatchTable dispatch_table = {};
53};
54
55struct layer_data {
56 debug_report_data *report_data = nullptr;
57 // Map for queue family index to queue count
58 std::unordered_map<uint32_t, uint32_t> queueFamilyIndexMap;
59 VkPhysicalDeviceLimits device_limits = {};
60 VkPhysicalDeviceFeatures physical_device_features = {};
61 VkPhysicalDevice physical_device = VK_NULL_HANDLE;
62 VkDevice device = VK_NULL_HANDLE;
Mark Lobodzinskib8626db2017-06-01 08:44:53 -060063 DeviceExtensions extensions;
Mark Lobodzinski944ec372017-05-30 14:21:21 -060064
65 VkLayerDispatchTable dispatch_table = {};
66};
67
Dustin Gravesf233e502016-05-05 13:44:21 -060068enum ErrorCode {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070069 NONE, // Used for INFO & other non-error messages
70 INVALID_USAGE, // The value of a parameter is not consistent
71 // with the valid usage criteria defined in
72 // the Vulkan specification.
73 INVALID_STRUCT_STYPE, // The sType field of a Vulkan structure does
74 // not contain the value expected for a structure
75 // of that type.
76 INVALID_STRUCT_PNEXT, // The pNext field of a Vulkan structure references
77 // a value that is not compatible with a structure of
78 // that type or is not NULL when a structure of that
79 // type has no compatible pNext values.
80 REQUIRED_PARAMETER, // A required parameter was specified as 0 or NULL.
81 RESERVED_PARAMETER, // A parameter reserved for future use was not
82 // specified as 0 or NULL.
83 UNRECOGNIZED_VALUE, // A Vulkan enumeration, VkFlags, or VkBool32 parameter
84 // contains a value that is not recognized as valid for
85 // that type.
86 DEVICE_LIMIT, // A specified parameter exceeds the limits returned
87 // by the physical device
88 DEVICE_FEATURE, // Use of a requested feature is not supported by
89 // the device
90 FAILURE_RETURN_CODE, // A Vulkan return code indicating a failure condition
91 // was encountered.
92 EXTENSION_NOT_ENABLED, // An extension entrypoint was called, but the required
93 // extension was not enabled at CreateInstance or
94 // CreateDevice time.
Dustin Gravesf233e502016-05-05 13:44:21 -060095};
96
Dustin Graves58c2f662016-03-08 17:48:20 -070097struct GenericHeader {
98 VkStructureType sType;
99 const void *pNext;
100};
Dustin Graves58c2f662016-03-08 17:48:20 -0700101
Dustin Graves29148ff2016-03-23 19:44:00 -0600102// Layer name string to be logged with validation messages.
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600103const char LayerName[] = "ParameterValidation";
Dustin Graves29148ff2016-03-23 19:44:00 -0600104
105// String returned by string_VkStructureType for an unrecognized type.
Dustin Graves58c2f662016-03-08 17:48:20 -0700106const std::string UnsupportedStructureTypeString = "Unhandled VkStructureType";
107
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600108// String returned by string_VkResult for an unrecognized type.
109const std::string UnsupportedResultString = "Unhandled VkResult";
110
Dustin Graves29148ff2016-03-23 19:44:00 -0600111// The base value used when computing the offset for an enumeration token value that is added by an extension.
112// When validating enumeration tokens, any value >= to this value is considered to be provided by an extension.
113// See Appendix C.10 "Assigning Extension Token Values" from the Vulkan specification
114const uint32_t ExtEnumBaseValue = 1000000000;
115
Cort Strattonedbe9b82017-05-16 07:38:35 -0700116// The value of all VK_xxx_MAX_ENUM tokens
117const uint32_t MaxEnumValue = 0x7FFFFFFF;
118
Mark Lobodzinski26112592017-05-30 12:02:17 -0600119// Forward declaration
Mark Lobodzinskic4452972017-05-31 09:14:22 -0600120template <typename T>
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600121bool OutputExtensionError(const T *layer_data, const std::string &api_name, const std::string &extension_name);
Mark Lobodzinski26112592017-05-30 12:02:17 -0600122
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700123template <typename T>
124bool is_extension_added_token(T value) {
Cort Strattonedbe9b82017-05-16 07:38:35 -0700125 return (value != MaxEnumValue) && (static_cast<uint32_t>(std::abs(static_cast<int32_t>(value))) >= ExtEnumBaseValue);
Dustin Graves29148ff2016-03-23 19:44:00 -0600126}
127
128// VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE token is a special case that was converted from a core token to an
129// extension added token. Its original value was intentionally preserved after the conversion, so it does not use
130// the base value that other extension added tokens use, and it does not fall within the enum's begin/end range.
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700131template <>
132bool is_extension_added_token(VkSamplerAddressMode value) {
Cort Strattonedbe9b82017-05-16 07:38:35 -0700133 bool result = is_extension_added_token(static_cast<uint32_t>(value));
134 return result || (value == VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE);
Dustin Graves29148ff2016-03-23 19:44:00 -0600135}
136
Dustin Graves1e92cd72016-02-09 14:00:18 -0700137/**
Dustin Gravesf8032f22016-05-11 18:31:44 -0600138* Validate a minimum value.
139*
140* Verify that the specified value is greater than the specified lower bound.
141*
142* @param report_data debug_report_data object for routing validation messages.
143* @param api_name Name of API call being validated.
144* @param parameter_name Name of parameter being validated.
145* @param value Value to validate.
146* @param lower_bound Lower bound value to use for validation.
147* @return Boolean value indicating that the call should be skipped.
148*/
149template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600150bool ValidateGreaterThan(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name, T value,
151 T lower_bound) {
Dustin Gravesf8032f22016-05-11 18:31:44 -0600152 bool skip_call = false;
153
154 if (value <= lower_bound) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -0600155 skip_call |=
156 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, 1, LayerName,
157 "%s: parameter %s must be greater than %d", api_name, parameter_name.get_name().c_str(), lower_bound);
Dustin Gravesf8032f22016-05-11 18:31:44 -0600158 }
159
160 return skip_call;
161}
162
163/**
Dustin Graves1e92cd72016-02-09 14:00:18 -0700164 * Validate a required pointer.
165 *
Dustin Graves58c2f662016-03-08 17:48:20 -0700166 * Verify that a required pointer is not NULL.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700167 *
168 * @param report_data debug_report_data object for routing validation messages.
169 * @param apiName Name of API call being validated.
170 * @param parameterName Name of parameter being validated.
171 * @param value Pointer to validate.
172 * @return Boolean value indicating that the call should be skipped.
173 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600174static bool validate_required_pointer(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinskidead0b62017-06-28 13:22:03 -0600175 const void *value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600176 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700177
178 if (value == NULL) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600179 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskidead0b62017-06-28 13:22:03 -0600180 vuid, LayerName, "%s: required parameter %s specified as NULL. %s", apiName,
181 parameterName.get_name().c_str(), validation_error_map[vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700182 }
183
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600184 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700185}
186
187/**
188 * Validate array count and pointer to array.
189 *
Dustin Graves58d114b2016-03-08 14:42:59 -0700190 * Verify that required count and array parameters are not 0 or NULL. If the
191 * count parameter is not optional, verify that it is not 0. If the array
192 * parameter is NULL, and it is not optional, verify that count is 0.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700193 *
194 * @param report_data debug_report_data object for routing validation messages.
195 * @param apiName Name of API call being validated.
196 * @param countName Name of count parameter.
197 * @param arrayName Name of array parameter.
198 * @param count Number of elements in the array.
199 * @param array Array to validate.
200 * @param countRequired The 'count' parameter may not be 0 when true.
201 * @param arrayRequired The 'array' parameter may not be NULL when true.
202 * @return Boolean value indicating that the call should be skipped.
203 */
204template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600205bool validate_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600206 const ParameterName &arrayName, T count, const void *array, bool countRequired, bool arrayRequired,
207 UNIQUE_VALIDATION_ERROR_CODE count_required_vuid, UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600208 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700209
210 // Count parameters not tagged as optional cannot be 0
Józef Kucia20bb8fb2016-09-23 12:45:04 +0200211 if (countRequired && (count == 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600212 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600213 count_required_vuid, LayerName, "%s: parameter %s must be greater than 0. %s", apiName,
214 countName.get_name().c_str(), validation_error_map[count_required_vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700215 }
216
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600217 // Array parameters not tagged as optional cannot be NULL, unless the count is 0
Dustin Graves080069b2016-04-05 13:48:15 -0600218 if ((array == NULL) && arrayRequired && (count != 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600219 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600220 array_required_vuid, LayerName, "%s: required parameter %s specified as NULL. %s", apiName,
221 arrayName.get_name().c_str(), validation_error_map[array_required_vuid]);
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600222 }
223
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600224 return skip_call;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600225}
226
227/**
228* Validate pointer to array count and pointer to array.
229*
230* Verify that required count and array parameters are not NULL. If count
231* is not NULL and its value is not optional, verify that it is not 0. If the
232* array parameter is NULL, and it is not optional, verify that count is 0.
233* The array parameter will typically be optional for this case (where count is
234* a pointer), allowing the caller to retrieve the available count.
235*
236* @param report_data debug_report_data object for routing validation messages.
237* @param apiName Name of API call being validated.
238* @param countName Name of count parameter.
239* @param arrayName Name of array parameter.
240* @param count Pointer to the number of elements in the array.
241* @param array Array to validate.
242* @param countPtrRequired The 'count' parameter may not be NULL when true.
243* @param countValueRequired The '*count' value may not be 0 when true.
244* @param arrayRequired The 'array' parameter may not be NULL when true.
245* @return Boolean value indicating that the call should be skipped.
246*/
247template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600248bool validate_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
249 const ParameterName &arrayName, const T *count, const void *array, bool countPtrRequired,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600250 bool countValueRequired, bool arrayRequired, UNIQUE_VALIDATION_ERROR_CODE count_required_vuid,
251 UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600252 bool skip_call = false;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600253
254 if (count == NULL) {
255 if (countPtrRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600256 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600257 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
258 countName.get_name().c_str());
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600259 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700260 } else {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600261 skip_call |= validate_array(report_data, apiName, countName, arrayName, array ? (*count) : 0, array, countValueRequired,
262 arrayRequired, count_required_vuid, array_required_vuid);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700263 }
264
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600265 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700266}
267
268/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600269 * Validate a pointer to a Vulkan structure.
270 *
271 * Verify that a required pointer to a structure is not NULL. If the pointer is
272 * not NULL, verify that each structure's sType field is set to the correct
273 * VkStructureType value.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700274 *
275 * @param report_data debug_report_data object for routing validation messages.
276 * @param apiName Name of API call being validated.
277 * @param parameterName Name of struct parameter being validated.
278 * @param sTypeName Name of expected VkStructureType value.
279 * @param value Pointer to the struct to validate.
280 * @param sType VkStructureType for structure validation.
281 * @param required The parameter may not be NULL when true.
282 * @return Boolean value indicating that the call should be skipped.
283 */
284template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600285bool validate_struct_type(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600286 const char *sTypeName, const T *value, VkStructureType sType, bool required,
287 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600288 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700289
290 if (value == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600291 if (required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600292 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
293 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
294 parameterName.get_name().c_str());
Dustin Graves1e92cd72016-02-09 14:00:18 -0700295 }
296 } else if (value->sType != sType) {
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600297 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, vuid,
298 LayerName, "%s: parameter %s->sType must be %s. %s", apiName, parameterName.get_name().c_str(),
299 sTypeName, validation_error_map[vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700300 }
301
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600302 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700303}
304
305/**
Dustin Graves1e92cd72016-02-09 14:00:18 -0700306 * Validate an array of Vulkan structures
307 *
308 * Verify that required count and array parameters are not 0 or NULL. If
309 * the array contains 1 or more structures, verify that each structure's
310 * sType field is set to the correct VkStructureType value.
311 *
312 * @param report_data debug_report_data object for routing validation messages.
313 * @param apiName Name of API call being validated.
314 * @param countName Name of count parameter.
315 * @param arrayName Name of array parameter.
316 * @param sTypeName Name of expected VkStructureType value.
317 * @param count Number of elements in the array.
318 * @param array Array to validate.
319 * @param sType VkStructureType for structure validation.
320 * @param countRequired The 'count' parameter may not be 0 when true.
321 * @param arrayRequired The 'array' parameter may not be NULL when true.
322 * @return Boolean value indicating that the call should be skipped.
323 */
324template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600325bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
326 const ParameterName &arrayName, const char *sTypeName, uint32_t count, const T *array,
327 VkStructureType sType, bool countRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600328 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700329
330 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600331 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
332 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700333 } else {
334 // Verify that all structs in the array have the correct type
335 for (uint32_t i = 0; i < count; ++i) {
336 if (array[i].sType != sType) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600337 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600338 __LINE__, INVALID_STRUCT_STYPE, LayerName, "%s: parameter %s[%d].sType must be %s", apiName,
339 arrayName.get_name().c_str(), i, sTypeName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700340 }
341 }
342 }
343
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600344 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700345}
346
Dustin Graves58d114b2016-03-08 14:42:59 -0700347/**
Mark Young39389872017-01-19 21:10:49 -0700348 * Validate an array of Vulkan structures.
349 *
350 * Verify that required count and array parameters are not NULL. If count
351 * is not NULL and its value is not optional, verify that it is not 0.
352 * If the array contains 1 or more structures, verify that each structure's
353 * sType field is set to the correct VkStructureType value.
354 *
355 * @param report_data debug_report_data object for routing validation messages.
356 * @param apiName Name of API call being validated.
357 * @param countName Name of count parameter.
358 * @param arrayName Name of array parameter.
359 * @param sTypeName Name of expected VkStructureType value.
360 * @param count Pointer to the number of elements in the array.
361 * @param array Array to validate.
362 * @param sType VkStructureType for structure validation.
363 * @param countPtrRequired The 'count' parameter may not be NULL when true.
364 * @param countValueRequired The '*count' value may not be 0 when true.
365 * @param arrayRequired The 'array' parameter may not be NULL when true.
366 * @return Boolean value indicating that the call should be skipped.
367 */
368template <typename T>
369bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
370 const ParameterName &arrayName, const char *sTypeName, uint32_t *count, const T *array,
371 VkStructureType sType, bool countPtrRequired, bool countValueRequired, bool arrayRequired) {
372 bool skip_call = false;
373
374 if (count == NULL) {
375 if (countPtrRequired) {
376 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
377 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
378 countName.get_name().c_str());
379 }
380 } else {
381 skip_call |= validate_struct_type_array(report_data, apiName, countName, arrayName, sTypeName, (*count), array, sType,
382 countValueRequired, arrayRequired);
383 }
384
385 return skip_call;
386}
387
388/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600389* Validate a Vulkan handle.
390*
391* Verify that the specified handle is not VK_NULL_HANDLE.
392*
393* @param report_data debug_report_data object for routing validation messages.
394* @param api_name Name of API call being validated.
395* @param parameter_name Name of struct parameter being validated.
396* @param value Handle to validate.
397* @return Boolean value indicating that the call should be skipped.
398*/
399template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600400bool validate_required_handle(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name, T value) {
Dustin Graves20fd66f2016-04-18 18:33:21 -0600401 bool skip_call = false;
402
403 if (value == VK_NULL_HANDLE) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600404 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
405 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as VK_NULL_HANDLE", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600406 parameter_name.get_name().c_str());
Dustin Graves20fd66f2016-04-18 18:33:21 -0600407 }
408
409 return skip_call;
410}
411
412/**
413* Validate an array of Vulkan handles.
414*
415* Verify that required count and array parameters are not NULL. If count
416* is not NULL and its value is not optional, verify that it is not 0.
417* If the array contains 1 or more handles, verify that no handle is set to
418* VK_NULL_HANDLE.
419*
420* @note This function is only intended to validate arrays of handles when none
421* of the handles are allowed to be VK_NULL_HANDLE. For arrays of handles
422* that are allowed to contain VK_NULL_HANDLE, use validate_array() instead.
423*
424* @param report_data debug_report_data object for routing validation messages.
425* @param api_name Name of API call being validated.
426* @param count_name Name of count parameter.
427* @param array_name Name of array parameter.
428* @param count Number of elements in the array.
429* @param array Array to validate.
430* @param count_required The 'count' parameter may not be 0 when true.
431* @param array_required The 'array' parameter may not be NULL when true.
432* @return Boolean value indicating that the call should be skipped.
433*/
434template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600435bool validate_handle_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
436 const ParameterName &array_name, uint32_t count, const T *array, bool count_required,
437 bool array_required) {
Dustin Graves20fd66f2016-04-18 18:33:21 -0600438 bool skip_call = false;
439
440 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600441 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required,
442 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600443 } else {
444 // Verify that no handles in the array are VK_NULL_HANDLE
445 for (uint32_t i = 0; i < count; ++i) {
446 if (array[i] == VK_NULL_HANDLE) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600447 skip_call |=
448 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
449 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as VK_NULL_HANDLE", api_name,
450 array_name.get_name().c_str(), i);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600451 }
452 }
453 }
454
455 return skip_call;
456}
457
458/**
Dustin Graves58d114b2016-03-08 14:42:59 -0700459 * Validate string array count and content.
460 *
461 * Verify that required count and array parameters are not 0 or NULL. If the
462 * count parameter is not optional, verify that it is not 0. If the array
463 * parameter is NULL, and it is not optional, verify that count is 0. If the
464 * array parameter is not NULL, verify that none of the strings are NULL.
465 *
466 * @param report_data debug_report_data object for routing validation messages.
467 * @param apiName Name of API call being validated.
468 * @param countName Name of count parameter.
469 * @param arrayName Name of array parameter.
470 * @param count Number of strings in the array.
471 * @param array Array of strings to validate.
472 * @param countRequired The 'count' parameter may not be 0 when true.
473 * @param arrayRequired The 'array' parameter may not be NULL when true.
474 * @return Boolean value indicating that the call should be skipped.
475 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600476static bool validate_string_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
477 const ParameterName &arrayName, uint32_t count, const char *const *array, bool countRequired,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600478 bool arrayRequired, UNIQUE_VALIDATION_ERROR_CODE count_required_vuid,
479 UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600480 bool skip_call = false;
Dustin Graves58d114b2016-03-08 14:42:59 -0700481
482 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600483 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
484 count_required_vuid, array_required_vuid);
Dustin Graves58d114b2016-03-08 14:42:59 -0700485 } else {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600486 // Verify that strings in the array are not NULL
Dustin Graves58d114b2016-03-08 14:42:59 -0700487 for (uint32_t i = 0; i < count; ++i) {
488 if (array[i] == NULL) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600489 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600490 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as NULL",
491 apiName, arrayName.get_name().c_str(), i);
Dustin Graves58d114b2016-03-08 14:42:59 -0700492 }
493 }
494 }
495
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600496 return skip_call;
Dustin Graves58d114b2016-03-08 14:42:59 -0700497}
498
Dustin Graves58c2f662016-03-08 17:48:20 -0700499/**
500 * Validate a structure's pNext member.
501 *
502 * Verify that the specified pNext value points to the head of a list of
503 * allowed extension structures. If no extension structures are allowed,
504 * verify that pNext is null.
505 *
506 * @param report_data debug_report_data object for routing validation messages.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600507 * @param api_name Name of API call being validated.
508 * @param parameter_name Name of parameter being validated.
509 * @param allowed_struct_names Names of allowed structs.
Dustin Graves58c2f662016-03-08 17:48:20 -0700510 * @param next Pointer to validate.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600511 * @param allowed_type_count Total number of allowed structure types.
512 * @param allowed_types Array of strcuture types allowed for pNext.
513 * @param header_version Version of header defining the pNext validation rules.
Dustin Graves58c2f662016-03-08 17:48:20 -0700514 * @return Boolean value indicating that the call should be skipped.
515 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600516static bool validate_struct_pnext(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600517 const char *allowed_struct_names, const void *next, size_t allowed_type_count,
Mark Lobodzinski3c828522017-06-26 13:05:57 -0600518 const VkStructureType *allowed_types, uint32_t header_version,
519 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600520 bool skip_call = false;
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600521 std::unordered_set<const void *> cycle_check;
522 std::unordered_set<VkStructureType, std::hash<int>> unique_stype_check;
523
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700524 const char disclaimer[] =
525 "This warning is based on the Valid Usage documentation for version %d of the Vulkan header. It "
526 "is possible that you are using a struct from a private extension or an extension that was added "
527 "to a later version of the Vulkan header, in which case your use of %s is perfectly valid but "
528 "is not guaranteed to work correctly with validation enabled";
Dustin Graves58c2f662016-03-08 17:48:20 -0700529
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600530 // TODO: The valid pNext structure types are not recursive. Each structure has its own list of valid sTypes for pNext.
531 // Codegen a map of vectors containing the allowable pNext types for each struct and use that here -- also simplifies parms.
Dustin Graves58c2f662016-03-08 17:48:20 -0700532 if (next != NULL) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600533 if (allowed_type_count == 0) {
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600534 std::string message = "%s: value of %s must be NULL. %s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600535 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600536 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600537 vuid, LayerName, message.c_str(), api_name, parameter_name.get_name().c_str(),
538 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700539 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600540 const VkStructureType *start = allowed_types;
541 const VkStructureType *end = allowed_types + allowed_type_count;
Dustin Graves58c2f662016-03-08 17:48:20 -0700542 const GenericHeader *current = reinterpret_cast<const GenericHeader *>(next);
543
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600544 cycle_check.insert(next);
Dustin Graves58c2f662016-03-08 17:48:20 -0700545
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600546 while (current != NULL) {
547 if (cycle_check.find(current->pNext) != cycle_check.end()) {
548 std::string message = "%s: %s chain contains a cycle -- pNext pointer " PRIx64 " is repeated.";
549 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
550 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
551 parameter_name.get_name().c_str(), reinterpret_cast<uint64_t>(next));
552 break;
553 } else {
554 cycle_check.insert(current->pNext);
555 }
556
557 std::string type_name = string_VkStructureType(current->sType);
558 if (unique_stype_check.find(current->sType) != unique_stype_check.end()) {
559 std::string message = "%s: %s chain contains duplicate structure types: %s appears multiple times.";
560 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
561 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
562 parameter_name.get_name().c_str(), type_name.c_str());
563 } else {
564 unique_stype_check.insert(current->sType);
565 }
566
567 if (std::find(start, end, current->sType) == end) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600568 if (type_name == UnsupportedStructureTypeString) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700569 std::string message =
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600570 "%s: %s chain includes a structure with unknown VkStructureType (%d); Allowed structures are [%s]. %s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600571 message += disclaimer;
572 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600573 0, __LINE__, vuid, LayerName, message.c_str(), api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600574 parameter_name.get_name().c_str(), current->sType, allowed_struct_names,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600575 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700576 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600577 std::string message =
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600578 "%s: %s chain includes a structure with unexpected VkStructureType %s; Allowed structures are [%s]. "
579 "%s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600580 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600581 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600582 0, __LINE__, vuid, LayerName, message.c_str(), api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600583 parameter_name.get_name().c_str(), type_name.c_str(), allowed_struct_names,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600584 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700585 }
586 }
Dustin Graves58c2f662016-03-08 17:48:20 -0700587 current = reinterpret_cast<const GenericHeader *>(current->pNext);
588 }
589 }
590 }
591
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600592 return skip_call;
Dustin Graves58c2f662016-03-08 17:48:20 -0700593}
594
Dustin Graves29148ff2016-03-23 19:44:00 -0600595/**
596* Validate a VkBool32 value.
597*
598* Generate a warning if a VkBool32 value is neither VK_TRUE nor VK_FALSE.
599*
600* @param report_data debug_report_data object for routing validation messages.
601* @param apiName Name of API call being validated.
602* @param parameterName Name of parameter being validated.
603* @param value Boolean value to validate.
604* @return Boolean value indicating that the call should be skipped.
605*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600606static bool validate_bool32(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
607 VkBool32 value) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600608 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600609
610 if ((value != VK_TRUE) && (value != VK_FALSE)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600611 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600612 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s (%d) is neither VK_TRUE nor VK_FALSE", apiName,
613 parameterName.get_name().c_str(), value);
Dustin Graves29148ff2016-03-23 19:44:00 -0600614 }
615
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600616 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600617}
618
619/**
620* Validate a Vulkan enumeration value.
621*
622* Generate a warning if an enumeration token value does not fall within the core enumeration
623* begin and end token values, and was not added to the enumeration by an extension. Extension
624* provided enumerations use the equation specified in Appendix C.10 of the Vulkan specification,
625* with 1,000,000,000 as the base token value.
626*
627* @note This function does not expect to process enumerations defining bitmask flag bits.
628*
629* @param report_data debug_report_data object for routing validation messages.
630* @param apiName Name of API call being validated.
631* @param parameterName Name of parameter being validated.
632* @param enumName Name of the enumeration being validated.
633* @param begin The begin range value for the enumeration.
634* @param end The end range value for the enumeration.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600635* @param value Enumeration value to validate.
Dustin Graves29148ff2016-03-23 19:44:00 -0600636* @return Boolean value indicating that the call should be skipped.
637*/
638template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600639bool validate_ranged_enum(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinski42eb3c32017-06-28 11:47:22 -0600640 const char *enumName, T begin, T end, T value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600641 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600642
643 if (((value < begin) || (value > end)) && !is_extension_added_token(value)) {
Mark Lobodzinski42eb3c32017-06-28 11:47:22 -0600644 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, vuid,
645 LayerName,
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700646 "%s: value of %s (%d) does not fall within the begin..end range of the core %s "
Mark Lobodzinski42eb3c32017-06-28 11:47:22 -0600647 "enumeration tokens and is not an extension added token. %s",
648 apiName, parameterName.get_name().c_str(), value, enumName, validation_error_map[vuid]);
Dustin Graves29148ff2016-03-23 19:44:00 -0600649 }
650
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600651 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600652}
653
654/**
655* Validate an array of Vulkan enumeration value.
656*
657* Process all enumeration token values in the specified array and generate a warning if a value
658* does not fall within the core enumeration begin and end token values, and was not added to
659* the enumeration by an extension. Extension provided enumerations use the equation specified
660* in Appendix C.10 of the Vulkan specification, with 1,000,000,000 as the base token value.
661*
662* @note This function does not expect to process enumerations defining bitmask flag bits.
663*
664* @param report_data debug_report_data object for routing validation messages.
665* @param apiName Name of API call being validated.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600666* @param countName Name of count parameter.
667* @param arrayName Name of array parameter.
Dustin Graves29148ff2016-03-23 19:44:00 -0600668* @param enumName Name of the enumeration being validated.
669* @param begin The begin range value for the enumeration.
670* @param end The end range value for the enumeration.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600671* @param count Number of enumeration values in the array.
672* @param array Array of enumeration values to validate.
673* @param countRequired The 'count' parameter may not be 0 when true.
674* @param arrayRequired The 'array' parameter may not be NULL when true.
Dustin Graves29148ff2016-03-23 19:44:00 -0600675* @return Boolean value indicating that the call should be skipped.
676*/
677template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600678static bool validate_ranged_enum_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
679 const ParameterName &arrayName, const char *enumName, T begin, T end, uint32_t count,
680 const T *array, bool countRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600681 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600682
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600683 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600684 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
685 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600686 } else {
687 for (uint32_t i = 0; i < count; ++i) {
688 if (((array[i] < begin) || (array[i] > end)) && !is_extension_added_token(array[i])) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600689 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600690 __LINE__, UNRECOGNIZED_VALUE, LayerName,
691 "%s: value of %s[%d] (%d) does not fall within the begin..end range of the core %s "
692 "enumeration tokens and is not an extension added token",
693 apiName, arrayName.get_name().c_str(), i, array[i], enumName);
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600694 }
Dustin Graves29148ff2016-03-23 19:44:00 -0600695 }
696 }
697
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600698 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600699}
700
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600701/**
Dustin Graves78df8512016-04-28 17:58:59 -0600702* Verify that a reserved VkFlags value is zero.
703*
704* Verify that the specified value is zero, to check VkFlags values that are reserved for
705* future use.
706*
707* @param report_data debug_report_data object for routing validation messages.
708* @param api_name Name of API call being validated.
709* @param parameter_name Name of parameter being validated.
710* @param value Value to validate.
711* @return Boolean value indicating that the call should be skipped.
712*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600713static bool validate_reserved_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Mark Lobodzinskid0b0c512017-06-28 12:06:41 -0600714 VkFlags value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Graves78df8512016-04-28 17:58:59 -0600715 bool skip_call = false;
716
717 if (value != 0) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600718 skip_call |=
719 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskid0b0c512017-06-28 12:06:41 -0600720 vuid, LayerName, "%s: parameter %s must be 0. %s", api_name, parameter_name.get_name().c_str(),
721 validation_error_map[vuid]);
Dustin Graves78df8512016-04-28 17:58:59 -0600722 }
723
724 return skip_call;
725}
726
727/**
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600728* Validate a Vulkan bitmask value.
729*
730* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
731* for that type.
732*
733* @param report_data debug_report_data object for routing validation messages.
734* @param api_name Name of API call being validated.
735* @param parameter_name Name of parameter being validated.
736* @param flag_bits_name Name of the VkFlags type being validated.
737* @param all_flags A bit mask combining all valid flag bits for the VkFlags type being validated.
738* @param value VkFlags value to validate.
739* @param flags_required The 'value' parameter may not be 0 when true.
Mike Schuchardt47619c82017-05-31 09:14:22 -0600740* @param singleFlag The 'value' parameter may not contain more than one bit from all_flags.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600741* @return Boolean value indicating that the call should be skipped.
742*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600743static bool validate_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -0600744 const char *flag_bits_name, VkFlags all_flags, VkFlags value, bool flags_required, bool singleFlag,
745 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600746 bool skip_call = false;
747
748 if (value == 0) {
749 if (flags_required) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600750 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -0600751 vuid, LayerName, "%s: value of %s must not be 0. %s", api_name,
752 parameter_name.get_name().c_str(), validation_error_map[vuid]);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600753 }
754 } else if ((value & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600755 skip_call |=
756 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
757 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s contains flag bits that are not recognized members of %s",
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600758 api_name, parameter_name.get_name().c_str(), flag_bits_name);
Mike Schuchardt47619c82017-05-31 09:14:22 -0600759 } else if (singleFlag && (std::bitset<sizeof(VkFlags) * 8>(value).count() > 1)) {
760 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
761 UNRECOGNIZED_VALUE, LayerName,
762 "%s: value of %s contains multiple members of %s when only a single value is allowed", api_name,
763 parameter_name.get_name().c_str(), flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600764 }
765
766 return skip_call;
767}
768
769/**
770* Validate an array of Vulkan bitmask values.
771*
772* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
773* for that type.
774*
775* @param report_data debug_report_data object for routing validation messages.
776* @param api_name Name of API call being validated.
777* @param count_name Name of parameter being validated.
778* @param array_name Name of parameter being validated.
779* @param flag_bits_name Name of the VkFlags type being validated.
780* @param all_flags A bitmask combining all valid flag bits for the VkFlags type being validated.
781* @param count Number of VkFlags values in the array.
782* @param array Array of VkFlags value to validate.
783* @param count_required The 'count' parameter may not be 0 when true.
784* @param array_required The 'array' parameter may not be NULL when true.
785* @return Boolean value indicating that the call should be skipped.
786*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600787static bool validate_flags_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
788 const ParameterName &array_name, const char *flag_bits_name, VkFlags all_flags, uint32_t count,
Dustin Graves78df8512016-04-28 17:58:59 -0600789 const VkFlags *array, bool count_required, bool array_required) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600790 bool skip_call = false;
791
792 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600793 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required,
794 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600795 } else {
796 // Verify that all VkFlags values in the array
797 for (uint32_t i = 0; i < count; ++i) {
Dustin Graves78df8512016-04-28 17:58:59 -0600798 if (array[i] == 0) {
799 // Current XML registry logic for validity generation uses the array parameter's optional tag to determine if
800 // elements in the array are allowed be 0
801 if (array_required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600802 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
803 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: value of %s[%d] must not be 0", api_name,
804 array_name.get_name().c_str(), i);
Dustin Graves78df8512016-04-28 17:58:59 -0600805 }
806 } else if ((array[i] & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600807 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
808 __LINE__, UNRECOGNIZED_VALUE, LayerName,
809 "%s: value of %s[%d] contains flag bits that are not recognized members of %s", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600810 array_name.get_name().c_str(), i, flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600811 }
812 }
813 }
814
815 return skip_call;
816}
817
818/**
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600819* Get VkResult code description.
820*
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600821* Returns a string describing the specified VkResult code. The description is based on the language in the Vulkan API
822* specification.
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600823*
824* @param value VkResult code to process.
825* @return String describing the specified VkResult code.
826*/
827static std::string get_result_description(VkResult result) {
828 // clang-format off
829 switch (result) {
830 case VK_SUCCESS: return "a command completed successfully";
831 case VK_NOT_READY: return "a fence or query has not yet completed";
832 case VK_TIMEOUT: return "a wait operation has not completed in the specified time";
833 case VK_EVENT_SET: return "an event is signaled";
834 case VK_EVENT_RESET: return "an event is unsignalled";
835 case VK_INCOMPLETE: return "a return array was too small for the result";
836 case VK_ERROR_OUT_OF_HOST_MEMORY: return "a host memory allocation has failed";
837 case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "a device memory allocation has failed";
Dustin Graves2adca842016-05-16 18:35:55 -0600838 case VK_ERROR_INITIALIZATION_FAILED: return "initialization of an object has failed";
839 case VK_ERROR_DEVICE_LOST: return "the logical device has been lost";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600840 case VK_ERROR_MEMORY_MAP_FAILED: return "mapping of a memory object has failed";
841 case VK_ERROR_LAYER_NOT_PRESENT: return "the specified layer does not exist";
842 case VK_ERROR_EXTENSION_NOT_PRESENT: return "the specified extension does not exist";
843 case VK_ERROR_FEATURE_NOT_PRESENT: return "the requested feature is not available on this device";
844 case VK_ERROR_INCOMPATIBLE_DRIVER: return "a Vulkan driver could not be found";
845 case VK_ERROR_TOO_MANY_OBJECTS: return "too many objects of the type have already been created";
846 case VK_ERROR_FORMAT_NOT_SUPPORTED: return "the requested format is not supported on this device";
847 case VK_ERROR_SURFACE_LOST_KHR: return "a surface is no longer available";
848 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: return "the requested window is already connected to another "
849 "VkSurfaceKHR object, or some other non-Vulkan surface object";
850 case VK_SUBOPTIMAL_KHR: return "an image became available, and the swapchain no longer "
851 "matches the surface properties exactly, but can still be used to "
852 "present to the surface successfully.";
853 case VK_ERROR_OUT_OF_DATE_KHR: return "a surface has changed in such a way that it is no "
854 "longer compatible with the swapchain";
855 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: return "the display used by a swapchain does not use the same "
856 "presentable image layout, or is incompatible in a way that prevents "
857 "sharing an image";
858 case VK_ERROR_VALIDATION_FAILED_EXT: return "API validation has detected an invalid use of the API";
859 case VK_ERROR_INVALID_SHADER_NV: return "one or more shaders failed to compile or link";
Eric Engestrombcbb0fd2016-04-02 22:06:13 +0100860 default: return "an error has occurred";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600861 };
862 // clang-format on
863}
864
865/**
866* Validate return code.
867*
868* Print a message describing the reason for failure when an error code is returned.
869*
870* @param report_data debug_report_data object for routing validation messages.
871* @param apiName Name of API call being validated.
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600872* @param ignore vector of VkResult return codes to be ignored
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600873* @param value VkResult value to validate.
874*/
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600875static void validate_result(debug_report_data *report_data, const char *apiName, std::vector<VkResult> const &ignore,
876 VkResult result) {
Chris Forbesf1f4e382016-10-13 14:44:03 +1300877 if (result < 0 && result != VK_ERROR_VALIDATION_FAILED_EXT) {
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600878 if (std::find(ignore.begin(), ignore.end(), result) != ignore.end()) {
879 return;
880 }
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600881 std::string resultName = string_VkResult(result);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600882 if (resultName == UnsupportedResultString) {
883 // Unrecognized result code
Dustin Gravesf233e502016-05-05 13:44:21 -0600884 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
885 FAILURE_RETURN_CODE, LayerName, "%s: returned a result code indicating that an error has occurred", apiName);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600886 } else {
887 std::string resultDesc = get_result_description(result);
Dustin Gravesf233e502016-05-05 13:44:21 -0600888 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
889 FAILURE_RETURN_CODE, LayerName, "%s: returned %s, indicating that %s", apiName, resultName.c_str(),
890 resultDesc.c_str());
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600891 }
892 }
893}
894
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700895} // namespace parameter_validation
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600896
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700897#endif // PARAMETER_VALIDATION_UTILS_H