blob: b9d153cefaefe63de40b8f7911d5eded65d4d565 [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>
Petr Kraus595468a2017-09-10 02:26:33 +020028#include <unordered_map>
Mark Lobodzinskid4950072017-08-01 13:02:20 -060029#include <unordered_set>
Petr Kraus595468a2017-09-10 02:26:33 +020030#include <mutex>
Dustin Graves58c2f662016-03-08 17:48:20 -070031
Dustin Graves1e92cd72016-02-09 14:00:18 -070032#include "vulkan/vulkan.h"
Dustin Graves58c2f662016-03-08 17:48:20 -070033#include "vk_enum_string_helper.h"
Dustin Graves1e92cd72016-02-09 14:00:18 -070034#include "vk_layer_logging.h"
Karl Schultza9ef1e52016-10-06 17:53:48 -060035#include "vk_validation_error_messages.h"
Mark Lobodzinski89cdf722017-06-01 15:09:55 -060036#include "vk_extension_helper.h"
Mark Lobodzinski944ec372017-05-30 14:21:21 -060037
Dustin Graves1e92cd72016-02-09 14:00:18 -070038
Dustin Graves8ffbbf62016-07-22 13:19:46 -060039#include "parameter_name.h"
40
Dustin Gravesb83fc2d2016-05-04 12:56:08 -060041namespace parameter_validation {
42
Mark Lobodzinskid4950072017-08-01 13:02:20 -060043extern const uint32_t GeneratedHeaderVersion;
44extern const std::unordered_map<std::string, void*> name_to_funcptr_map;
45
46extern const VkQueryPipelineStatisticFlags AllVkQueryPipelineStatisticFlagBits;
47extern const VkColorComponentFlags AllVkColorComponentFlagBits;
48extern const VkShaderStageFlags AllVkShaderStageFlagBits;
49extern const VkQueryControlFlags AllVkQueryControlFlagBits;
50
51extern const std::vector<VkCompareOp> AllVkCompareOpEnums;
52extern const std::vector<VkStencilOp> AllVkStencilOpEnums;
53extern const std::vector<VkBlendFactor> AllVkBlendFactorEnums;
54extern const std::vector<VkBlendOp> AllVkBlendOpEnums;
55extern const std::vector<VkLogicOp> AllVkLogicOpEnums;
56extern const std::vector<VkBorderColor> AllVkBorderColorEnums;
57extern const std::vector<VkImageLayout> AllVkImageLayoutEnums;
58
Mark Lobodzinski944ec372017-05-30 14:21:21 -060059struct instance_layer_data {
60 VkInstance instance = VK_NULL_HANDLE;
61
62 debug_report_data *report_data = nullptr;
63 std::vector<VkDebugReportCallbackEXT> logging_callback;
64
65 // The following are for keeping track of the temporary callbacks that can
66 // be used in vkCreateInstance and vkDestroyInstance:
67 uint32_t num_tmp_callbacks = 0;
68 VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos = nullptr;
69 VkDebugReportCallbackEXT *tmp_callbacks = nullptr;
70 InstanceExtensions extensions = {};
Mark Lobodzinski944ec372017-05-30 14:21:21 -060071 VkLayerInstanceDispatchTable dispatch_table = {};
72};
73
74struct layer_data {
75 debug_report_data *report_data = nullptr;
76 // Map for queue family index to queue count
77 std::unordered_map<uint32_t, uint32_t> queueFamilyIndexMap;
78 VkPhysicalDeviceLimits device_limits = {};
79 VkPhysicalDeviceFeatures physical_device_features = {};
80 VkPhysicalDevice physical_device = VK_NULL_HANDLE;
81 VkDevice device = VK_NULL_HANDLE;
Mark Lobodzinskib8626db2017-06-01 08:44:53 -060082 DeviceExtensions extensions;
Mark Lobodzinski944ec372017-05-30 14:21:21 -060083
Petr Kraus595468a2017-09-10 02:26:33 +020084 struct SubpassesUsageStates {
85 std::unordered_set<uint32_t> subpasses_using_color_attachment;
86 std::unordered_set<uint32_t> subpasses_using_depthstencil_attachment;
87 };
88
89 std::unordered_map<VkRenderPass, SubpassesUsageStates> renderpasses_states;
90
Mark Lobodzinski944ec372017-05-30 14:21:21 -060091 VkLayerDispatchTable dispatch_table = {};
92};
93
Dustin Gravesf233e502016-05-05 13:44:21 -060094enum ErrorCode {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070095 NONE, // Used for INFO & other non-error messages
96 INVALID_USAGE, // The value of a parameter is not consistent
97 // with the valid usage criteria defined in
98 // the Vulkan specification.
99 INVALID_STRUCT_STYPE, // The sType field of a Vulkan structure does
100 // not contain the value expected for a structure
101 // of that type.
102 INVALID_STRUCT_PNEXT, // The pNext field of a Vulkan structure references
103 // a value that is not compatible with a structure of
104 // that type or is not NULL when a structure of that
105 // type has no compatible pNext values.
106 REQUIRED_PARAMETER, // A required parameter was specified as 0 or NULL.
107 RESERVED_PARAMETER, // A parameter reserved for future use was not
108 // specified as 0 or NULL.
109 UNRECOGNIZED_VALUE, // A Vulkan enumeration, VkFlags, or VkBool32 parameter
110 // contains a value that is not recognized as valid for
111 // that type.
112 DEVICE_LIMIT, // A specified parameter exceeds the limits returned
113 // by the physical device
114 DEVICE_FEATURE, // Use of a requested feature is not supported by
115 // the device
116 FAILURE_RETURN_CODE, // A Vulkan return code indicating a failure condition
117 // was encountered.
118 EXTENSION_NOT_ENABLED, // An extension entrypoint was called, but the required
119 // extension was not enabled at CreateInstance or
120 // CreateDevice time.
Dustin Gravesf233e502016-05-05 13:44:21 -0600121};
122
Dustin Graves58c2f662016-03-08 17:48:20 -0700123struct GenericHeader {
124 VkStructureType sType;
125 const void *pNext;
126};
Dustin Graves58c2f662016-03-08 17:48:20 -0700127
Dustin Graves29148ff2016-03-23 19:44:00 -0600128// Layer name string to be logged with validation messages.
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600129const char LayerName[] = "ParameterValidation";
Dustin Graves29148ff2016-03-23 19:44:00 -0600130
131// String returned by string_VkStructureType for an unrecognized type.
Dustin Graves58c2f662016-03-08 17:48:20 -0700132const std::string UnsupportedStructureTypeString = "Unhandled VkStructureType";
133
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600134// String returned by string_VkResult for an unrecognized type.
135const std::string UnsupportedResultString = "Unhandled VkResult";
136
Dustin Graves29148ff2016-03-23 19:44:00 -0600137// The base value used when computing the offset for an enumeration token value that is added by an extension.
138// When validating enumeration tokens, any value >= to this value is considered to be provided by an extension.
139// See Appendix C.10 "Assigning Extension Token Values" from the Vulkan specification
140const uint32_t ExtEnumBaseValue = 1000000000;
141
Cort Strattonedbe9b82017-05-16 07:38:35 -0700142// The value of all VK_xxx_MAX_ENUM tokens
143const uint32_t MaxEnumValue = 0x7FFFFFFF;
144
Mark Lobodzinski26112592017-05-30 12:02:17 -0600145
Dustin Graves1e92cd72016-02-09 14:00:18 -0700146/**
Dustin Gravesf8032f22016-05-11 18:31:44 -0600147* Validate a minimum value.
148*
149* Verify that the specified value is greater than the specified lower bound.
150*
151* @param report_data debug_report_data object for routing validation messages.
152* @param api_name Name of API call being validated.
153* @param parameter_name Name of parameter being validated.
154* @param value Value to validate.
155* @param lower_bound Lower bound value to use for validation.
156* @return Boolean value indicating that the call should be skipped.
157*/
158template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600159bool ValidateGreaterThan(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name, T value,
160 T lower_bound) {
Dustin Gravesf8032f22016-05-11 18:31:44 -0600161 bool skip_call = false;
162
163 if (value <= lower_bound) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -0600164 skip_call |=
165 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, 1, LayerName,
166 "%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 -0600167 }
168
169 return skip_call;
170}
171
172/**
Dustin Graves1e92cd72016-02-09 14:00:18 -0700173 * Validate a required pointer.
174 *
Dustin Graves58c2f662016-03-08 17:48:20 -0700175 * Verify that a required pointer is not NULL.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700176 *
177 * @param report_data debug_report_data object for routing validation messages.
178 * @param apiName Name of API call being validated.
179 * @param parameterName Name of parameter being validated.
180 * @param value Pointer to validate.
181 * @return Boolean value indicating that the call should be skipped.
182 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600183static bool validate_required_pointer(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinskidead0b62017-06-28 13:22:03 -0600184 const void *value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600185 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700186
187 if (value == NULL) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600188 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskidead0b62017-06-28 13:22:03 -0600189 vuid, LayerName, "%s: required parameter %s specified as NULL. %s", apiName,
190 parameterName.get_name().c_str(), validation_error_map[vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700191 }
192
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600193 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700194}
195
196/**
197 * Validate array count and pointer to array.
198 *
Dustin Graves58d114b2016-03-08 14:42:59 -0700199 * Verify that required count and array parameters are not 0 or NULL. If the
200 * count parameter is not optional, verify that it is not 0. If the array
201 * parameter is NULL, and it is not optional, verify that count is 0.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700202 *
203 * @param report_data debug_report_data object for routing validation messages.
204 * @param apiName Name of API call being validated.
205 * @param countName Name of count parameter.
206 * @param arrayName Name of array parameter.
207 * @param count Number of elements in the array.
208 * @param array Array to validate.
209 * @param countRequired The 'count' parameter may not be 0 when true.
210 * @param arrayRequired The 'array' parameter may not be NULL when true.
211 * @return Boolean value indicating that the call should be skipped.
212 */
213template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600214bool validate_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600215 const ParameterName &arrayName, T count, const void *array, bool countRequired, bool arrayRequired,
216 UNIQUE_VALIDATION_ERROR_CODE count_required_vuid, UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600217 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700218
219 // Count parameters not tagged as optional cannot be 0
Józef Kucia20bb8fb2016-09-23 12:45:04 +0200220 if (countRequired && (count == 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600221 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600222 count_required_vuid, LayerName, "%s: parameter %s must be greater than 0. %s", apiName,
223 countName.get_name().c_str(), validation_error_map[count_required_vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700224 }
225
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600226 // Array parameters not tagged as optional cannot be NULL, unless the count is 0
Dustin Graves080069b2016-04-05 13:48:15 -0600227 if ((array == NULL) && arrayRequired && (count != 0)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600228 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600229 array_required_vuid, LayerName, "%s: required parameter %s specified as NULL. %s", apiName,
230 arrayName.get_name().c_str(), validation_error_map[array_required_vuid]);
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600231 }
232
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600233 return skip_call;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600234}
235
236/**
237* Validate pointer to array count and pointer to array.
238*
239* Verify that required count and array parameters are not NULL. If count
240* is not NULL and its value is not optional, verify that it is not 0. If the
241* array parameter is NULL, and it is not optional, verify that count is 0.
242* The array parameter will typically be optional for this case (where count is
243* a pointer), allowing the caller to retrieve the available count.
244*
245* @param report_data debug_report_data object for routing validation messages.
246* @param apiName Name of API call being validated.
247* @param countName Name of count parameter.
248* @param arrayName Name of array parameter.
249* @param count Pointer to the number of elements in the array.
250* @param array Array to validate.
251* @param countPtrRequired The 'count' parameter may not be NULL when true.
252* @param countValueRequired The '*count' value may not be 0 when true.
253* @param arrayRequired The 'array' parameter may not be NULL when true.
254* @return Boolean value indicating that the call should be skipped.
255*/
256template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600257bool validate_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
258 const ParameterName &arrayName, const T *count, const void *array, bool countPtrRequired,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600259 bool countValueRequired, bool arrayRequired, UNIQUE_VALIDATION_ERROR_CODE count_required_vuid,
260 UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600261 bool skip_call = false;
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600262
263 if (count == NULL) {
264 if (countPtrRequired) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600265 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 -0600266 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
267 countName.get_name().c_str());
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600268 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700269 } else {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600270 skip_call |= validate_array(report_data, apiName, countName, arrayName, array ? (*count) : 0, array, countValueRequired,
271 arrayRequired, count_required_vuid, array_required_vuid);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700272 }
273
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600274 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700275}
276
277/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600278 * Validate a pointer to a Vulkan structure.
279 *
280 * Verify that a required pointer to a structure is not NULL. If the pointer is
281 * not NULL, verify that each structure's sType field is set to the correct
282 * VkStructureType value.
Dustin Graves1e92cd72016-02-09 14:00:18 -0700283 *
284 * @param report_data debug_report_data object for routing validation messages.
285 * @param apiName Name of API call being validated.
286 * @param parameterName Name of struct parameter being validated.
287 * @param sTypeName Name of expected VkStructureType value.
288 * @param value Pointer to the struct to validate.
289 * @param sType VkStructureType for structure validation.
290 * @param required The parameter may not be NULL when true.
291 * @return Boolean value indicating that the call should be skipped.
292 */
293template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600294bool validate_struct_type(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600295 const char *sTypeName, const T *value, VkStructureType sType, bool required,
296 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600297 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700298
299 if (value == NULL) {
Dustin Graves080069b2016-04-05 13:48:15 -0600300 if (required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600301 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
302 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
303 parameterName.get_name().c_str());
Dustin Graves1e92cd72016-02-09 14:00:18 -0700304 }
305 } else if (value->sType != sType) {
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600306 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, vuid,
307 LayerName, "%s: parameter %s->sType must be %s. %s", apiName, parameterName.get_name().c_str(),
308 sTypeName, validation_error_map[vuid]);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700309 }
310
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600311 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700312}
313
314/**
Dustin Graves1e92cd72016-02-09 14:00:18 -0700315 * Validate an array of Vulkan structures
316 *
317 * Verify that required count and array parameters are not 0 or NULL. If
318 * the array contains 1 or more structures, verify that each structure's
319 * sType field is set to the correct VkStructureType value.
320 *
321 * @param report_data debug_report_data object for routing validation messages.
322 * @param apiName Name of API call being validated.
323 * @param countName Name of count parameter.
324 * @param arrayName Name of array parameter.
325 * @param sTypeName Name of expected VkStructureType value.
326 * @param count Number of elements in the array.
327 * @param array Array to validate.
328 * @param sType VkStructureType for structure validation.
329 * @param countRequired The 'count' parameter may not be 0 when true.
330 * @param arrayRequired The 'array' parameter may not be NULL when true.
331 * @return Boolean value indicating that the call should be skipped.
332 */
333template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600334bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
335 const ParameterName &arrayName, const char *sTypeName, uint32_t count, const T *array,
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -0600336 VkStructureType sType, bool countRequired, bool arrayRequired, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600337 bool skip_call = false;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700338
339 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600340 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -0600341 VALIDATION_ERROR_UNDEFINED, vuid);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700342 } else {
343 // Verify that all structs in the array have the correct type
344 for (uint32_t i = 0; i < count; ++i) {
345 if (array[i].sType != sType) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600346 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 -0600347 __LINE__, INVALID_STRUCT_STYPE, LayerName, "%s: parameter %s[%d].sType must be %s", apiName,
348 arrayName.get_name().c_str(), i, sTypeName);
Dustin Graves1e92cd72016-02-09 14:00:18 -0700349 }
350 }
351 }
352
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600353 return skip_call;
Dustin Graves1e92cd72016-02-09 14:00:18 -0700354}
355
Dustin Graves58d114b2016-03-08 14:42:59 -0700356/**
Mark Young39389872017-01-19 21:10:49 -0700357 * Validate an array of Vulkan structures.
358 *
359 * Verify that required count and array parameters are not NULL. If count
360 * is not NULL and its value is not optional, verify that it is not 0.
361 * If the array contains 1 or more structures, verify that each structure's
362 * sType field is set to the correct VkStructureType value.
363 *
364 * @param report_data debug_report_data object for routing validation messages.
365 * @param apiName Name of API call being validated.
366 * @param countName Name of count parameter.
367 * @param arrayName Name of array parameter.
368 * @param sTypeName Name of expected VkStructureType value.
369 * @param count Pointer to the number of elements in the array.
370 * @param array Array to validate.
371 * @param sType VkStructureType for structure validation.
372 * @param countPtrRequired The 'count' parameter may not be NULL when true.
373 * @param countValueRequired The '*count' value may not be 0 when true.
374 * @param arrayRequired The 'array' parameter may not be NULL when true.
375 * @return Boolean value indicating that the call should be skipped.
376 */
377template <typename T>
378bool validate_struct_type_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
379 const ParameterName &arrayName, const char *sTypeName, uint32_t *count, const T *array,
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -0600380 VkStructureType sType, bool countPtrRequired, bool countValueRequired, bool arrayRequired,
381 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Mark Young39389872017-01-19 21:10:49 -0700382 bool skip_call = false;
383
384 if (count == NULL) {
385 if (countPtrRequired) {
386 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
387 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as NULL", apiName,
388 countName.get_name().c_str());
389 }
390 } else {
391 skip_call |= validate_struct_type_array(report_data, apiName, countName, arrayName, sTypeName, (*count), array, sType,
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -0600392 countValueRequired, arrayRequired, vuid);
Mark Young39389872017-01-19 21:10:49 -0700393 }
394
395 return skip_call;
396}
397
398/**
Dustin Graves20fd66f2016-04-18 18:33:21 -0600399* Validate a Vulkan handle.
400*
401* Verify that the specified handle is not VK_NULL_HANDLE.
402*
403* @param report_data debug_report_data object for routing validation messages.
404* @param api_name Name of API call being validated.
405* @param parameter_name Name of struct parameter being validated.
406* @param value Handle to validate.
407* @return Boolean value indicating that the call should be skipped.
408*/
409template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600410bool 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 -0600411 bool skip_call = false;
412
413 if (value == VK_NULL_HANDLE) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600414 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
415 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s specified as VK_NULL_HANDLE", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600416 parameter_name.get_name().c_str());
Dustin Graves20fd66f2016-04-18 18:33:21 -0600417 }
418
419 return skip_call;
420}
421
422/**
423* Validate an array of Vulkan handles.
424*
425* Verify that required count and array parameters are not NULL. If count
426* is not NULL and its value is not optional, verify that it is not 0.
427* If the array contains 1 or more handles, verify that no handle is set to
428* VK_NULL_HANDLE.
429*
430* @note This function is only intended to validate arrays of handles when none
431* of the handles are allowed to be VK_NULL_HANDLE. For arrays of handles
432* that are allowed to contain VK_NULL_HANDLE, use validate_array() instead.
433*
434* @param report_data debug_report_data object for routing validation messages.
435* @param api_name Name of API call being validated.
436* @param count_name Name of count parameter.
437* @param array_name Name of array parameter.
438* @param count Number of elements in the array.
439* @param array Array to validate.
440* @param count_required The 'count' parameter may not be 0 when true.
441* @param array_required The 'array' parameter may not be NULL when true.
442* @return Boolean value indicating that the call should be skipped.
443*/
444template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600445bool validate_handle_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
446 const ParameterName &array_name, uint32_t count, const T *array, bool count_required,
447 bool array_required) {
Dustin Graves20fd66f2016-04-18 18:33:21 -0600448 bool skip_call = false;
449
450 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600451 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required,
452 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600453 } else {
454 // Verify that no handles in the array are VK_NULL_HANDLE
455 for (uint32_t i = 0; i < count; ++i) {
456 if (array[i] == VK_NULL_HANDLE) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600457 skip_call |=
458 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
459 REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as VK_NULL_HANDLE", api_name,
460 array_name.get_name().c_str(), i);
Dustin Graves20fd66f2016-04-18 18:33:21 -0600461 }
462 }
463 }
464
465 return skip_call;
466}
467
468/**
Dustin Graves58d114b2016-03-08 14:42:59 -0700469 * Validate string array count and content.
470 *
471 * Verify that required count and array parameters are not 0 or NULL. If the
472 * count parameter is not optional, verify that it is not 0. If the array
473 * parameter is NULL, and it is not optional, verify that count is 0. If the
474 * array parameter is not NULL, verify that none of the strings are NULL.
475 *
476 * @param report_data debug_report_data object for routing validation messages.
477 * @param apiName Name of API call being validated.
478 * @param countName Name of count parameter.
479 * @param arrayName Name of array parameter.
480 * @param count Number of strings in the array.
481 * @param array Array of strings to validate.
482 * @param countRequired The 'count' parameter may not be 0 when true.
483 * @param arrayRequired The 'array' parameter may not be NULL when true.
484 * @return Boolean value indicating that the call should be skipped.
485 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600486static bool validate_string_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
487 const ParameterName &arrayName, uint32_t count, const char *const *array, bool countRequired,
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600488 bool arrayRequired, UNIQUE_VALIDATION_ERROR_CODE count_required_vuid,
489 UNIQUE_VALIDATION_ERROR_CODE array_required_vuid) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600490 bool skip_call = false;
Dustin Graves58d114b2016-03-08 14:42:59 -0700491
492 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600493 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
494 count_required_vuid, array_required_vuid);
Dustin Graves58d114b2016-03-08 14:42:59 -0700495 } else {
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600496 // Verify that strings in the array are not NULL
Dustin Graves58d114b2016-03-08 14:42:59 -0700497 for (uint32_t i = 0; i < count; ++i) {
498 if (array[i] == NULL) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600499 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 -0600500 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: required parameter %s[%d] specified as NULL",
501 apiName, arrayName.get_name().c_str(), i);
Dustin Graves58d114b2016-03-08 14:42:59 -0700502 }
503 }
504 }
505
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600506 return skip_call;
Dustin Graves58d114b2016-03-08 14:42:59 -0700507}
508
Dustin Graves58c2f662016-03-08 17:48:20 -0700509/**
510 * Validate a structure's pNext member.
511 *
512 * Verify that the specified pNext value points to the head of a list of
513 * allowed extension structures. If no extension structures are allowed,
514 * verify that pNext is null.
515 *
516 * @param report_data debug_report_data object for routing validation messages.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600517 * @param api_name Name of API call being validated.
518 * @param parameter_name Name of parameter being validated.
519 * @param allowed_struct_names Names of allowed structs.
Dustin Graves58c2f662016-03-08 17:48:20 -0700520 * @param next Pointer to validate.
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600521 * @param allowed_type_count Total number of allowed structure types.
522 * @param allowed_types Array of strcuture types allowed for pNext.
523 * @param header_version Version of header defining the pNext validation rules.
Dustin Graves58c2f662016-03-08 17:48:20 -0700524 * @return Boolean value indicating that the call should be skipped.
525 */
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600526static bool validate_struct_pnext(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600527 const char *allowed_struct_names, const void *next, size_t allowed_type_count,
Mark Lobodzinski3c828522017-06-26 13:05:57 -0600528 const VkStructureType *allowed_types, uint32_t header_version,
529 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600530 bool skip_call = false;
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600531 std::unordered_set<const void *> cycle_check;
532 std::unordered_set<VkStructureType, std::hash<int>> unique_stype_check;
533
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700534 const char disclaimer[] =
535 "This warning is based on the Valid Usage documentation for version %d of the Vulkan header. It "
536 "is possible that you are using a struct from a private extension or an extension that was added "
537 "to a later version of the Vulkan header, in which case your use of %s is perfectly valid but "
538 "is not guaranteed to work correctly with validation enabled";
Dustin Graves58c2f662016-03-08 17:48:20 -0700539
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600540 // TODO: The valid pNext structure types are not recursive. Each structure has its own list of valid sTypes for pNext.
541 // 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 -0700542 if (next != NULL) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600543 if (allowed_type_count == 0) {
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600544 std::string message = "%s: value of %s must be NULL. %s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600545 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600546 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600547 vuid, LayerName, message.c_str(), api_name, parameter_name.get_name().c_str(),
548 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700549 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600550 const VkStructureType *start = allowed_types;
551 const VkStructureType *end = allowed_types + allowed_type_count;
Dustin Graves58c2f662016-03-08 17:48:20 -0700552 const GenericHeader *current = reinterpret_cast<const GenericHeader *>(next);
553
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600554 cycle_check.insert(next);
Dustin Graves58c2f662016-03-08 17:48:20 -0700555
Mark Lobodzinskic9b74d32017-03-27 11:52:02 -0600556 while (current != NULL) {
557 if (cycle_check.find(current->pNext) != cycle_check.end()) {
558 std::string message = "%s: %s chain contains a cycle -- pNext pointer " PRIx64 " is repeated.";
559 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
560 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
561 parameter_name.get_name().c_str(), reinterpret_cast<uint64_t>(next));
562 break;
563 } else {
564 cycle_check.insert(current->pNext);
565 }
566
567 std::string type_name = string_VkStructureType(current->sType);
568 if (unique_stype_check.find(current->sType) != unique_stype_check.end()) {
569 std::string message = "%s: %s chain contains duplicate structure types: %s appears multiple times.";
570 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
571 __LINE__, INVALID_STRUCT_PNEXT, LayerName, message.c_str(), api_name,
572 parameter_name.get_name().c_str(), type_name.c_str());
573 } else {
574 unique_stype_check.insert(current->sType);
575 }
576
577 if (std::find(start, end, current->sType) == end) {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600578 if (type_name == UnsupportedStructureTypeString) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700579 std::string message =
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600580 "%s: %s chain includes a structure with unknown VkStructureType (%d); Allowed structures are [%s]. %s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600581 message += disclaimer;
582 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600583 0, __LINE__, vuid, LayerName, message.c_str(), api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600584 parameter_name.get_name().c_str(), current->sType, allowed_struct_names,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600585 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700586 } else {
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600587 std::string message =
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600588 "%s: %s chain includes a structure with unexpected VkStructureType %s; Allowed structures are [%s]. "
589 "%s ";
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600590 message += disclaimer;
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600591 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600592 0, __LINE__, vuid, LayerName, message.c_str(), api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600593 parameter_name.get_name().c_str(), type_name.c_str(), allowed_struct_names,
Mark Lobodzinskia1400e12017-06-26 14:03:16 -0600594 validation_error_map[vuid], header_version, parameter_name.get_name().c_str());
Dustin Graves58c2f662016-03-08 17:48:20 -0700595 }
596 }
Dustin Graves58c2f662016-03-08 17:48:20 -0700597 current = reinterpret_cast<const GenericHeader *>(current->pNext);
598 }
599 }
600 }
601
Dustin Gravesaf5c0292016-07-19 13:43:53 -0600602 return skip_call;
Dustin Graves58c2f662016-03-08 17:48:20 -0700603}
604
Dustin Graves29148ff2016-03-23 19:44:00 -0600605/**
606* Validate a VkBool32 value.
607*
608* Generate a warning if a VkBool32 value is neither VK_TRUE nor VK_FALSE.
609*
610* @param report_data debug_report_data object for routing validation messages.
611* @param apiName Name of API call being validated.
612* @param parameterName Name of parameter being validated.
613* @param value Boolean value to validate.
614* @return Boolean value indicating that the call should be skipped.
615*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600616static bool validate_bool32(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
617 VkBool32 value) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600618 bool skip_call = false;
Dustin Graves29148ff2016-03-23 19:44:00 -0600619
620 if ((value != VK_TRUE) && (value != VK_FALSE)) {
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600621 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 -0600622 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s (%d) is neither VK_TRUE nor VK_FALSE", apiName,
623 parameterName.get_name().c_str(), value);
Dustin Graves29148ff2016-03-23 19:44:00 -0600624 }
625
Mark Lobodzinski72ecd912016-08-11 13:25:38 -0600626 return skip_call;
Dustin Graves29148ff2016-03-23 19:44:00 -0600627}
628
629/**
630* Validate a Vulkan enumeration value.
631*
632* Generate a warning if an enumeration token value does not fall within the core enumeration
633* begin and end token values, and was not added to the enumeration by an extension. Extension
634* provided enumerations use the equation specified in Appendix C.10 of the Vulkan specification,
635* with 1,000,000,000 as the base token value.
636*
637* @note This function does not expect to process enumerations defining bitmask flag bits.
638*
639* @param report_data debug_report_data object for routing validation messages.
640* @param apiName Name of API call being validated.
641* @param parameterName Name of parameter being validated.
642* @param enumName Name of the enumeration being validated.
Mark Lobodzinskiff9db7c2017-07-25 15:32:11 -0600643* @param valid_values The list of valid values for the enumeration.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600644* @param value Enumeration value to validate.
Dustin Graves29148ff2016-03-23 19:44:00 -0600645* @return Boolean value indicating that the call should be skipped.
646*/
647template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600648bool validate_ranged_enum(debug_report_data *report_data, const char *apiName, const ParameterName &parameterName,
Mark Lobodzinskib4d1ab72017-07-25 14:49:06 -0600649 const char *enumName, const std::vector<T> &valid_values, T value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
650 bool skip = false;
651
652 if (std::find(valid_values.begin(), valid_values.end(), value) == valid_values.end()) {
653 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, vuid,
654 LayerName,
655 "%s: value of %s (%d) does not fall within the begin..end range of the core %s "
656 "enumeration tokens and is not an extension added token. %s",
657 apiName, parameterName.get_name().c_str(), value, enumName, validation_error_map[vuid]);
658 }
659
660 return skip;
661}
662
Dustin Graves29148ff2016-03-23 19:44:00 -0600663/**
664* Validate an array of Vulkan enumeration value.
665*
666* Process all enumeration token values in the specified array and generate a warning if a value
667* does not fall within the core enumeration begin and end token values, and was not added to
668* the enumeration by an extension. Extension provided enumerations use the equation specified
669* in Appendix C.10 of the Vulkan specification, with 1,000,000,000 as the base token value.
670*
671* @note This function does not expect to process enumerations defining bitmask flag bits.
672*
673* @param report_data debug_report_data object for routing validation messages.
674* @param apiName Name of API call being validated.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600675* @param countName Name of count parameter.
676* @param arrayName Name of array parameter.
Dustin Graves29148ff2016-03-23 19:44:00 -0600677* @param enumName Name of the enumeration being validated.
Mark Lobodzinskiff9db7c2017-07-25 15:32:11 -0600678* @param valid_values The list of valid values for the enumeration.
Dustin Graves0d15c6e2016-04-26 16:34:10 -0600679* @param count Number of enumeration values in the array.
680* @param array Array of enumeration values to validate.
681* @param countRequired The 'count' parameter may not be 0 when true.
682* @param arrayRequired The 'array' parameter may not be NULL when true.
Dustin Graves29148ff2016-03-23 19:44:00 -0600683* @return Boolean value indicating that the call should be skipped.
684*/
685template <typename T>
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600686static bool validate_ranged_enum_array(debug_report_data *report_data, const char *apiName, const ParameterName &countName,
Mark Lobodzinskibd72bec2017-07-25 15:29:36 -0600687 const ParameterName &arrayName, const char *enumName, const std::vector<T> &valid_values,
688 uint32_t count, const T *array, bool countRequired, bool arrayRequired) {
689 bool skip_call = false;
690
691 if ((count == 0) || (array == NULL)) {
692 skip_call |= validate_array(report_data, apiName, countName, arrayName, count, array, countRequired, arrayRequired,
693 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
694 } else {
695 for (uint32_t i = 0; i < count; ++i) {
696 if (std::find(valid_values.begin(), valid_values.end(), array[i]) == valid_values.end()) {
697 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
698 __LINE__, UNRECOGNIZED_VALUE, LayerName,
699 "%s: value of %s[%d] (%d) does not fall within the begin..end range of the core %s "
700 "enumeration tokens and is not an extension added token",
701 apiName, arrayName.get_name().c_str(), i, array[i], enumName);
702 }
703 }
704 }
705
706 return skip_call;
707}
708
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600709/**
Dustin Graves78df8512016-04-28 17:58:59 -0600710* Verify that a reserved VkFlags value is zero.
711*
712* Verify that the specified value is zero, to check VkFlags values that are reserved for
713* future use.
714*
715* @param report_data debug_report_data object for routing validation messages.
716* @param api_name Name of API call being validated.
717* @param parameter_name Name of parameter being validated.
718* @param value Value to validate.
719* @return Boolean value indicating that the call should be skipped.
720*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600721static bool validate_reserved_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Mark Lobodzinskid0b0c512017-06-28 12:06:41 -0600722 VkFlags value, UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Graves78df8512016-04-28 17:58:59 -0600723 bool skip_call = false;
724
725 if (value != 0) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600726 skip_call |=
727 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskid0b0c512017-06-28 12:06:41 -0600728 vuid, LayerName, "%s: parameter %s must be 0. %s", api_name, parameter_name.get_name().c_str(),
729 validation_error_map[vuid]);
Dustin Graves78df8512016-04-28 17:58:59 -0600730 }
731
732 return skip_call;
733}
734
735/**
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600736* Validate a Vulkan bitmask value.
737*
738* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
739* for that type.
740*
741* @param report_data debug_report_data object for routing validation messages.
742* @param api_name Name of API call being validated.
743* @param parameter_name Name of parameter being validated.
744* @param flag_bits_name Name of the VkFlags type being validated.
745* @param all_flags A bit mask combining all valid flag bits for the VkFlags type being validated.
746* @param value VkFlags value to validate.
747* @param flags_required The 'value' parameter may not be 0 when true.
Mike Schuchardt47619c82017-05-31 09:14:22 -0600748* @param singleFlag The 'value' parameter may not contain more than one bit from all_flags.
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600749* @return Boolean value indicating that the call should be skipped.
750*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600751static bool validate_flags(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name,
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -0600752 const char *flag_bits_name, VkFlags all_flags, VkFlags value, bool flags_required, bool singleFlag,
753 UNIQUE_VALIDATION_ERROR_CODE vuid) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600754 bool skip_call = false;
755
756 if (value == 0) {
757 if (flags_required) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600758 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -0600759 vuid, LayerName, "%s: value of %s must not be 0. %s", api_name,
760 parameter_name.get_name().c_str(), validation_error_map[vuid]);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600761 }
762 } else if ((value & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600763 skip_call |=
764 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
765 UNRECOGNIZED_VALUE, LayerName, "%s: value of %s contains flag bits that are not recognized members of %s",
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600766 api_name, parameter_name.get_name().c_str(), flag_bits_name);
Mike Schuchardt47619c82017-05-31 09:14:22 -0600767 } else if (singleFlag && (std::bitset<sizeof(VkFlags) * 8>(value).count() > 1)) {
768 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
769 UNRECOGNIZED_VALUE, LayerName,
770 "%s: value of %s contains multiple members of %s when only a single value is allowed", api_name,
771 parameter_name.get_name().c_str(), flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600772 }
773
774 return skip_call;
775}
776
777/**
778* Validate an array of Vulkan bitmask values.
779*
780* Generate a warning if a value with a VkFlags derived type does not contain valid flag bits
781* for that type.
782*
783* @param report_data debug_report_data object for routing validation messages.
784* @param api_name Name of API call being validated.
785* @param count_name Name of parameter being validated.
786* @param array_name Name of parameter being validated.
787* @param flag_bits_name Name of the VkFlags type being validated.
788* @param all_flags A bitmask combining all valid flag bits for the VkFlags type being validated.
789* @param count Number of VkFlags values in the array.
790* @param array Array of VkFlags value to validate.
791* @param count_required The 'count' parameter may not be 0 when true.
792* @param array_required The 'array' parameter may not be NULL when true.
793* @return Boolean value indicating that the call should be skipped.
794*/
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600795static bool validate_flags_array(debug_report_data *report_data, const char *api_name, const ParameterName &count_name,
796 const ParameterName &array_name, const char *flag_bits_name, VkFlags all_flags, uint32_t count,
Dustin Graves78df8512016-04-28 17:58:59 -0600797 const VkFlags *array, bool count_required, bool array_required) {
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600798 bool skip_call = false;
799
800 if ((count == 0) || (array == NULL)) {
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600801 skip_call |= validate_array(report_data, api_name, count_name, array_name, count, array, count_required, array_required,
802 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600803 } else {
804 // Verify that all VkFlags values in the array
805 for (uint32_t i = 0; i < count; ++i) {
Dustin Graves78df8512016-04-28 17:58:59 -0600806 if (array[i] == 0) {
807 // Current XML registry logic for validity generation uses the array parameter's optional tag to determine if
808 // elements in the array are allowed be 0
809 if (array_required) {
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600810 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
811 __LINE__, REQUIRED_PARAMETER, LayerName, "%s: value of %s[%d] must not be 0", api_name,
812 array_name.get_name().c_str(), i);
Dustin Graves78df8512016-04-28 17:58:59 -0600813 }
814 } else if ((array[i] & (~all_flags)) != 0) {
Dustin Gravesf233e502016-05-05 13:44:21 -0600815 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
816 __LINE__, UNRECOGNIZED_VALUE, LayerName,
817 "%s: value of %s[%d] contains flag bits that are not recognized members of %s", api_name,
Dustin Graves8ffbbf62016-07-22 13:19:46 -0600818 array_name.get_name().c_str(), i, flag_bits_name);
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600819 }
820 }
821 }
822
823 return skip_call;
824}
825
826/**
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600827* Get VkResult code description.
828*
Dustin Graves9c6b62b2016-04-26 15:37:10 -0600829* Returns a string describing the specified VkResult code. The description is based on the language in the Vulkan API
830* specification.
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600831*
832* @param value VkResult code to process.
833* @return String describing the specified VkResult code.
834*/
835static std::string get_result_description(VkResult result) {
836 // clang-format off
837 switch (result) {
838 case VK_SUCCESS: return "a command completed successfully";
839 case VK_NOT_READY: return "a fence or query has not yet completed";
840 case VK_TIMEOUT: return "a wait operation has not completed in the specified time";
841 case VK_EVENT_SET: return "an event is signaled";
842 case VK_EVENT_RESET: return "an event is unsignalled";
843 case VK_INCOMPLETE: return "a return array was too small for the result";
844 case VK_ERROR_OUT_OF_HOST_MEMORY: return "a host memory allocation has failed";
845 case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "a device memory allocation has failed";
Dustin Graves2adca842016-05-16 18:35:55 -0600846 case VK_ERROR_INITIALIZATION_FAILED: return "initialization of an object has failed";
847 case VK_ERROR_DEVICE_LOST: return "the logical device has been lost";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600848 case VK_ERROR_MEMORY_MAP_FAILED: return "mapping of a memory object has failed";
849 case VK_ERROR_LAYER_NOT_PRESENT: return "the specified layer does not exist";
850 case VK_ERROR_EXTENSION_NOT_PRESENT: return "the specified extension does not exist";
851 case VK_ERROR_FEATURE_NOT_PRESENT: return "the requested feature is not available on this device";
852 case VK_ERROR_INCOMPATIBLE_DRIVER: return "a Vulkan driver could not be found";
853 case VK_ERROR_TOO_MANY_OBJECTS: return "too many objects of the type have already been created";
854 case VK_ERROR_FORMAT_NOT_SUPPORTED: return "the requested format is not supported on this device";
855 case VK_ERROR_SURFACE_LOST_KHR: return "a surface is no longer available";
856 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: return "the requested window is already connected to another "
857 "VkSurfaceKHR object, or some other non-Vulkan surface object";
858 case VK_SUBOPTIMAL_KHR: return "an image became available, and the swapchain no longer "
859 "matches the surface properties exactly, but can still be used to "
860 "present to the surface successfully.";
861 case VK_ERROR_OUT_OF_DATE_KHR: return "a surface has changed in such a way that it is no "
862 "longer compatible with the swapchain";
863 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: return "the display used by a swapchain does not use the same "
864 "presentable image layout, or is incompatible in a way that prevents "
865 "sharing an image";
866 case VK_ERROR_VALIDATION_FAILED_EXT: return "API validation has detected an invalid use of the API";
867 case VK_ERROR_INVALID_SHADER_NV: return "one or more shaders failed to compile or link";
Eric Engestrombcbb0fd2016-04-02 22:06:13 +0100868 default: return "an error has occurred";
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600869 };
870 // clang-format on
871}
872
873/**
874* Validate return code.
875*
876* Print a message describing the reason for failure when an error code is returned.
877*
878* @param report_data debug_report_data object for routing validation messages.
879* @param apiName Name of API call being validated.
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600880* @param ignore vector of VkResult return codes to be ignored
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600881* @param value VkResult value to validate.
882*/
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600883static void validate_result(debug_report_data *report_data, const char *apiName, std::vector<VkResult> const &ignore,
884 VkResult result) {
Chris Forbesf1f4e382016-10-13 14:44:03 +1300885 if (result < 0 && result != VK_ERROR_VALIDATION_FAILED_EXT) {
Mark Lobodzinskib42e51b2017-05-09 13:49:59 -0600886 if (std::find(ignore.begin(), ignore.end(), result) != ignore.end()) {
887 return;
888 }
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600889 std::string resultName = string_VkResult(result);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600890 if (resultName == UnsupportedResultString) {
891 // Unrecognized result code
Dustin Gravesf233e502016-05-05 13:44:21 -0600892 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
893 FAILURE_RETURN_CODE, LayerName, "%s: returned a result code indicating that an error has occurred", apiName);
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600894 } else {
895 std::string resultDesc = get_result_description(result);
Dustin Gravesf233e502016-05-05 13:44:21 -0600896 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
897 FAILURE_RETURN_CODE, LayerName, "%s: returned %s, indicating that %s", apiName, resultName.c_str(),
898 resultDesc.c_str());
Dustin Gravesca7aa7c2016-03-25 15:13:28 -0600899 }
900 }
901}
902
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700903} // namespace parameter_validation
Dustin Gravesb83fc2d2016-05-04 12:56:08 -0600904
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700905#endif // PARAMETER_VALIDATION_UTILS_H