blob: d0b43823b36c2e3a87a856226851da2d64447933 [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>
27
Dustin Graves1e92cd72016-02-09 14:00:18 -070028#include "vulkan/vulkan.h"
Dustin Graves58c2f662016-03-08 17:48:20 -070029#include "vk_enum_string_helper.h"
Dustin Graves1e92cd72016-02-09 14:00:18 -070030#include "vk_layer_logging.h"
31
Dustin Graves58c2f662016-03-08 17:48:20 -070032namespace {
33struct GenericHeader {
34 VkStructureType sType;
35 const void *pNext;
36};
37}
38
Dustin Graves29148ff2016-03-23 19:44:00 -060039// Layer name string to be logged with validation messages.
40const char ParameterValidationName[] = "ParameterValidation";
41
42// String returned by string_VkStructureType for an unrecognized type.
Dustin Graves58c2f662016-03-08 17:48:20 -070043const std::string UnsupportedStructureTypeString = "Unhandled VkStructureType";
44
Dustin Gravesca7aa7c2016-03-25 15:13:28 -060045// String returned by string_VkResult for an unrecognized type.
46const std::string UnsupportedResultString = "Unhandled VkResult";
47
Dustin Graves29148ff2016-03-23 19:44:00 -060048// The base value used when computing the offset for an enumeration token value that is added by an extension.
49// When validating enumeration tokens, any value >= to this value is considered to be provided by an extension.
50// See Appendix C.10 "Assigning Extension Token Values" from the Vulkan specification
51const uint32_t ExtEnumBaseValue = 1000000000;
52
53template <typename T> bool is_extension_added_token(T value) {
54 return (std::abs(static_cast<int32_t>(value)) >= ExtEnumBaseValue);
55}
56
57// VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE token is a special case that was converted from a core token to an
58// extension added token. Its original value was intentionally preserved after the conversion, so it does not use
59// the base value that other extension added tokens use, and it does not fall within the enum's begin/end range.
60template <> bool is_extension_added_token(VkSamplerAddressMode value) {
61 bool result = (std::abs(static_cast<int32_t>(value)) >= ExtEnumBaseValue);
62 return (result || (value == VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE));
63}
64
Dustin Graves1e92cd72016-02-09 14:00:18 -070065/**
66 * Validate a required pointer.
67 *
Dustin Graves58c2f662016-03-08 17:48:20 -070068 * Verify that a required pointer is not NULL.
Dustin Graves1e92cd72016-02-09 14:00:18 -070069 *
70 * @param report_data debug_report_data object for routing validation messages.
71 * @param apiName Name of API call being validated.
72 * @param parameterName Name of parameter being validated.
73 * @param value Pointer to validate.
74 * @return Boolean value indicating that the call should be skipped.
75 */
Dustin Graves080069b2016-04-05 13:48:15 -060076static bool validate_required_pointer(debug_report_data *report_data, const char *apiName, const char *parameterName,
77 const void *value) {
78 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -070079
80 if (value == NULL) {
Dustin Graves29148ff2016-03-23 19:44:00 -060081 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
82 ParameterValidationName, "%s: required parameter %s specified as NULL", apiName, parameterName);
Jon Ashburn5484e0c2016-03-08 17:48:44 -070083 }
Dustin Graves1e92cd72016-02-09 14:00:18 -070084
85 return skipCall;
86}
87
88/**
89 * Validate pointer to array count and pointer to array.
90 *
91 * Verify that required count and array parameters are not NULL. If count
Dustin Graveseec48bf2016-03-02 18:23:29 -070092 * is not NULL and its value is not optional, verify that it is not 0. If the
Dustin Graves58d114b2016-03-08 14:42:59 -070093 * array parameter is NULL, and it is not optional, verify that count is 0.
94 * The array parameter will typically be optional for this case (where count is
95 * a pointer), allowing the caller to retrieve the available count.
Dustin Graves1e92cd72016-02-09 14:00:18 -070096 *
97 * @param report_data debug_report_data object for routing validation messages.
98 * @param apiName Name of API call being validated.
99 * @param countName Name of count parameter.
100 * @param arrayName Name of array parameter.
101 * @param count Pointer to the number of elements in the array.
102 * @param array Array to validate.
103 * @param countPtrRequired The 'count' parameter may not be NULL when true.
104 * @param countValueRequired The '*count' value may not be 0 when true.
105 * @param arrayRequired The 'array' parameter may not be NULL when true.
106 * @return Boolean value indicating that the call should be skipped.
107 */
108template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600109bool validate_array(debug_report_data *report_data, const char *apiName, const char *countName, const char *arrayName,
110 const T *count, const void *array, bool countPtrRequired, bool countValueRequired, bool arrayRequired) {
111 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700112
113 if (count == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600114 if (countPtrRequired) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700115 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Dustin Graves29148ff2016-03-23 19:44:00 -0600116 ParameterValidationName, "%s: required parameter %s specified as NULL", apiName, countName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700117 }
118 } else {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700119 skipCall |= validate_array(report_data, apiName, countName, arrayName, (*count), array, countValueRequired, arrayRequired);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700120 }
121
122 return skipCall;
123}
124
125/**
126 * Validate array count and pointer to array.
127 *
Dustin Graves58d114b2016-03-08 14:42:59 -0700128 * Verify that required count and array parameters are not 0 or NULL. If the
129 * count parameter is not optional, verify that it is not 0. If the array
130 * parameter is NULL, and it is not optional, verify that count is 0.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700131 *
132 * @param report_data debug_report_data object for routing validation messages.
133 * @param apiName Name of API call being validated.
134 * @param countName Name of count parameter.
135 * @param arrayName Name of array parameter.
136 * @param count Number of elements in the array.
137 * @param array Array to validate.
138 * @param countRequired The 'count' parameter may not be 0 when true.
139 * @param arrayRequired The 'array' parameter may not be NULL when true.
140 * @return Boolean value indicating that the call should be skipped.
141 */
142template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600143bool validate_array(debug_report_data *report_data, const char *apiName, const char *countName, const char *arrayName, T count,
144 const void *array, bool countRequired, bool arrayRequired) {
145 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700146
147 // Count parameters not tagged as optional cannot be 0
Dustin Graves080069b2016-04-05 13:48:15 -0600148 if ((count == 0) && countRequired) {
Dustin Graves29148ff2016-03-23 19:44:00 -0600149 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600150 ParameterValidationName, "%s: parameter %s must be greater than 0", apiName, countName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700151 }
152
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600153 // Array parameters not tagged as optional cannot be NULL, unless the count is 0
Dustin Graves080069b2016-04-05 13:48:15 -0600154 if ((array == NULL) && arrayRequired && (count != 0)) {
Dustin Graves29148ff2016-03-23 19:44:00 -0600155 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
156 ParameterValidationName, "%s: required parameter %s specified as NULL", apiName, arrayName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700157 }
158
159 return skipCall;
160}
161
162/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600163 * Validate a pointer to a Vulkan structure.
164 *
165 * Verify that a required pointer to a structure is not NULL. If the pointer is
166 * not NULL, verify that each structure's sType field is set to the correct
167 * VkStructureType value.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700168 *
169 * @param report_data debug_report_data object for routing validation messages.
170 * @param apiName Name of API call being validated.
171 * @param parameterName Name of struct parameter being validated.
172 * @param sTypeName Name of expected VkStructureType value.
173 * @param value Pointer to the struct to validate.
174 * @param sType VkStructureType for structure validation.
175 * @param required The parameter may not be NULL when true.
176 * @return Boolean value indicating that the call should be skipped.
177 */
178template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600179bool validate_struct_type(debug_report_data *report_data, const char *apiName, const char *parameterName, const char *sTypeName,
180 const T *value, VkStructureType sType, bool required) {
181 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700182
183 if (value == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600184 if (required) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700185 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Dustin Graves29148ff2016-03-23 19:44:00 -0600186 ParameterValidationName, "%s: required parameter %s specified as NULL", apiName, parameterName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700187 }
188 } else if (value->sType != sType) {
Dustin Graves29148ff2016-03-23 19:44:00 -0600189 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
190 ParameterValidationName, "%s: parameter %s->sType must be %s", apiName, parameterName, sTypeName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700191 }
192
193 return skipCall;
194}
195
196/**
197 * Validate an array of Vulkan structures.
198 *
199 * Verify that required count and array parameters are not NULL. If count
200 * is not NULL and its value is not optional, verify that it is not 0.
201 * If the array contains 1 or more structures, verify that each structure's
202 * sType field is set to the correct VkStructureType value.
203 *
204 * @param report_data debug_report_data object for routing validation messages.
205 * @param apiName Name of API call being validated.
206 * @param countName Name of count parameter.
207 * @param arrayName Name of array parameter.
208 * @param sTypeName Name of expected VkStructureType value.
209 * @param count Pointer to the number of elements in the array.
210 * @param array Array to validate.
211 * @param sType VkStructureType for structure validation.
212 * @param countPtrRequired The 'count' parameter may not be NULL when true.
213 * @param countValueRequired The '*count' value may not be 0 when true.
214 * @param arrayRequired The 'array' parameter may not be NULL when true.
215 * @return Boolean value indicating that the call should be skipped.
216 */
217template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600218bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const char *countName, const char *arrayName,
219 const char *sTypeName, const uint32_t *count, const T *array, VkStructureType sType,
220 bool countPtrRequired, bool countValueRequired, bool arrayRequired) {
221 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700222
223 if (count == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600224 if (countPtrRequired) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700225 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Dustin Graves29148ff2016-03-23 19:44:00 -0600226 ParameterValidationName, "%s: required parameter %s specified as NULL", apiName, countName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700227 }
228 } else {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700229 skipCall |= validate_struct_type_array(report_data, apiName, countName, arrayName, sTypeName, (*count), array, sType,
230 countValueRequired, arrayRequired);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700231 }
232
233 return skipCall;
234}
235
236/**
237 * Validate an array of Vulkan structures
238 *
239 * Verify that required count and array parameters are not 0 or NULL. If
240 * the array contains 1 or more structures, verify that each structure's
241 * sType field is set to the correct VkStructureType value.
242 *
243 * @param report_data debug_report_data object for routing validation messages.
244 * @param apiName Name of API call being validated.
245 * @param countName Name of count parameter.
246 * @param arrayName Name of array parameter.
247 * @param sTypeName Name of expected VkStructureType value.
248 * @param count Number of elements in the array.
249 * @param array Array to validate.
250 * @param sType VkStructureType for structure validation.
251 * @param countRequired The 'count' parameter may not be 0 when true.
252 * @param arrayRequired The 'array' parameter may not be NULL when true.
253 * @return Boolean value indicating that the call should be skipped.
254 */
255template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600256bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const char *countName, const char *arrayName,
257 const char *sTypeName, uint32_t count, const T *array, VkStructureType sType, bool countRequired,
258 bool arrayRequired) {
259 bool skipCall = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700260
261 if ((count == 0) || (array == NULL)) {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600262 skipCall |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700263 } else {
264 // Verify that all structs in the array have the correct type
265 for (uint32_t i = 0; i < count; ++i) {
266 if (array[i].sType != sType) {
Dustin Graves29148ff2016-03-23 19:44:00 -0600267 skipCall |=
268 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
269 ParameterValidationName, "%s: parameter %s[%d].sType must be %s", apiName, arrayName, i, sTypeName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700270 }
271 }
272 }
273
274 return skipCall;
275}
276
Dustin Graves58d114b2016-03-08 14:42:59 -0700277/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600278* Validate a Vulkan handle.
279*
280* Verify that the specified handle is not VK_NULL_HANDLE.
281*
282* @param report_data debug_report_data object for routing validation messages.
283* @param api_name Name of API call being validated.
284* @param parameter_name Name of struct parameter being validated.
285* @param value Handle to validate.
286* @return Boolean value indicating that the call should be skipped.
287*/
288template <typename T>
289bool validate_required_handle(debug_report_data *report_data, const char *api_name, const char *parameter_name, T value) {
290 bool skip_call = false;
291
292 if (value == VK_NULL_HANDLE) {
293 skip_call |=
294 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
295 ParameterValidationName, "%s: required parameter %s specified as VK_NULL_HANDLE", api_name, parameter_name);
296 }
297
298 return skip_call;
299}
300
301/**
302* Validate an array of Vulkan handles.
303*
304* Verify that required count and array parameters are not NULL. If count
305* is not NULL and its value is not optional, verify that it is not 0.
306* If the array contains 1 or more handles, verify that no handle is set to
307* VK_NULL_HANDLE.
308*
309* @note This function is only intended to validate arrays of handles when none
310* of the handles are allowed to be VK_NULL_HANDLE. For arrays of handles
311* that are allowed to contain VK_NULL_HANDLE, use validate_array() instead.
312*
313* @param report_data debug_report_data object for routing validation messages.
314* @param api_name Name of API call being validated.
315* @param count_name Name of count parameter.
316* @param array_name Name of array parameter.
317* @param count Number of elements in the array.
318* @param array Array to validate.
319* @param count_required The 'count' parameter may not be 0 when true.
320* @param array_required The 'array' parameter may not be NULL when true.
321* @return Boolean value indicating that the call should be skipped.
322*/
323template <typename T>
324bool validate_handle_array(debug_report_data *report_data, const char *api_name, const char *count_name, const char *array_name,
325 uint32_t count, const T *array, bool count_required, bool array_required) {
326 bool skip_call = false;
327
328 if ((count == 0) || (array == NULL)) {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600329 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 -0600330 } else {
331 // Verify that no handles in the array are VK_NULL_HANDLE
332 for (uint32_t i = 0; i < count; ++i) {
333 if (array[i] == VK_NULL_HANDLE) {
334 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
335 ParameterValidationName, "%s: required parameter %s[%d] specified as VK_NULL_HANDLE", api_name,
336 array_name, i);
337 }
338 }
339 }
340
341 return skip_call;
342}
343
344/**
Dustin Graves58d114b2016-03-08 14:42:59 -0700345 * Validate string array count and content.
346 *
347 * Verify that required count and array parameters are not 0 or NULL. If the
348 * count parameter is not optional, verify that it is not 0. If the array
349 * parameter is NULL, and it is not optional, verify that count is 0. If the
350 * array parameter is not NULL, verify that none of the strings are NULL.
351 *
352 * @param report_data debug_report_data object for routing validation messages.
353 * @param apiName Name of API call being validated.
354 * @param countName Name of count parameter.
355 * @param arrayName Name of array parameter.
356 * @param count Number of strings in the array.
357 * @param array Array of strings to validate.
358 * @param countRequired The 'count' parameter may not be 0 when true.
359 * @param arrayRequired The 'array' parameter may not be NULL when true.
360 * @return Boolean value indicating that the call should be skipped.
361 */
Dustin Graves080069b2016-04-05 13:48:15 -0600362static bool validate_string_array(debug_report_data *report_data, const char *apiName, const char *countName, const char *arrayName,
363 uint32_t count, const char *const *array, bool countRequired, bool arrayRequired) {
364 bool skipCall = false;
Dustin Graves58d114b2016-03-08 14:42:59 -0700365
366 if ((count == 0) || (array == NULL)) {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600367 skipCall |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
Dustin Graves58d114b2016-03-08 14:42:59 -0700368 } else {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600369 // Verify that strings in the array are not NULL
Dustin Graves58d114b2016-03-08 14:42:59 -0700370 for (uint32_t i = 0; i < count; ++i) {
371 if (array[i] == NULL) {
Dustin Graves29148ff2016-03-23 19:44:00 -0600372 skipCall |=
373 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
374 ParameterValidationName, "%s: required parameter %s[%d] specified as NULL", apiName, arrayName, i);
Dustin Graves58d114b2016-03-08 14:42:59 -0700375 }
376 }
377 }
378
379 return skipCall;
380}
381
Dustin Graves58c2f662016-03-08 17:48:20 -0700382/**
383 * Validate a structure's pNext member.
384 *
385 * Verify that the specified pNext value points to the head of a list of
386 * allowed extension structures. If no extension structures are allowed,
387 * verify that pNext is null.
388 *
389 * @param report_data debug_report_data object for routing validation messages.
390 * @param apiName Name of API call being validated.
391 * @param parameterName Name of parameter being validated.
392 * @param allowedStructNames Names of allowed structs.
393 * @param next Pointer to validate.
394 * @param allowedTypeCount total number of allowed structure types.
395 * @param allowedTypes array of strcuture types allowed for pNext.
396 * @return Boolean value indicating that the call should be skipped.
397 */
Dustin Graves080069b2016-04-05 13:48:15 -0600398static bool validate_struct_pnext(debug_report_data *report_data, const char *apiName, const char *parameterName,
399 const char *allowedStructNames, const void *next, size_t allowedTypeCount,
400 const VkStructureType *allowedTypes) {
401 bool skipCall = false;
Dustin Graves58c2f662016-03-08 17:48:20 -0700402
403 if (next != NULL) {
404 if (allowedTypeCount == 0) {
405 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Dustin Graves29148ff2016-03-23 19:44:00 -0600406 ParameterValidationName, "%s: value of %s must be NULL", apiName, parameterName);
Dustin Graves58c2f662016-03-08 17:48:20 -0700407 } else {
408 const VkStructureType *start = allowedTypes;
409 const VkStructureType *end = allowedTypes + allowedTypeCount;
410 const GenericHeader *current = reinterpret_cast<const GenericHeader *>(next);
411
412 while (current != NULL) {
413 if (std::find(start, end, current->sType) == end) {
414 std::string typeName = string_VkStructureType(current->sType);
415
416 if (typeName == UnsupportedStructureTypeString) {
417 skipCall |= log_msg(
Dustin Graves29148ff2016-03-23 19:44:00 -0600418 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
419 ParameterValidationName,
Dustin Graves58c2f662016-03-08 17:48:20 -0700420 "%s: %s chain includes a structure with unexpected VkStructureType (%d); Allowed structures are [%s]",
421 apiName, parameterName, current->sType, allowedStructNames);
422 } else {
423 skipCall |= log_msg(
Dustin Graves29148ff2016-03-23 19:44:00 -0600424 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
425 ParameterValidationName,
Dustin Graves58c2f662016-03-08 17:48:20 -0700426 "%s: %s chain includes a structure with unexpected VkStructureType %s; Allowed structures are [%s]",
427 apiName, parameterName, typeName.c_str(), allowedStructNames);
428 }
429 }
430
431 current = reinterpret_cast<const GenericHeader *>(current->pNext);
432 }
433 }
434 }
435
436 return skipCall;
437}
438
Dustin Graves29148ff2016-03-23 19:44:00 -0600439/**
440* Validate a VkBool32 value.
441*
442* Generate a warning if a VkBool32 value is neither VK_TRUE nor VK_FALSE.
443*
444* @param report_data debug_report_data object for routing validation messages.
445* @param apiName Name of API call being validated.
446* @param parameterName Name of parameter being validated.
447* @param value Boolean value to validate.
448* @return Boolean value indicating that the call should be skipped.
449*/
Dustin Graves080069b2016-04-05 13:48:15 -0600450static bool validate_bool32(debug_report_data *report_data, const char *apiName, const char *parameterName, VkBool32 value) {
451 bool skipCall = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600452
453 if ((value != VK_TRUE) && (value != VK_FALSE)) {
454 skipCall |=
455 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
456 ParameterValidationName, "%s: value of %s (%d) is neither VK_TRUE nor VK_FALSE", apiName, parameterName, value);
457 }
458
459 return skipCall;
460}
461
462/**
463* Validate a Vulkan enumeration value.
464*
465* Generate a warning if an enumeration token value does not fall within the core enumeration
466* begin and end token values, and was not added to the enumeration by an extension. Extension
467* provided enumerations use the equation specified in Appendix C.10 of the Vulkan specification,
468* with 1,000,000,000 as the base token value.
469*
470* @note This function does not expect to process enumerations defining bitmask flag bits.
471*
472* @param report_data debug_report_data object for routing validation messages.
473* @param apiName Name of API call being validated.
474* @param parameterName Name of parameter being validated.
475* @param enumName Name of the enumeration being validated.
476* @param begin The begin range value for the enumeration.
477* @param end The end range value for the enumeration.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600478* @param value Enumeration value to validate.
Dustin Graves29148ff2016-03-23 19:44:00 -0600479* @return Boolean value indicating that the call should be skipped.
480*/
481template <typename T>
Dustin Graves080069b2016-04-05 13:48:15 -0600482bool validate_ranged_enum(debug_report_data *report_data, const char *apiName, const char *parameterName, const char *enumName,
483 T begin, T end, T value) {
484 bool skipCall = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600485
486 if (((value < begin) || (value > end)) && !is_extension_added_token(value)) {
487 skipCall |=
488 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
489 ParameterValidationName, "%s: value of %s (%d) does not fall within the begin..end range of the core %s "
490 "enumeration tokens and is not an extension added token",
491 apiName, parameterName, value, enumName);
492 }
493
494 return skipCall;
495}
496
497/**
498* Validate an array of Vulkan enumeration value.
499*
500* Process all enumeration token values in the specified array and generate a warning if a value
501* does not fall within the core enumeration begin and end token values, and was not added to
502* the enumeration by an extension. Extension provided enumerations use the equation specified
503* in Appendix C.10 of the Vulkan specification, with 1,000,000,000 as the base token value.
504*
505* @note This function does not expect to process enumerations defining bitmask flag bits.
506*
507* @param report_data debug_report_data object for routing validation messages.
508* @param apiName Name of API call being validated.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600509* @param countName Name of count parameter.
510* @param arrayName Name of array parameter.
Dustin Graves29148ff2016-03-23 19:44:00 -0600511* @param enumName Name of the enumeration being validated.
512* @param begin The begin range value for the enumeration.
513* @param end The end range value for the enumeration.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600514* @param count Number of enumeration values in the array.
515* @param array Array of enumeration values to validate.
516* @param countRequired The 'count' parameter may not be 0 when true.
517* @param arrayRequired The 'array' parameter may not be NULL when true.
Dustin Graves29148ff2016-03-23 19:44:00 -0600518* @return Boolean value indicating that the call should be skipped.
519*/
520template <typename T>
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600521static bool validate_ranged_enum_array(debug_report_data *report_data, const char *apiName, const char *countName,
522 const char *arrayName, const char *enumName, T begin, T end, uint32_t count, const T *array,
523 bool countRequired, bool arrayRequired) {
Dustin Graves080069b2016-04-05 13:48:15 -0600524 bool skipCall = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600525
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600526 if ((count == 0) || (array == NULL)) {
527 skipCall |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired);
528 } else {
529 for (uint32_t i = 0; i < count; ++i) {
530 if (((array[i] < begin) || (array[i] > end)) && !is_extension_added_token(array[i])) {
531 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
532 ParameterValidationName,
533 "%s: value of %s[%d] (%d) does not fall within the begin..end range of the core %s "
534 "enumeration tokens and is not an extension added token",
535 apiName, arrayName, i, array[i], enumName);
536 }
Dustin Graves29148ff2016-03-23 19:44:00 -0600537 }
538 }
539
540 return skipCall;
541}
542
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600543/**
Dustin Graves78df8512016-04-28 17:58:59 -0600544* Verify that a reserved VkFlags value is zero.
545*
546* Verify that the specified value is zero, to check VkFlags values that are reserved for
547* future use.
548*
549* @param report_data debug_report_data object for routing validation messages.
550* @param api_name Name of API call being validated.
551* @param parameter_name Name of parameter being validated.
552* @param value Value to validate.
553* @return Boolean value indicating that the call should be skipped.
554*/
555static bool validate_reserved_flags(debug_report_data *report_data, const char *api_name, const char *parameter_name,
556 VkFlags value) {
557 bool skip_call = false;
558
559 if (value != 0) {
560 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
561 ParameterValidationName, "%s: parameter %s must be 0", api_name, parameter_name);
562 }
563
564 return skip_call;
565}
566
567/**
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600568* Validate a Vulkan bitmask value.
569*
570* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
571* for that type.
572*
573* @param report_data debug_report_data object for routing validation messages.
574* @param api_name Name of API call being validated.
575* @param parameter_name Name of parameter being validated.
576* @param flag_bits_name Name of the VkFlags type being validated.
577* @param all_flags A bit mask combining all valid flag bits for the VkFlags type being validated.
578* @param value VkFlags value to validate.
579* @param flags_required The 'value' parameter may not be 0 when true.
580* @return Boolean value indicating that the call should be skipped.
581*/
Dustin Graves78df8512016-04-28 17:58:59 -0600582static bool validate_flags(debug_report_data *report_data, const char *api_name, const char *parameter_name,
583 const char *flag_bits_name, VkFlags all_flags, VkFlags value, bool flags_required) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600584 bool skip_call = false;
585
586 if (value == 0) {
587 if (flags_required) {
588 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
589 ParameterValidationName, "%s: value of %s must not be 0", api_name, parameter_name);
590 }
591 } else if ((value & (~all_flags)) != 0) {
592 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
593 ParameterValidationName, "%s: value of %s contains flag bits that are not recognized members of %s",
594 api_name, parameter_name, flag_bits_name);
595 }
596
597 return skip_call;
598}
599
600/**
601* Validate an array of Vulkan bitmask values.
602*
603* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
604* for that type.
605*
606* @param report_data debug_report_data object for routing validation messages.
607* @param api_name Name of API call being validated.
608* @param count_name Name of parameter being validated.
609* @param array_name Name of parameter being validated.
610* @param flag_bits_name Name of the VkFlags type being validated.
611* @param all_flags A bitmask combining all valid flag bits for the VkFlags type being validated.
612* @param count Number of VkFlags values in the array.
613* @param array Array of VkFlags value to validate.
614* @param count_required The 'count' parameter may not be 0 when true.
615* @param array_required The 'array' parameter may not be NULL when true.
616* @return Boolean value indicating that the call should be skipped.
617*/
Dustin Graves78df8512016-04-28 17:58:59 -0600618static bool validate_flags_array(debug_report_data *report_data, const char *api_name, const char *count_name,
619 const char *array_name, const char *flag_bits_name, VkFlags all_flags, uint32_t count,
620 const VkFlags *array, bool count_required, bool array_required) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600621 bool skip_call = false;
622
623 if ((count == 0) || (array == NULL)) {
624 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required);
625 } else {
626 // Verify that all VkFlags values in the array
627 for (uint32_t i = 0; i < count; ++i) {
Dustin Graves78df8512016-04-28 17:58:59 -0600628 if (array[i] == 0) {
629 // Current XML registry logic for validity generation uses the array parameter's optional tag to determine if
630 // elements in the array are allowed be 0
631 if (array_required) {
632 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
633 1, ParameterValidationName, "%s: value of %s[%d] must not be 0", api_name, array_name, i);
634 }
635 } else if ((array[i] & (~all_flags)) != 0) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600636 skip_call |=
637 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
638 ParameterValidationName, "%s: value of %s[%d] contains flag bits that are not recognized members of %s",
639 api_name, array_name, i, flag_bits_name);
640 }
641 }
642 }
643
644 return skip_call;
645}
646
647/**
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600648* Get VkResult code description.
649*
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600650* Returns a string describing the specified VkResult code. The description is based on the language in the Vulkan API
651* specification.
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600652*
653* @param value VkResult code to process.
654* @return String describing the specified VkResult code.
655*/
656static std::string get_result_description(VkResult result) {
657 // clang-format off
658 switch (result) {
659 case VK_SUCCESS: return "a command completed successfully";
660 case VK_NOT_READY: return "a fence or query has not yet completed";
661 case VK_TIMEOUT: return "a wait operation has not completed in the specified time";
662 case VK_EVENT_SET: return "an event is signaled";
663 case VK_EVENT_RESET: return "an event is unsignalled";
664 case VK_INCOMPLETE: return "a return array was too small for the result";
665 case VK_ERROR_OUT_OF_HOST_MEMORY: return "a host memory allocation has failed";
666 case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "a device memory allocation has failed";
667 case VK_ERROR_INITIALIZATION_FAILED: return "the logical device has been lost";
668 case VK_ERROR_DEVICE_LOST: return "initialization of an object has failed";
669 case VK_ERROR_MEMORY_MAP_FAILED: return "mapping of a memory object has failed";
670 case VK_ERROR_LAYER_NOT_PRESENT: return "the specified layer does not exist";
671 case VK_ERROR_EXTENSION_NOT_PRESENT: return "the specified extension does not exist";
672 case VK_ERROR_FEATURE_NOT_PRESENT: return "the requested feature is not available on this device";
673 case VK_ERROR_INCOMPATIBLE_DRIVER: return "a Vulkan driver could not be found";
674 case VK_ERROR_TOO_MANY_OBJECTS: return "too many objects of the type have already been created";
675 case VK_ERROR_FORMAT_NOT_SUPPORTED: return "the requested format is not supported on this device";
676 case VK_ERROR_SURFACE_LOST_KHR: return "a surface is no longer available";
677 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: return "the requested window is already connected to another "
678 "VkSurfaceKHR object, or some other non-Vulkan surface object";
679 case VK_SUBOPTIMAL_KHR: return "an image became available, and the swapchain no longer "
680 "matches the surface properties exactly, but can still be used to "
681 "present to the surface successfully.";
682 case VK_ERROR_OUT_OF_DATE_KHR: return "a surface has changed in such a way that it is no "
683 "longer compatible with the swapchain";
684 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: return "the display used by a swapchain does not use the same "
685 "presentable image layout, or is incompatible in a way that prevents "
686 "sharing an image";
687 case VK_ERROR_VALIDATION_FAILED_EXT: return "API validation has detected an invalid use of the API";
688 case VK_ERROR_INVALID_SHADER_NV: return "one or more shaders failed to compile or link";
Eric Engestrombcbb0fd2016-04-02 22:06:13 +0100689 default: return "an error has occurred";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600690 };
691 // clang-format on
692}
693
694/**
695* Validate return code.
696*
697* Print a message describing the reason for failure when an error code is returned.
698*
699* @param report_data debug_report_data object for routing validation messages.
700* @param apiName Name of API call being validated.
701* @param value VkResult value to validate.
702*/
703static void validate_result(debug_report_data *report_data, const char *apiName, VkResult result) {
704 if (result < 0) {
705 std::string resultName = string_VkResult(result);
706
707 if (resultName == UnsupportedResultString) {
708 // Unrecognized result code
709 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
Eric Engestrombcbb0fd2016-04-02 22:06:13 +0100710 ParameterValidationName, "%s: returned a result code indicating that an error has occurred", apiName);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600711 } else {
712 std::string resultDesc = get_result_description(result);
713 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1,
714 ParameterValidationName, "%s: returned %s, indicating that %s", apiName, resultName.c_str(), resultDesc.c_str());
715 }
716 }
717}
718
Mark Lobodzinski739391a2016-03-17 15:08:18 -0600719#endif // PARAMETER_VALIDATION_UTILS_H