blob: 8d60f926c7a3834acea3475198e269e553511b40 [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,
Dustin Graves080069b2016-04-05 13:48:15 -0600175 const void *value) {
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__,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600180 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
181 parameterName.get_name().c_str());
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,
206 const ParameterName &arrayName, T count, const void *array, bool countRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600207 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700208
209 // Count parameters not tagged as optional cannot be 0
Józef Kucia20bb8fb2016-09-23 12:45:04 +0200210 if (countRequired && (count == 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600211 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700212 REQUIRED_PARAMETER, LayerName, "%s: parameter %s must be greater than 0", apiName,
213 countName.get_name().c_str());
Dustin Graves1e92cd72016-02-09 14:00:18 -0700214 }
215
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600216 // Array parameters not tagged as optional cannot be NULL, unless the count is 0
Dustin Graves080069b2016-04-05 13:48:15 -0600217 if ((array == NULL) && arrayRequired && (count != 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600218 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700219 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
220 arrayName.get_name().c_str());
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600221 }
222
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600223 return skip_call;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600224}
225
226/**
227* Validate pointer to array count and pointer to array.
228*
229* Verify that required count and array parameters are not NULL. If count
230* is not NULL and its value is not optional, verify that it is not 0. If the
231* array parameter is NULL, and it is not optional, verify that count is 0.
232* The array parameter will typically be optional for this case (where count is
233* a pointer), allowing the caller to retrieve the available count.
234*
235* @param report_data debug_report_data object for routing validation messages.
236* @param apiName Name of API call being validated.
237* @param countName Name of count parameter.
238* @param arrayName Name of array parameter.
239* @param count Pointer to the number of elements in the array.
240* @param array Array to validate.
241* @param countPtrRequired The 'count' parameter may not be NULL when true.
242* @param countValueRequired The '*count' value may not be 0 when true.
243* @param arrayRequired The 'array' parameter may not be NULL when true.
244* @return Boolean value indicating that the call should be skipped.
245*/
246template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600247bool validate_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
248 const ParameterName &arrayName, const T *count, const void *array, bool countPtrRequired,
249 bool countValueRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600250 bool skip_call = false;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600251
252 if (count == NULL) {
253 if (countPtrRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600254 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 -0600255 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
256 countName.get_name().c_str());
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600257 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700258 } else {
Chris Forbesb2b43482017-06-06 16:05:26 -0700259 skip_call |= validate_array(report_data, apiName, countName, arrayName, array ? (*count) : 0, array, countValueRequired, arrayRequired);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700260 }
261
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600262 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700263}
264
265/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600266 * Validate a pointer to a Vulkan structure.
267 *
268 * Verify that a required pointer to a structure is not NULL. If the pointer is
269 * not NULL, verify that each structure's sType field is set to the correct
270 * VkStructureType value.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700271 *
272 * @param report_data debug_report_data object for routing validation messages.
273 * @param apiName Name of API call being validated.
274 * @param parameterName Name of struct parameter being validated.
275 * @param sTypeName Name of expected VkStructureType value.
276 * @param value Pointer to the struct to validate.
277 * @param sType VkStructureType for structure validation.
278 * @param required The parameter may not be NULL when true.
279 * @return Boolean value indicating that the call should be skipped.
280 */
281template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600282bool validate_struct_type(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600283 const char *sTypeName, const T *value, VkStructureType sType, bool required,
284 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600285 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700286
287 if (value == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600288 if (required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600289 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
290 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
291 parameterName.get_name().c_str());
Dustin Graves1e92cd72016-02-09 14:00:18 -0700292 }
293 } else if (value->sType != sType) {
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600294 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, vuid,
295 LayerName, "%s: parameter %s->sType must be %s. %s", apiName, parameterName.get_name().c_str(),
296 sTypeName, validation_error_map[vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700297 }
298
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600299 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700300}
301
302/**
Dustin Graves1e92cd72016-02-09 14:00:18 -0700303 * Validate an array of Vulkan structures
304 *
305 * Verify that required count and array parameters are not 0 or NULL. If
306 * the array contains 1 or more structures, verify that each structure's
307 * sType field is set to the correct VkStructureType value.
308 *
309 * @param report_data debug_report_data object for routing validation messages.
310 * @param apiName Name of API call being validated.
311 * @param countName Name of count parameter.
312 * @param arrayName Name of array parameter.
313 * @param sTypeName Name of expected VkStructureType value.
314 * @param count Number of elements in the array.
315 * @param array Array to validate.
316 * @param sType VkStructureType for structure validation.
317 * @param countRequired The 'count' parameter may not be 0 when true.
318 * @param arrayRequired The 'array' parameter may not be NULL when true.
319 * @return Boolean value indicating that the call should be skipped.
320 */
321template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600322bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
323 const ParameterName &arrayName, const char *sTypeName, uint32_t count, const T *array,
324 VkStructureType sType, bool countRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600325 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700326
327 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600328 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700329 } else {
330 // Verify that all structs in the array have the correct type
331 for (uint32_t i = 0; i < count; ++i) {
332 if (array[i].sType != sType) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600333 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 -0600334 __LINE__, INVALID_STRUCT_STYPE, LayerName, "%s: parameter %s[%d].sType must be %s", apiName,
335 arrayName.get_name().c_str(), i, sTypeName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700336 }
337 }
338 }
339
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600340 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700341}
342
Dustin Graves58d114b2016-03-08 14:42:59 -0700343/**
Mark Young39389872017-01-19 21:10:49 -0700344 * Validate an array of Vulkan structures.
345 *
346 * Verify that required count and array parameters are not NULL. If count
347 * is not NULL and its value is not optional, verify that it is not 0.
348 * If the array contains 1 or more structures, verify that each structure's
349 * sType field is set to the correct VkStructureType value.
350 *
351 * @param report_data debug_report_data object for routing validation messages.
352 * @param apiName Name of API call being validated.
353 * @param countName Name of count parameter.
354 * @param arrayName Name of array parameter.
355 * @param sTypeName Name of expected VkStructureType value.
356 * @param count Pointer to the number of elements in the array.
357 * @param array Array to validate.
358 * @param sType VkStructureType for structure validation.
359 * @param countPtrRequired The 'count' parameter may not be NULL when true.
360 * @param countValueRequired The '*count' value may not be 0 when true.
361 * @param arrayRequired The 'array' parameter may not be NULL when true.
362 * @return Boolean value indicating that the call should be skipped.
363 */
364template <typename T>
365bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
366 const ParameterName &arrayName, const char *sTypeName, uint32_t *count, const T *array,
367 VkStructureType sType, bool countPtrRequired, bool countValueRequired, bool arrayRequired) {
368 bool skip_call = false;
369
370 if (count == NULL) {
371 if (countPtrRequired) {
372 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
373 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
374 countName.get_name().c_str());
375 }
376 } else {
377 skip_call |= validate_struct_type_array(report_data, apiName, countName, arrayName, sTypeName, (*count), array, sType,
378 countValueRequired, arrayRequired);
379 }
380
381 return skip_call;
382}
383
384/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600385* Validate a Vulkan handle.
386*
387* Verify that the specified handle is not VK_NULL_HANDLE.
388*
389* @param report_data debug_report_data object for routing validation messages.
390* @param api_name Name of API call being validated.
391* @param parameter_name Name of struct parameter being validated.
392* @param value Handle to validate.
393* @return Boolean value indicating that the call should be skipped.
394*/
395template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600396bool 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 -0600397 bool skip_call = false;
398
399 if (value == VK_NULL_HANDLE) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600400 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
401 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as VK_NULL_HANDLE", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600402 parameter_name.get_name().c_str());
Dustin Graves20fd66f2016-04-18 18:33:21 -0600403 }
404
405 return skip_call;
406}
407
408/**
409* Validate an array of Vulkan handles.
410*
411* Verify that required count and array parameters are not NULL. If count
412* is not NULL and its value is not optional, verify that it is not 0.
413* If the array contains 1 or more handles, verify that no handle is set to
414* VK_NULL_HANDLE.
415*
416* @note This function is only intended to validate arrays of handles when none
417* of the handles are allowed to be VK_NULL_HANDLE. For arrays of handles
418* that are allowed to contain VK_NULL_HANDLE, use validate_array() instead.
419*
420* @param report_data debug_report_data object for routing validation messages.
421* @param api_name Name of API call being validated.
422* @param count_name Name of count parameter.
423* @param array_name Name of array parameter.
424* @param count Number of elements in the array.
425* @param array Array to validate.
426* @param count_required The 'count' parameter may not be 0 when true.
427* @param array_required The 'array' parameter may not be NULL when true.
428* @return Boolean value indicating that the call should be skipped.
429*/
430template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600431bool validate_handle_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
432 const ParameterName &array_name, uint32_t count, const T *array, bool count_required,
433 bool array_required) {
Dustin Graves20fd66f2016-04-18 18:33:21 -0600434 bool skip_call = false;
435
436 if ((count == 0) || (array == NULL)) {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600437 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600438 } else {
439 // Verify that no handles in the array are VK_NULL_HANDLE
440 for (uint32_t i = 0; i < count; ++i) {
441 if (array[i] == VK_NULL_HANDLE) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600442 skip_call |=
443 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
444 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as VK_NULL_HANDLE", api_name,
445 array_name.get_name().c_str(), i);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600446 }
447 }
448 }
449
450 return skip_call;
451}
452
453/**
Dustin Graves58d114b2016-03-08 14:42:59 -0700454 * Validate string array count and content.
455 *
456 * Verify that required count and array parameters are not 0 or NULL. If the
457 * count parameter is not optional, verify that it is not 0. If the array
458 * parameter is NULL, and it is not optional, verify that count is 0. If the
459 * array parameter is not NULL, verify that none of the strings are NULL.
460 *
461 * @param report_data debug_report_data object for routing validation messages.
462 * @param apiName Name of API call being validated.
463 * @param countName Name of count parameter.
464 * @param arrayName Name of array parameter.
465 * @param count Number of strings in the array.
466 * @param array Array of strings to validate.
467 * @param countRequired The 'count' parameter may not be 0 when true.
468 * @param arrayRequired The 'array' parameter may not be NULL when true.
469 * @return Boolean value indicating that the call should be skipped.
470 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600471static bool validate_string_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
472 const ParameterName &arrayName, uint32_t count, const char *const *array, bool countRequired,
473 bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600474 bool skip_call = false;
Dustin Graves58d114b2016-03-08 14:42:59 -0700475
476 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600477 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
Dustin Graves58d114b2016-03-08 14:42:59 -0700478 } else {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600479 // Verify that strings in the array are not NULL
Dustin Graves58d114b2016-03-08 14:42:59 -0700480 for (uint32_t i = 0; i < count; ++i) {
481 if (array[i] == NULL) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600482 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 -0600483 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as NULL",
484 apiName, arrayName.get_name().c_str(), i);
Dustin Graves58d114b2016-03-08 14:42:59 -0700485 }
486 }
487 }
488
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600489 return skip_call;
Dustin Graves58d114b2016-03-08 14:42:59 -0700490}
491
Dustin Graves58c2f662016-03-08 17:48:20 -0700492/**
493 * Validate a structure's pNext member.
494 *
495 * Verify that the specified pNext value points to the head of a list of
496 * allowed extension structures. If no extension structures are allowed,
497 * verify that pNext is null.
498 *
499 * @param report_data debug_report_data object for routing validation messages.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600500 * @param api_name Name of API call being validated.
501 * @param parameter_name Name of parameter being validated.
502 * @param allowed_struct_names Names of allowed structs.
Dustin Graves58c2f662016-03-08 17:48:20 -0700503 * @param next Pointer to validate.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600504 * @param allowed_type_count Total number of allowed structure types.
505 * @param allowed_types Array of strcuture types allowed for pNext.
506 * @param header_version Version of header defining the pNext validation rules.
Dustin Graves58c2f662016-03-08 17:48:20 -0700507 * @return Boolean value indicating that the call should be skipped.
508 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600509static bool validate_struct_pnext(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600510 const char *allowed_struct_names, const void *next, size_t allowed_type_count,
Mark Lobodzinski3c828522017-06-26 13:05:57 -0600511 const VkStructureType *allowed_types, uint32_t header_version,
512 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600513 bool skip_call = false;
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600514 std::unordered_set<const void *> cycle_check;
515 std::unordered_set<VkStructureType, std::hash<int>> unique_stype_check;
516
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700517 const char disclaimer[] =
518 "This warning is based on the Valid Usage documentation for version %d of the Vulkan header. It "
519 "is possible that you are using a struct from a private extension or an extension that was added "
520 "to a later version of the Vulkan header, in which case your use of %s is perfectly valid but "
521 "is not guaranteed to work correctly with validation enabled";
Dustin Graves58c2f662016-03-08 17:48:20 -0700522
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600523 // TODO: The valid pNext structure types are not recursive. Each structure has its own list of valid sTypes for pNext.
524 // 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 -0700525 if (next != NULL) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600526 if (allowed_type_count == 0) {
527 std::string message = "%s: value of %s must be NULL. ";
528 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600529 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
530 INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name, parameter_name.get_name().c_str(),
531 header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700532 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600533 const VkStructureType *start = allowed_types;
534 const VkStructureType *end = allowed_types + allowed_type_count;
Dustin Graves58c2f662016-03-08 17:48:20 -0700535 const GenericHeader *current = reinterpret_cast<const GenericHeader *>(next);
536
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600537 cycle_check.insert(next);
Dustin Graves58c2f662016-03-08 17:48:20 -0700538
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600539 while (current != NULL) {
540 if (cycle_check.find(current->pNext) != cycle_check.end()) {
541 std::string message = "%s: %s chain contains a cycle -- pNext pointer " PRIx64 " is repeated.";
542 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
543 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
544 parameter_name.get_name().c_str(), reinterpret_cast<uint64_t>(next));
545 break;
546 } else {
547 cycle_check.insert(current->pNext);
548 }
549
550 std::string type_name = string_VkStructureType(current->sType);
551 if (unique_stype_check.find(current->sType) != unique_stype_check.end()) {
552 std::string message = "%s: %s chain contains duplicate structure types: %s appears multiple times.";
553 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
554 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
555 parameter_name.get_name().c_str(), type_name.c_str());
556 } else {
557 unique_stype_check.insert(current->sType);
558 }
559
560 if (std::find(start, end, current->sType) == end) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600561 if (type_name == UnsupportedStructureTypeString) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700562 std::string message =
Mark Lobodzinski184a1dc2017-06-26 11:32:30 -0600563 "%s: %s chain includes a structure with unknown VkStructureType (%d); Allowed structures are [%s]. ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600564 message += disclaimer;
565 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
566 0, __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600567 parameter_name.get_name().c_str(), current->sType, allowed_struct_names,
568 header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700569 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600570 std::string message =
571 "%s: %s chain includes a structure with unexpected VkStructureType %s; Allowed structures are [%s]. ";
572 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600573 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
574 0, __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
575 parameter_name.get_name().c_str(), type_name.c_str(), allowed_struct_names,
576 header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700577 }
578 }
Dustin Graves58c2f662016-03-08 17:48:20 -0700579 current = reinterpret_cast<const GenericHeader *>(current->pNext);
580 }
581 }
582 }
583
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600584 return skip_call;
Dustin Graves58c2f662016-03-08 17:48:20 -0700585}
586
Dustin Graves29148ff2016-03-23 19:44:00 -0600587/**
588* Validate a VkBool32 value.
589*
590* Generate a warning if a VkBool32 value is neither VK_TRUE nor VK_FALSE.
591*
592* @param report_data debug_report_data object for routing validation messages.
593* @param apiName Name of API call being validated.
594* @param parameterName Name of parameter being validated.
595* @param value Boolean value to validate.
596* @return Boolean value indicating that the call should be skipped.
597*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600598static bool validate_bool32(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
599 VkBool32 value) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600600 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600601
602 if ((value != VK_TRUE) && (value != VK_FALSE)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600603 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 -0600604 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s (%d) is neither VK_TRUE nor VK_FALSE", apiName,
605 parameterName.get_name().c_str(), value);
Dustin Graves29148ff2016-03-23 19:44:00 -0600606 }
607
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600608 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600609}
610
611/**
612* Validate a Vulkan enumeration value.
613*
614* Generate a warning if an enumeration token value does not fall within the core enumeration
615* begin and end token values, and was not added to the enumeration by an extension. Extension
616* provided enumerations use the equation specified in Appendix C.10 of the Vulkan specification,
617* with 1,000,000,000 as the base token value.
618*
619* @note This function does not expect to process enumerations defining bitmask flag bits.
620*
621* @param report_data debug_report_data object for routing validation messages.
622* @param apiName Name of API call being validated.
623* @param parameterName Name of parameter being validated.
624* @param enumName Name of the enumeration being validated.
625* @param begin The begin range value for the enumeration.
626* @param end The end range value for the enumeration.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600627* @param value Enumeration value to validate.
Dustin Graves29148ff2016-03-23 19:44:00 -0600628* @return Boolean value indicating that the call should be skipped.
629*/
630template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600631bool validate_ranged_enum(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
632 const char *enumName, T begin, T end, T value) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600633 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600634
635 if (((value < begin) || (value > end)) && !is_extension_added_token(value)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700636 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
637 UNRECOGNIZED_VALUE, LayerName,
638 "%s: value of %s (%d) does not fall within the begin..end range of the core %s "
639 "enumeration tokens and is not an extension added token",
640 apiName, parameterName.get_name().c_str(), value, enumName);
Dustin Graves29148ff2016-03-23 19:44:00 -0600641 }
642
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600643 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600644}
645
646/**
647* Validate an array of Vulkan enumeration value.
648*
649* Process all enumeration token values in the specified array and generate a warning if a value
650* does not fall within the core enumeration begin and end token values, and was not added to
651* the enumeration by an extension. Extension provided enumerations use the equation specified
652* in Appendix C.10 of the Vulkan specification, with 1,000,000,000 as the base token value.
653*
654* @note This function does not expect to process enumerations defining bitmask flag bits.
655*
656* @param report_data debug_report_data object for routing validation messages.
657* @param apiName Name of API call being validated.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600658* @param countName Name of count parameter.
659* @param arrayName Name of array parameter.
Dustin Graves29148ff2016-03-23 19:44:00 -0600660* @param enumName Name of the enumeration being validated.
661* @param begin The begin range value for the enumeration.
662* @param end The end range value for the enumeration.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600663* @param count Number of enumeration values in the array.
664* @param array Array of enumeration values to validate.
665* @param countRequired The 'count' parameter may not be 0 when true.
666* @param arrayRequired The 'array' parameter may not be NULL when true.
Dustin Graves29148ff2016-03-23 19:44:00 -0600667* @return Boolean value indicating that the call should be skipped.
668*/
669template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600670static bool validate_ranged_enum_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
671 const ParameterName &arrayName, const char *enumName, T begin, T end, uint32_t count,
672 const T *array, bool countRequired, bool arrayRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600673 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600674
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600675 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600676 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600677 } else {
678 for (uint32_t i = 0; i < count; ++i) {
679 if (((array[i] < begin) || (array[i] > end)) && !is_extension_added_token(array[i])) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600680 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 -0600681 __LINE__, UNRECOGNIZED_VALUE, LayerName,
682 "%s: value of %s[%d] (%d) does not fall within the begin..end range of the core %s "
683 "enumeration tokens and is not an extension added token",
684 apiName, arrayName.get_name().c_str(), i, array[i], enumName);
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600685 }
Dustin Graves29148ff2016-03-23 19:44:00 -0600686 }
687 }
688
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600689 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600690}
691
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600692/**
Dustin Graves78df8512016-04-28 17:58:59 -0600693* Verify that a reserved VkFlags value is zero.
694*
695* Verify that the specified value is zero, to check VkFlags values that are reserved for
696* future use.
697*
698* @param report_data debug_report_data object for routing validation messages.
699* @param api_name Name of API call being validated.
700* @param parameter_name Name of parameter being validated.
701* @param value Value to validate.
702* @return Boolean value indicating that the call should be skipped.
703*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600704static bool validate_reserved_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Dustin Graves78df8512016-04-28 17:58:59 -0600705 VkFlags value) {
706 bool skip_call = false;
707
708 if (value != 0) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600709 skip_call |=
710 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
711 RESERVED_PARAMETER, LayerName, "%s: parameter %s must be 0", api_name, parameter_name.get_name().c_str());
Dustin Graves78df8512016-04-28 17:58:59 -0600712 }
713
714 return skip_call;
715}
716
717/**
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600718* Validate a Vulkan bitmask value.
719*
720* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
721* for that type.
722*
723* @param report_data debug_report_data object for routing validation messages.
724* @param api_name Name of API call being validated.
725* @param parameter_name Name of parameter being validated.
726* @param flag_bits_name Name of the VkFlags type being validated.
727* @param all_flags A bit mask combining all valid flag bits for the VkFlags type being validated.
728* @param value VkFlags value to validate.
729* @param flags_required The 'value' parameter may not be 0 when true.
Mike Schuchardt47619c82017-05-31 09:14:22 -0600730* @param singleFlag The 'value' parameter may not contain more than one bit from all_flags.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600731* @return Boolean value indicating that the call should be skipped.
732*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600733static bool validate_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Mike Schuchardt47619c82017-05-31 09:14:22 -0600734 const char *flag_bits_name, VkFlags all_flags, VkFlags value, bool flags_required, bool singleFlag) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600735 bool skip_call = false;
736
737 if (value == 0) {
738 if (flags_required) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600739 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 -0600740 REQUIRED_PARAMETER, LayerName, "%s: value of %s must not be 0", api_name,
741 parameter_name.get_name().c_str());
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600742 }
743 } else if ((value & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600744 skip_call |=
745 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
746 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s contains flag bits that are not recognized members of %s",
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600747 api_name, parameter_name.get_name().c_str(), flag_bits_name);
Mike Schuchardt47619c82017-05-31 09:14:22 -0600748 } else if (singleFlag && (std::bitset<sizeof(VkFlags) * 8>(value).count() > 1)) {
749 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
750 UNRECOGNIZED_VALUE, LayerName,
751 "%s: value of %s contains multiple members of %s when only a single value is allowed", api_name,
752 parameter_name.get_name().c_str(), flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600753 }
754
755 return skip_call;
756}
757
758/**
759* Validate an array of Vulkan bitmask values.
760*
761* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
762* for that type.
763*
764* @param report_data debug_report_data object for routing validation messages.
765* @param api_name Name of API call being validated.
766* @param count_name Name of parameter being validated.
767* @param array_name Name of parameter being validated.
768* @param flag_bits_name Name of the VkFlags type being validated.
769* @param all_flags A bitmask combining all valid flag bits for the VkFlags type being validated.
770* @param count Number of VkFlags values in the array.
771* @param array Array of VkFlags value to validate.
772* @param count_required The 'count' parameter may not be 0 when true.
773* @param array_required The 'array' parameter may not be NULL when true.
774* @return Boolean value indicating that the call should be skipped.
775*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600776static bool validate_flags_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
777 const ParameterName &array_name, const char *flag_bits_name, VkFlags all_flags, uint32_t count,
Dustin Graves78df8512016-04-28 17:58:59 -0600778 const VkFlags *array, bool count_required, bool array_required) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600779 bool skip_call = false;
780
781 if ((count == 0) || (array == NULL)) {
782 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required);
783 } else {
784 // Verify that all VkFlags values in the array
785 for (uint32_t i = 0; i < count; ++i) {
Dustin Graves78df8512016-04-28 17:58:59 -0600786 if (array[i] == 0) {
787 // Current XML registry logic for validity generation uses the array parameter's optional tag to determine if
788 // elements in the array are allowed be 0
789 if (array_required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600790 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
791 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: value of %s[%d] must not be 0", api_name,
792 array_name.get_name().c_str(), i);
Dustin Graves78df8512016-04-28 17:58:59 -0600793 }
794 } else if ((array[i] & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600795 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
796 __LINE__, UNRECOGNIZED_VALUE, LayerName,
797 "%s: value of %s[%d] contains flag bits that are not recognized members of %s", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600798 array_name.get_name().c_str(), i, flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600799 }
800 }
801 }
802
803 return skip_call;
804}
805
806/**
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600807* Get VkResult code description.
808*
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600809* Returns a string describing the specified VkResult code. The description is based on the language in the Vulkan API
810* specification.
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600811*
812* @param value VkResult code to process.
813* @return String describing the specified VkResult code.
814*/
815static std::string get_result_description(VkResult result) {
816 // clang-format off
817 switch (result) {
818 case VK_SUCCESS: return "a command completed successfully";
819 case VK_NOT_READY: return "a fence or query has not yet completed";
820 case VK_TIMEOUT: return "a wait operation has not completed in the specified time";
821 case VK_EVENT_SET: return "an event is signaled";
822 case VK_EVENT_RESET: return "an event is unsignalled";
823 case VK_INCOMPLETE: return "a return array was too small for the result";
824 case VK_ERROR_OUT_OF_HOST_MEMORY: return "a host memory allocation has failed";
825 case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "a device memory allocation has failed";
Dustin Graves2adca842016-05-16 18:35:55 -0600826 case VK_ERROR_INITIALIZATION_FAILED: return "initialization of an object has failed";
827 case VK_ERROR_DEVICE_LOST: return "the logical device has been lost";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600828 case VK_ERROR_MEMORY_MAP_FAILED: return "mapping of a memory object has failed";
829 case VK_ERROR_LAYER_NOT_PRESENT: return "the specified layer does not exist";
830 case VK_ERROR_EXTENSION_NOT_PRESENT: return "the specified extension does not exist";
831 case VK_ERROR_FEATURE_NOT_PRESENT: return "the requested feature is not available on this device";
832 case VK_ERROR_INCOMPATIBLE_DRIVER: return "a Vulkan driver could not be found";
833 case VK_ERROR_TOO_MANY_OBJECTS: return "too many objects of the type have already been created";
834 case VK_ERROR_FORMAT_NOT_SUPPORTED: return "the requested format is not supported on this device";
835 case VK_ERROR_SURFACE_LOST_KHR: return "a surface is no longer available";
836 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: return "the requested window is already connected to another "
837 "VkSurfaceKHR object, or some other non-Vulkan surface object";
838 case VK_SUBOPTIMAL_KHR: return "an image became available, and the swapchain no longer "
839 "matches the surface properties exactly, but can still be used to "
840 "present to the surface successfully.";
841 case VK_ERROR_OUT_OF_DATE_KHR: return "a surface has changed in such a way that it is no "
842 "longer compatible with the swapchain";
843 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: return "the display used by a swapchain does not use the same "
844 "presentable image layout, or is incompatible in a way that prevents "
845 "sharing an image";
846 case VK_ERROR_VALIDATION_FAILED_EXT: return "API validation has detected an invalid use of the API";
847 case VK_ERROR_INVALID_SHADER_NV: return "one or more shaders failed to compile or link";
Eric Engestrombcbb0fd2016-04-02 22:06:13 +0100848 default: return "an error has occurred";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600849 };
850 // clang-format on
851}
852
853/**
854* Validate return code.
855*
856* Print a message describing the reason for failure when an error code is returned.
857*
858* @param report_data debug_report_data object for routing validation messages.
859* @param apiName Name of API call being validated.
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600860* @param ignore vector of VkResult return codes to be ignored
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600861* @param value VkResult value to validate.
862*/
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600863static void validate_result(debug_report_data *report_data, const char *apiName, std::vector<VkResult> const &ignore,
864 VkResult result) {
Chris Forbesf1f4e382016-10-13 14:44:03 +1300865 if (result < 0 && result != VK_ERROR_VALIDATION_FAILED_EXT) {
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600866 if (std::find(ignore.begin(), ignore.end(), result) != ignore.end()) {
867 return;
868 }
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600869 std::string resultName = string_VkResult(result);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600870 if (resultName == UnsupportedResultString) {
871 // Unrecognized result code
Dustin Gravesf233e502016-05-05 13:44:21 -0600872 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
873 FAILURE_RETURN_CODE, LayerName, "%s: returned a result code indicating that an error has occurred", apiName);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600874 } else {
875 std::string resultDesc = get_result_description(result);
Dustin Gravesf233e502016-05-05 13:44:21 -0600876 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
877 FAILURE_RETURN_CODE, LayerName, "%s: returned %s, indicating that %s", apiName, resultName.c_str(),
878 resultDesc.c_str());
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600879 }
880 }
881}
882
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700883} // namespace parameter_validation
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600884
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700885#endif // PARAMETER_VALIDATION_UTILS_H