1///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015-2016 The Khronos Group Inc.
4// Copyright (c) 2015-2016 Valve Corporation
5// Copyright (c) 2015-2016 LunarG, Inc.
6// Copyright (c) 2015-2016 Google, Inc.
7//
8// Licensed under the Apache License, Version 2.0 (the "License");
9// you may not use this file except in compliance with the License.
10// You may obtain a copy of the License at
11//
12//     http://www.apache.org/licenses/LICENSE-2.0
13//
14// Unless required by applicable law or agreed to in writing, software
15// distributed under the License is distributed on an "AS IS" BASIS,
16// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17// See the License for the specific language governing permissions and
18// limitations under the License.
19///////////////////////////////////////////////////////////////////////////////
20
21#include "vkjson.h"
22
23#include <assert.h>
24#include <string.h>
25#include <stdlib.h>
26
27#include <cmath>
28#include <cinttypes>
29#include <cstdio>
30#include <limits>
31#include <memory>
32#include <sstream>
33#include <type_traits>
34#include <utility>
35
36#include <json/json.h>
37
38namespace {
39
40inline bool IsIntegral(double value) {
41#if defined(ANDROID)
42  // Android NDK doesn't provide std::trunc yet
43  return trunc(value) == value;
44#else
45  return std::trunc(value) == value;
46#endif
47}
48
49template <typename T> struct EnumTraits;
50template <> struct EnumTraits<VkPhysicalDeviceType> {
51  static uint32_t min() { return VK_PHYSICAL_DEVICE_TYPE_BEGIN_RANGE; }
52  static uint32_t max() { return VK_PHYSICAL_DEVICE_TYPE_END_RANGE; }
53  static bool exist(uint32_t e) { return e >= min() && e <= max(); }
54};
55
56template <> struct EnumTraits<VkFormat> {
57  static bool exist(uint32_t e) {
58    switch (e) {
59      case VK_FORMAT_UNDEFINED:
60      case VK_FORMAT_R4G4_UNORM_PACK8:
61      case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
62      case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
63      case VK_FORMAT_R5G6B5_UNORM_PACK16:
64      case VK_FORMAT_B5G6R5_UNORM_PACK16:
65      case VK_FORMAT_R5G5B5A1_UNORM_PACK16:
66      case VK_FORMAT_B5G5R5A1_UNORM_PACK16:
67      case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
68      case VK_FORMAT_R8_UNORM:
69      case VK_FORMAT_R8_SNORM:
70      case VK_FORMAT_R8_USCALED:
71      case VK_FORMAT_R8_SSCALED:
72      case VK_FORMAT_R8_UINT:
73      case VK_FORMAT_R8_SINT:
74      case VK_FORMAT_R8_SRGB:
75      case VK_FORMAT_R8G8_UNORM:
76      case VK_FORMAT_R8G8_SNORM:
77      case VK_FORMAT_R8G8_USCALED:
78      case VK_FORMAT_R8G8_SSCALED:
79      case VK_FORMAT_R8G8_UINT:
80      case VK_FORMAT_R8G8_SINT:
81      case VK_FORMAT_R8G8_SRGB:
82      case VK_FORMAT_R8G8B8_UNORM:
83      case VK_FORMAT_R8G8B8_SNORM:
84      case VK_FORMAT_R8G8B8_USCALED:
85      case VK_FORMAT_R8G8B8_SSCALED:
86      case VK_FORMAT_R8G8B8_UINT:
87      case VK_FORMAT_R8G8B8_SINT:
88      case VK_FORMAT_R8G8B8_SRGB:
89      case VK_FORMAT_B8G8R8_UNORM:
90      case VK_FORMAT_B8G8R8_SNORM:
91      case VK_FORMAT_B8G8R8_USCALED:
92      case VK_FORMAT_B8G8R8_SSCALED:
93      case VK_FORMAT_B8G8R8_UINT:
94      case VK_FORMAT_B8G8R8_SINT:
95      case VK_FORMAT_B8G8R8_SRGB:
96      case VK_FORMAT_R8G8B8A8_UNORM:
97      case VK_FORMAT_R8G8B8A8_SNORM:
98      case VK_FORMAT_R8G8B8A8_USCALED:
99      case VK_FORMAT_R8G8B8A8_SSCALED:
100      case VK_FORMAT_R8G8B8A8_UINT:
101      case VK_FORMAT_R8G8B8A8_SINT:
102      case VK_FORMAT_R8G8B8A8_SRGB:
103      case VK_FORMAT_B8G8R8A8_UNORM:
104      case VK_FORMAT_B8G8R8A8_SNORM:
105      case VK_FORMAT_B8G8R8A8_USCALED:
106      case VK_FORMAT_B8G8R8A8_SSCALED:
107      case VK_FORMAT_B8G8R8A8_UINT:
108      case VK_FORMAT_B8G8R8A8_SINT:
109      case VK_FORMAT_B8G8R8A8_SRGB:
110      case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
111      case VK_FORMAT_A8B8G8R8_SNORM_PACK32:
112      case VK_FORMAT_A8B8G8R8_USCALED_PACK32:
113      case VK_FORMAT_A8B8G8R8_SSCALED_PACK32:
114      case VK_FORMAT_A8B8G8R8_UINT_PACK32:
115      case VK_FORMAT_A8B8G8R8_SINT_PACK32:
116      case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
117      case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
118      case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
119      case VK_FORMAT_A2R10G10B10_USCALED_PACK32:
120      case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
121      case VK_FORMAT_A2R10G10B10_UINT_PACK32:
122      case VK_FORMAT_A2R10G10B10_SINT_PACK32:
123      case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
124      case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
125      case VK_FORMAT_A2B10G10R10_USCALED_PACK32:
126      case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
127      case VK_FORMAT_A2B10G10R10_UINT_PACK32:
128      case VK_FORMAT_A2B10G10R10_SINT_PACK32:
129      case VK_FORMAT_R16_UNORM:
130      case VK_FORMAT_R16_SNORM:
131      case VK_FORMAT_R16_USCALED:
132      case VK_FORMAT_R16_SSCALED:
133      case VK_FORMAT_R16_UINT:
134      case VK_FORMAT_R16_SINT:
135      case VK_FORMAT_R16_SFLOAT:
136      case VK_FORMAT_R16G16_UNORM:
137      case VK_FORMAT_R16G16_SNORM:
138      case VK_FORMAT_R16G16_USCALED:
139      case VK_FORMAT_R16G16_SSCALED:
140      case VK_FORMAT_R16G16_UINT:
141      case VK_FORMAT_R16G16_SINT:
142      case VK_FORMAT_R16G16_SFLOAT:
143      case VK_FORMAT_R16G16B16_UNORM:
144      case VK_FORMAT_R16G16B16_SNORM:
145      case VK_FORMAT_R16G16B16_USCALED:
146      case VK_FORMAT_R16G16B16_SSCALED:
147      case VK_FORMAT_R16G16B16_UINT:
148      case VK_FORMAT_R16G16B16_SINT:
149      case VK_FORMAT_R16G16B16_SFLOAT:
150      case VK_FORMAT_R16G16B16A16_UNORM:
151      case VK_FORMAT_R16G16B16A16_SNORM:
152      case VK_FORMAT_R16G16B16A16_USCALED:
153      case VK_FORMAT_R16G16B16A16_SSCALED:
154      case VK_FORMAT_R16G16B16A16_UINT:
155      case VK_FORMAT_R16G16B16A16_SINT:
156      case VK_FORMAT_R16G16B16A16_SFLOAT:
157      case VK_FORMAT_R32_UINT:
158      case VK_FORMAT_R32_SINT:
159      case VK_FORMAT_R32_SFLOAT:
160      case VK_FORMAT_R32G32_UINT:
161      case VK_FORMAT_R32G32_SINT:
162      case VK_FORMAT_R32G32_SFLOAT:
163      case VK_FORMAT_R32G32B32_UINT:
164      case VK_FORMAT_R32G32B32_SINT:
165      case VK_FORMAT_R32G32B32_SFLOAT:
166      case VK_FORMAT_R32G32B32A32_UINT:
167      case VK_FORMAT_R32G32B32A32_SINT:
168      case VK_FORMAT_R32G32B32A32_SFLOAT:
169      case VK_FORMAT_R64_UINT:
170      case VK_FORMAT_R64_SINT:
171      case VK_FORMAT_R64_SFLOAT:
172      case VK_FORMAT_R64G64_UINT:
173      case VK_FORMAT_R64G64_SINT:
174      case VK_FORMAT_R64G64_SFLOAT:
175      case VK_FORMAT_R64G64B64_UINT:
176      case VK_FORMAT_R64G64B64_SINT:
177      case VK_FORMAT_R64G64B64_SFLOAT:
178      case VK_FORMAT_R64G64B64A64_UINT:
179      case VK_FORMAT_R64G64B64A64_SINT:
180      case VK_FORMAT_R64G64B64A64_SFLOAT:
181      case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
182      case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
183      case VK_FORMAT_D16_UNORM:
184      case VK_FORMAT_X8_D24_UNORM_PACK32:
185      case VK_FORMAT_D32_SFLOAT:
186      case VK_FORMAT_S8_UINT:
187      case VK_FORMAT_D16_UNORM_S8_UINT:
188      case VK_FORMAT_D24_UNORM_S8_UINT:
189      case VK_FORMAT_D32_SFLOAT_S8_UINT:
190      case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
191      case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
192      case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
193      case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
194      case VK_FORMAT_BC2_UNORM_BLOCK:
195      case VK_FORMAT_BC2_SRGB_BLOCK:
196      case VK_FORMAT_BC3_UNORM_BLOCK:
197      case VK_FORMAT_BC3_SRGB_BLOCK:
198      case VK_FORMAT_BC4_UNORM_BLOCK:
199      case VK_FORMAT_BC4_SNORM_BLOCK:
200      case VK_FORMAT_BC5_UNORM_BLOCK:
201      case VK_FORMAT_BC5_SNORM_BLOCK:
202      case VK_FORMAT_BC6H_UFLOAT_BLOCK:
203      case VK_FORMAT_BC6H_SFLOAT_BLOCK:
204      case VK_FORMAT_BC7_UNORM_BLOCK:
205      case VK_FORMAT_BC7_SRGB_BLOCK:
206      case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
207      case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
208      case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
209      case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
210      case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
211      case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
212      case VK_FORMAT_EAC_R11_UNORM_BLOCK:
213      case VK_FORMAT_EAC_R11_SNORM_BLOCK:
214      case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
215      case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
216      case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
217      case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
218      case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
219      case VK_FORMAT_ASTC_5x4_SRGB_BLOCK:
220      case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
221      case VK_FORMAT_ASTC_5x5_SRGB_BLOCK:
222      case VK_FORMAT_ASTC_6x5_UNORM_BLOCK:
223      case VK_FORMAT_ASTC_6x5_SRGB_BLOCK:
224      case VK_FORMAT_ASTC_6x6_UNORM_BLOCK:
225      case VK_FORMAT_ASTC_6x6_SRGB_BLOCK:
226      case VK_FORMAT_ASTC_8x5_UNORM_BLOCK:
227      case VK_FORMAT_ASTC_8x5_SRGB_BLOCK:
228      case VK_FORMAT_ASTC_8x6_UNORM_BLOCK:
229      case VK_FORMAT_ASTC_8x6_SRGB_BLOCK:
230      case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
231      case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
232      case VK_FORMAT_ASTC_10x5_UNORM_BLOCK:
233      case VK_FORMAT_ASTC_10x5_SRGB_BLOCK:
234      case VK_FORMAT_ASTC_10x6_UNORM_BLOCK:
235      case VK_FORMAT_ASTC_10x6_SRGB_BLOCK:
236      case VK_FORMAT_ASTC_10x8_UNORM_BLOCK:
237      case VK_FORMAT_ASTC_10x8_SRGB_BLOCK:
238      case VK_FORMAT_ASTC_10x10_UNORM_BLOCK:
239      case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
240      case VK_FORMAT_ASTC_12x10_UNORM_BLOCK:
241      case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
242      case VK_FORMAT_ASTC_12x12_UNORM_BLOCK:
243      case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
244      case VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG:
245      case VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG:
246      case VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG:
247      case VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG:
248      case VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG:
249      case VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG:
250      case VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG:
251      case VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG:
252      case VK_FORMAT_G8B8G8R8_422_UNORM_KHR:
253      case VK_FORMAT_B8G8R8G8_422_UNORM_KHR:
254      case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR:
255      case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM_KHR:
256      case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM_KHR:
257      case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM_KHR:
258      case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM_KHR:
259      case VK_FORMAT_R10X6_UNORM_PACK16_KHR:
260      case VK_FORMAT_R10X6G10X6_UNORM_2PACK16_KHR:
261      case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16_KHR:
262      case VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16_KHR:
263      case VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16_KHR:
264      case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16_KHR:
265      case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16_KHR:
266      case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16_KHR:
267      case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16_KHR:
268      case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16_KHR:
269      case VK_FORMAT_R12X4_UNORM_PACK16_KHR:
270      case VK_FORMAT_R12X4G12X4_UNORM_2PACK16_KHR:
271      case VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16_KHR:
272      case VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16_KHR:
273      case VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16_KHR:
274      case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16_KHR:
275      case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16_KHR:
276      case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16_KHR:
277      case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16_KHR:
278      case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16_KHR:
279      case VK_FORMAT_G16B16G16R16_422_UNORM_KHR:
280      case VK_FORMAT_B16G16R16G16_422_UNORM_KHR:
281      case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM_KHR:
282      case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM_KHR:
283      case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM_KHR:
284      case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM_KHR:
285      case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM_KHR:
286        return true;
287    }
288    return false;
289  }
290};
291
292template <>
293struct EnumTraits<VkPointClippingBehavior> {
294  static uint32_t min() { return VK_POINT_CLIPPING_BEHAVIOR_BEGIN_RANGE; }
295  static uint32_t max() { return VK_POINT_CLIPPING_BEHAVIOR_END_RANGE; }
296  static bool exist(uint32_t e) { return e >= min() && e <= max(); }
297};
298
299template <>
300struct EnumTraits<VkExternalFenceHandleTypeFlagBits> {
301  static bool exist(uint32_t e) {
302    switch (e) {
303      case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
304      case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_WIN32_BIT:
305      case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT:
306      case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
307        return true;
308    }
309    return false;
310  }
311};
312
313template <>
314struct EnumTraits<VkExternalSemaphoreHandleTypeFlagBits> {
315  static bool exist(uint32_t e) {
316    switch (e) {
317      case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
318      case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT:
319      case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT:
320      case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT:
321      case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
322        return true;
323    }
324    return false;
325  }
326};
327
328// VkSparseImageFormatProperties
329
330template <typename Visitor>
331inline bool Iterate(Visitor* visitor, VkExtent3D* extents) {
332  return
333    visitor->Visit("width", &extents->width) &&
334    visitor->Visit("height", &extents->height) &&
335    visitor->Visit("depth", &extents->depth);
336}
337
338template <typename Visitor>
339inline bool Iterate(Visitor* visitor, VkImageFormatProperties* properties) {
340  return
341    visitor->Visit("maxExtent", &properties->maxExtent) &&
342    visitor->Visit("maxMipLevels", &properties->maxMipLevels) &&
343    visitor->Visit("maxArrayLayers", &properties->maxArrayLayers) &&
344    visitor->Visit("sampleCounts", &properties->sampleCounts) &&
345    visitor->Visit("maxResourceSize", &properties->maxResourceSize);
346}
347
348template <typename Visitor>
349inline bool Iterate(Visitor* visitor, VkPhysicalDeviceLimits* limits) {
350  return
351    visitor->Visit("maxImageDimension1D", &limits->maxImageDimension1D) &&
352    visitor->Visit("maxImageDimension2D", &limits->maxImageDimension2D) &&
353    visitor->Visit("maxImageDimension3D", &limits->maxImageDimension3D) &&
354    visitor->Visit("maxImageDimensionCube", &limits->maxImageDimensionCube) &&
355    visitor->Visit("maxImageArrayLayers", &limits->maxImageArrayLayers) &&
356    visitor->Visit("maxTexelBufferElements", &limits->maxTexelBufferElements) &&
357    visitor->Visit("maxUniformBufferRange", &limits->maxUniformBufferRange) &&
358    visitor->Visit("maxStorageBufferRange", &limits->maxStorageBufferRange) &&
359    visitor->Visit("maxPushConstantsSize", &limits->maxPushConstantsSize) &&
360    visitor->Visit("maxMemoryAllocationCount", &limits->maxMemoryAllocationCount) &&
361    visitor->Visit("maxSamplerAllocationCount", &limits->maxSamplerAllocationCount) &&
362    visitor->Visit("bufferImageGranularity", &limits->bufferImageGranularity) &&
363    visitor->Visit("sparseAddressSpaceSize", &limits->sparseAddressSpaceSize) &&
364    visitor->Visit("maxBoundDescriptorSets", &limits->maxBoundDescriptorSets) &&
365    visitor->Visit("maxPerStageDescriptorSamplers", &limits->maxPerStageDescriptorSamplers) &&
366    visitor->Visit("maxPerStageDescriptorUniformBuffers", &limits->maxPerStageDescriptorUniformBuffers) &&
367    visitor->Visit("maxPerStageDescriptorStorageBuffers", &limits->maxPerStageDescriptorStorageBuffers) &&
368    visitor->Visit("maxPerStageDescriptorSampledImages", &limits->maxPerStageDescriptorSampledImages) &&
369    visitor->Visit("maxPerStageDescriptorStorageImages", &limits->maxPerStageDescriptorStorageImages) &&
370    visitor->Visit("maxPerStageDescriptorInputAttachments", &limits->maxPerStageDescriptorInputAttachments) &&
371    visitor->Visit("maxPerStageResources", &limits->maxPerStageResources) &&
372    visitor->Visit("maxDescriptorSetSamplers", &limits->maxDescriptorSetSamplers) &&
373    visitor->Visit("maxDescriptorSetUniformBuffers", &limits->maxDescriptorSetUniformBuffers) &&
374    visitor->Visit("maxDescriptorSetUniformBuffersDynamic", &limits->maxDescriptorSetUniformBuffersDynamic) &&
375    visitor->Visit("maxDescriptorSetStorageBuffers", &limits->maxDescriptorSetStorageBuffers) &&
376    visitor->Visit("maxDescriptorSetStorageBuffersDynamic", &limits->maxDescriptorSetStorageBuffersDynamic) &&
377    visitor->Visit("maxDescriptorSetSampledImages", &limits->maxDescriptorSetSampledImages) &&
378    visitor->Visit("maxDescriptorSetStorageImages", &limits->maxDescriptorSetStorageImages) &&
379    visitor->Visit("maxDescriptorSetInputAttachments", &limits->maxDescriptorSetInputAttachments) &&
380    visitor->Visit("maxVertexInputAttributes", &limits->maxVertexInputAttributes) &&
381    visitor->Visit("maxVertexInputBindings", &limits->maxVertexInputBindings) &&
382    visitor->Visit("maxVertexInputAttributeOffset", &limits->maxVertexInputAttributeOffset) &&
383    visitor->Visit("maxVertexInputBindingStride", &limits->maxVertexInputBindingStride) &&
384    visitor->Visit("maxVertexOutputComponents", &limits->maxVertexOutputComponents) &&
385    visitor->Visit("maxTessellationGenerationLevel", &limits->maxTessellationGenerationLevel) &&
386    visitor->Visit("maxTessellationPatchSize", &limits->maxTessellationPatchSize) &&
387    visitor->Visit("maxTessellationControlPerVertexInputComponents", &limits->maxTessellationControlPerVertexInputComponents) &&
388    visitor->Visit("maxTessellationControlPerVertexOutputComponents", &limits->maxTessellationControlPerVertexOutputComponents) &&
389    visitor->Visit("maxTessellationControlPerPatchOutputComponents", &limits->maxTessellationControlPerPatchOutputComponents) &&
390    visitor->Visit("maxTessellationControlTotalOutputComponents", &limits->maxTessellationControlTotalOutputComponents) &&
391    visitor->Visit("maxTessellationEvaluationInputComponents", &limits->maxTessellationEvaluationInputComponents) &&
392    visitor->Visit("maxTessellationEvaluationOutputComponents", &limits->maxTessellationEvaluationOutputComponents) &&
393    visitor->Visit("maxGeometryShaderInvocations", &limits->maxGeometryShaderInvocations) &&
394    visitor->Visit("maxGeometryInputComponents", &limits->maxGeometryInputComponents) &&
395    visitor->Visit("maxGeometryOutputComponents", &limits->maxGeometryOutputComponents) &&
396    visitor->Visit("maxGeometryOutputVertices", &limits->maxGeometryOutputVertices) &&
397    visitor->Visit("maxGeometryTotalOutputComponents", &limits->maxGeometryTotalOutputComponents) &&
398    visitor->Visit("maxFragmentInputComponents", &limits->maxFragmentInputComponents) &&
399    visitor->Visit("maxFragmentOutputAttachments", &limits->maxFragmentOutputAttachments) &&
400    visitor->Visit("maxFragmentDualSrcAttachments", &limits->maxFragmentDualSrcAttachments) &&
401    visitor->Visit("maxFragmentCombinedOutputResources", &limits->maxFragmentCombinedOutputResources) &&
402    visitor->Visit("maxComputeSharedMemorySize", &limits->maxComputeSharedMemorySize) &&
403    visitor->Visit("maxComputeWorkGroupCount", &limits->maxComputeWorkGroupCount) &&
404    visitor->Visit("maxComputeWorkGroupInvocations", &limits->maxComputeWorkGroupInvocations) &&
405    visitor->Visit("maxComputeWorkGroupSize", &limits->maxComputeWorkGroupSize) &&
406    visitor->Visit("subPixelPrecisionBits", &limits->subPixelPrecisionBits) &&
407    visitor->Visit("subTexelPrecisionBits", &limits->subTexelPrecisionBits) &&
408    visitor->Visit("mipmapPrecisionBits", &limits->mipmapPrecisionBits) &&
409    visitor->Visit("maxDrawIndexedIndexValue", &limits->maxDrawIndexedIndexValue) &&
410    visitor->Visit("maxDrawIndirectCount", &limits->maxDrawIndirectCount) &&
411    visitor->Visit("maxSamplerLodBias", &limits->maxSamplerLodBias) &&
412    visitor->Visit("maxSamplerAnisotropy", &limits->maxSamplerAnisotropy) &&
413    visitor->Visit("maxViewports", &limits->maxViewports) &&
414    visitor->Visit("maxViewportDimensions", &limits->maxViewportDimensions) &&
415    visitor->Visit("viewportBoundsRange", &limits->viewportBoundsRange) &&
416    visitor->Visit("viewportSubPixelBits", &limits->viewportSubPixelBits) &&
417    visitor->Visit("minMemoryMapAlignment", &limits->minMemoryMapAlignment) &&
418    visitor->Visit("minTexelBufferOffsetAlignment", &limits->minTexelBufferOffsetAlignment) &&
419    visitor->Visit("minUniformBufferOffsetAlignment", &limits->minUniformBufferOffsetAlignment) &&
420    visitor->Visit("minStorageBufferOffsetAlignment", &limits->minStorageBufferOffsetAlignment) &&
421    visitor->Visit("minTexelOffset", &limits->minTexelOffset) &&
422    visitor->Visit("maxTexelOffset", &limits->maxTexelOffset) &&
423    visitor->Visit("minTexelGatherOffset", &limits->minTexelGatherOffset) &&
424    visitor->Visit("maxTexelGatherOffset", &limits->maxTexelGatherOffset) &&
425    visitor->Visit("minInterpolationOffset", &limits->minInterpolationOffset) &&
426    visitor->Visit("maxInterpolationOffset", &limits->maxInterpolationOffset) &&
427    visitor->Visit("subPixelInterpolationOffsetBits", &limits->subPixelInterpolationOffsetBits) &&
428    visitor->Visit("maxFramebufferWidth", &limits->maxFramebufferWidth) &&
429    visitor->Visit("maxFramebufferHeight", &limits->maxFramebufferHeight) &&
430    visitor->Visit("maxFramebufferLayers", &limits->maxFramebufferLayers) &&
431    visitor->Visit("framebufferColorSampleCounts", &limits->framebufferColorSampleCounts) &&
432    visitor->Visit("framebufferDepthSampleCounts", &limits->framebufferDepthSampleCounts) &&
433    visitor->Visit("framebufferStencilSampleCounts", &limits->framebufferStencilSampleCounts) &&
434    visitor->Visit("framebufferNoAttachmentsSampleCounts", &limits->framebufferNoAttachmentsSampleCounts) &&
435    visitor->Visit("maxColorAttachments", &limits->maxColorAttachments) &&
436    visitor->Visit("sampledImageColorSampleCounts", &limits->sampledImageColorSampleCounts) &&
437    visitor->Visit("sampledImageIntegerSampleCounts", &limits->sampledImageIntegerSampleCounts) &&
438    visitor->Visit("sampledImageDepthSampleCounts", &limits->sampledImageDepthSampleCounts) &&
439    visitor->Visit("sampledImageStencilSampleCounts", &limits->sampledImageStencilSampleCounts) &&
440    visitor->Visit("storageImageSampleCounts", &limits->storageImageSampleCounts) &&
441    visitor->Visit("maxSampleMaskWords", &limits->maxSampleMaskWords) &&
442    visitor->Visit("timestampComputeAndGraphics", &limits->timestampComputeAndGraphics) &&
443    visitor->Visit("timestampPeriod", &limits->timestampPeriod) &&
444    visitor->Visit("maxClipDistances", &limits->maxClipDistances) &&
445    visitor->Visit("maxCullDistances", &limits->maxCullDistances) &&
446    visitor->Visit("maxCombinedClipAndCullDistances", &limits->maxCombinedClipAndCullDistances) &&
447    visitor->Visit("discreteQueuePriorities", &limits->discreteQueuePriorities) &&
448    visitor->Visit("pointSizeRange", &limits->pointSizeRange) &&
449    visitor->Visit("lineWidthRange", &limits->lineWidthRange) &&
450    visitor->Visit("pointSizeGranularity", &limits->pointSizeGranularity) &&
451    visitor->Visit("lineWidthGranularity", &limits->lineWidthGranularity) &&
452    visitor->Visit("strictLines", &limits->strictLines) &&
453    visitor->Visit("standardSampleLocations", &limits->standardSampleLocations) &&
454    visitor->Visit("optimalBufferCopyOffsetAlignment", &limits->optimalBufferCopyOffsetAlignment) &&
455    visitor->Visit("optimalBufferCopyRowPitchAlignment", &limits->optimalBufferCopyRowPitchAlignment) &&
456    visitor->Visit("nonCoherentAtomSize", &limits->nonCoherentAtomSize);
457}
458
459template <typename Visitor>
460inline bool Iterate(Visitor* visitor,
461                    VkPhysicalDeviceSparseProperties* properties) {
462  return
463    visitor->Visit("residencyStandard2DBlockShape", &properties->residencyStandard2DBlockShape) &&
464    visitor->Visit("residencyStandard2DMultisampleBlockShape", &properties->residencyStandard2DMultisampleBlockShape) &&
465    visitor->Visit("residencyStandard3DBlockShape", &properties->residencyStandard3DBlockShape) &&
466    visitor->Visit("residencyAlignedMipSize", &properties->residencyAlignedMipSize) &&
467    visitor->Visit("residencyNonResidentStrict", &properties->residencyNonResidentStrict);
468}
469
470template <typename Visitor>
471inline bool Iterate(Visitor* visitor,
472                    VkPhysicalDeviceProperties* properties) {
473  return
474    visitor->Visit("apiVersion", &properties->apiVersion) &&
475    visitor->Visit("driverVersion", &properties->driverVersion) &&
476    visitor->Visit("vendorID", &properties->vendorID) &&
477    visitor->Visit("deviceID", &properties->deviceID) &&
478    visitor->Visit("deviceType", &properties->deviceType) &&
479    visitor->Visit("deviceName", &properties->deviceName) &&
480    visitor->Visit("pipelineCacheUUID", &properties->pipelineCacheUUID) &&
481    visitor->Visit("limits", &properties->limits) &&
482    visitor->Visit("sparseProperties", &properties->sparseProperties);
483}
484
485template <typename Visitor>
486inline bool Iterate(Visitor* visitor, VkPhysicalDeviceFeatures* features) {
487  return
488    visitor->Visit("robustBufferAccess", &features->robustBufferAccess) &&
489    visitor->Visit("fullDrawIndexUint32", &features->fullDrawIndexUint32) &&
490    visitor->Visit("imageCubeArray", &features->imageCubeArray) &&
491    visitor->Visit("independentBlend", &features->independentBlend) &&
492    visitor->Visit("geometryShader", &features->geometryShader) &&
493    visitor->Visit("tessellationShader", &features->tessellationShader) &&
494    visitor->Visit("sampleRateShading", &features->sampleRateShading) &&
495    visitor->Visit("dualSrcBlend", &features->dualSrcBlend) &&
496    visitor->Visit("logicOp", &features->logicOp) &&
497    visitor->Visit("multiDrawIndirect", &features->multiDrawIndirect) &&
498    visitor->Visit("drawIndirectFirstInstance", &features->drawIndirectFirstInstance) &&
499    visitor->Visit("depthClamp", &features->depthClamp) &&
500    visitor->Visit("depthBiasClamp", &features->depthBiasClamp) &&
501    visitor->Visit("fillModeNonSolid", &features->fillModeNonSolid) &&
502    visitor->Visit("depthBounds", &features->depthBounds) &&
503    visitor->Visit("wideLines", &features->wideLines) &&
504    visitor->Visit("largePoints", &features->largePoints) &&
505    visitor->Visit("alphaToOne", &features->alphaToOne) &&
506    visitor->Visit("multiViewport", &features->multiViewport) &&
507    visitor->Visit("samplerAnisotropy", &features->samplerAnisotropy) &&
508    visitor->Visit("textureCompressionETC2", &features->textureCompressionETC2) &&
509    visitor->Visit("textureCompressionASTC_LDR", &features->textureCompressionASTC_LDR) &&
510    visitor->Visit("textureCompressionBC", &features->textureCompressionBC) &&
511    visitor->Visit("occlusionQueryPrecise", &features->occlusionQueryPrecise) &&
512    visitor->Visit("pipelineStatisticsQuery", &features->pipelineStatisticsQuery) &&
513    visitor->Visit("vertexPipelineStoresAndAtomics", &features->vertexPipelineStoresAndAtomics) &&
514    visitor->Visit("fragmentStoresAndAtomics", &features->fragmentStoresAndAtomics) &&
515    visitor->Visit("shaderTessellationAndGeometryPointSize", &features->shaderTessellationAndGeometryPointSize) &&
516    visitor->Visit("shaderImageGatherExtended", &features->shaderImageGatherExtended) &&
517    visitor->Visit("shaderStorageImageExtendedFormats", &features->shaderStorageImageExtendedFormats) &&
518    visitor->Visit("shaderStorageImageMultisample", &features->shaderStorageImageMultisample) &&
519    visitor->Visit("shaderStorageImageReadWithoutFormat", &features->shaderStorageImageReadWithoutFormat) &&
520    visitor->Visit("shaderStorageImageWriteWithoutFormat", &features->shaderStorageImageWriteWithoutFormat) &&
521    visitor->Visit("shaderUniformBufferArrayDynamicIndexing", &features->shaderUniformBufferArrayDynamicIndexing) &&
522    visitor->Visit("shaderSampledImageArrayDynamicIndexing", &features->shaderSampledImageArrayDynamicIndexing) &&
523    visitor->Visit("shaderStorageBufferArrayDynamicIndexing", &features->shaderStorageBufferArrayDynamicIndexing) &&
524    visitor->Visit("shaderStorageImageArrayDynamicIndexing", &features->shaderStorageImageArrayDynamicIndexing) &&
525    visitor->Visit("shaderClipDistance", &features->shaderClipDistance) &&
526    visitor->Visit("shaderCullDistance", &features->shaderCullDistance) &&
527    visitor->Visit("shaderFloat64", &features->shaderFloat64) &&
528    visitor->Visit("shaderInt64", &features->shaderInt64) &&
529    visitor->Visit("shaderInt16", &features->shaderInt16) &&
530    visitor->Visit("shaderResourceResidency", &features->shaderResourceResidency) &&
531    visitor->Visit("shaderResourceMinLod", &features->shaderResourceMinLod) &&
532    visitor->Visit("sparseBinding", &features->sparseBinding) &&
533    visitor->Visit("sparseResidencyBuffer", &features->sparseResidencyBuffer) &&
534    visitor->Visit("sparseResidencyImage2D", &features->sparseResidencyImage2D) &&
535    visitor->Visit("sparseResidencyImage3D", &features->sparseResidencyImage3D) &&
536    visitor->Visit("sparseResidency2Samples", &features->sparseResidency2Samples) &&
537    visitor->Visit("sparseResidency4Samples", &features->sparseResidency4Samples) &&
538    visitor->Visit("sparseResidency8Samples", &features->sparseResidency8Samples) &&
539    visitor->Visit("sparseResidency16Samples", &features->sparseResidency16Samples) &&
540    visitor->Visit("sparseResidencyAliased", &features->sparseResidencyAliased) &&
541    visitor->Visit("variableMultisampleRate", &features->variableMultisampleRate) &&
542    visitor->Visit("inheritedQueries", &features->inheritedQueries);
543}
544
545template <typename Visitor>
546inline bool Iterate(Visitor* visitor,
547                    VkJsonExtVariablePointerFeatures* features) {
548  return visitor->Visit("variablePointerFeaturesKHR",
549                        &features->variable_pointer_features_khr);
550}
551
552template <typename Visitor>
553inline bool Iterate(Visitor* visitor, VkMemoryType* type) {
554  return
555    visitor->Visit("propertyFlags", &type->propertyFlags) &&
556    visitor->Visit("heapIndex", &type->heapIndex);
557}
558
559template <typename Visitor>
560inline bool Iterate(Visitor* visitor, VkMemoryHeap* heap) {
561  return
562    visitor->Visit("size", &heap->size) &&
563    visitor->Visit("flags", &heap->flags);
564}
565
566template <typename Visitor>
567inline bool Iterate(Visitor* visitor, VkPhysicalDeviceMemoryProperties* properties) {
568  return
569    visitor->Visit("memoryTypeCount", &properties->memoryTypeCount) &&
570    visitor->VisitArray("memoryTypes", properties->memoryTypeCount, &properties->memoryTypes) &&
571    visitor->Visit("memoryHeapCount", &properties->memoryHeapCount) &&
572    visitor->VisitArray("memoryHeaps", properties->memoryHeapCount, &properties->memoryHeaps);
573}
574
575template <typename Visitor>
576inline bool Iterate(Visitor* visitor,
577                    VkPhysicalDeviceSubgroupProperties* properties) {
578  return visitor->Visit("subgroupSize", &properties->subgroupSize) &&
579         visitor->Visit("supportedStages", &properties->supportedStages) &&
580         visitor->Visit("supportedOperations",
581                        &properties->supportedOperations) &&
582         visitor->Visit("quadOperationsInAllStages",
583                        &properties->quadOperationsInAllStages);
584}
585
586template <typename Visitor>
587inline bool Iterate(Visitor* visitor,
588                    VkPhysicalDevicePointClippingProperties* properties) {
589  return visitor->Visit("pointClippingBehavior",
590                        &properties->pointClippingBehavior);
591}
592
593template <typename Visitor>
594inline bool Iterate(Visitor* visitor,
595                    VkPhysicalDeviceMultiviewProperties* properties) {
596  return visitor->Visit("maxMultiviewViewCount",
597                        &properties->maxMultiviewViewCount) &&
598         visitor->Visit("maxMultiviewInstanceIndex",
599                        &properties->maxMultiviewInstanceIndex);
600}
601
602template <typename Visitor>
603inline bool Iterate(Visitor* visitor,
604                    VkPhysicalDeviceIDProperties* properties) {
605  return visitor->Visit("deviceUUID", &properties->deviceUUID) &&
606         visitor->Visit("driverUUID", &properties->driverUUID) &&
607         visitor->Visit("deviceLUID", &properties->deviceLUID) &&
608         visitor->Visit("deviceNodeMask", &properties->deviceNodeMask) &&
609         visitor->Visit("deviceLUIDValid", &properties->deviceLUIDValid);
610}
611
612template <typename Visitor>
613inline bool Iterate(Visitor* visitor,
614                    VkPhysicalDeviceMaintenance3Properties* properties) {
615  return visitor->Visit("maxPerSetDescriptors",
616                        &properties->maxPerSetDescriptors) &&
617         visitor->Visit("maxMemoryAllocationSize",
618                        &properties->maxMemoryAllocationSize);
619}
620
621template <typename Visitor>
622inline bool Iterate(Visitor* visitor,
623                    VkPhysicalDevice16BitStorageFeatures* features) {
624  return visitor->Visit("storageBuffer16BitAccess",
625                        &features->storageBuffer16BitAccess) &&
626         visitor->Visit("uniformAndStorageBuffer16BitAccess",
627                        &features->uniformAndStorageBuffer16BitAccess) &&
628         visitor->Visit("storagePushConstant16",
629                        &features->storagePushConstant16) &&
630         visitor->Visit("storageInputOutput16",
631                        &features->storageInputOutput16);
632}
633
634template <typename Visitor>
635inline bool Iterate(Visitor* visitor,
636                    VkPhysicalDeviceMultiviewFeatures* features) {
637  return visitor->Visit("multiview", &features->multiview) &&
638         visitor->Visit("multiviewGeometryShader",
639                        &features->multiviewGeometryShader) &&
640         visitor->Visit("multiviewTessellationShader",
641                        &features->multiviewTessellationShader);
642}
643
644template <typename Visitor>
645inline bool Iterate(Visitor* visitor,
646                    VkPhysicalDeviceVariablePointerFeatures* features) {
647  return visitor->Visit("variablePointersStorageBuffer",
648                        &features->variablePointersStorageBuffer) &&
649         visitor->Visit("variablePointers", &features->variablePointers);
650}
651
652template <typename Visitor>
653inline bool Iterate(Visitor* visitor,
654                    VkPhysicalDeviceProtectedMemoryFeatures* features) {
655  return visitor->Visit("protectedMemory", &features->protectedMemory);
656}
657
658template <typename Visitor>
659inline bool Iterate(Visitor* visitor,
660                    VkPhysicalDeviceSamplerYcbcrConversionFeatures* features) {
661  return visitor->Visit("samplerYcbcrConversion",
662                        &features->samplerYcbcrConversion);
663}
664
665template <typename Visitor>
666inline bool Iterate(Visitor* visitor,
667                    VkPhysicalDeviceShaderDrawParameterFeatures* features) {
668  return visitor->Visit("shaderDrawParameters",
669                        &features->shaderDrawParameters);
670}
671
672template <typename Visitor>
673inline bool Iterate(Visitor* visitor, VkExternalFenceProperties* properties) {
674  return visitor->Visit("exportFromImportedHandleTypes",
675                        &properties->exportFromImportedHandleTypes) &&
676         visitor->Visit("compatibleHandleTypes",
677                        &properties->compatibleHandleTypes) &&
678         visitor->Visit("externalFenceFeatures",
679                        &properties->externalFenceFeatures);
680}
681
682template <typename Visitor>
683inline bool Iterate(Visitor* visitor,
684                    VkExternalSemaphoreProperties* properties) {
685  return visitor->Visit("exportFromImportedHandleTypes",
686                        &properties->exportFromImportedHandleTypes) &&
687         visitor->Visit("compatibleHandleTypes",
688                        &properties->compatibleHandleTypes) &&
689         visitor->Visit("externalSemaphoreFeatures",
690                        &properties->externalSemaphoreFeatures);
691}
692
693template <typename Visitor>
694inline bool Iterate(Visitor* visitor, VkQueueFamilyProperties* properties) {
695  return
696    visitor->Visit("queueFlags", &properties->queueFlags) &&
697    visitor->Visit("queueCount", &properties->queueCount) &&
698    visitor->Visit("timestampValidBits", &properties->timestampValidBits) &&
699    visitor->Visit("minImageTransferGranularity", &properties->minImageTransferGranularity);
700}
701
702template <typename Visitor>
703inline bool Iterate(Visitor* visitor, VkExtensionProperties* properties) {
704  return
705    visitor->Visit("extensionName", &properties->extensionName) &&
706    visitor->Visit("specVersion", &properties->specVersion);
707}
708
709template <typename Visitor>
710inline bool Iterate(Visitor* visitor, VkLayerProperties* properties) {
711  return
712    visitor->Visit("layerName", &properties->layerName) &&
713    visitor->Visit("specVersion", &properties->specVersion) &&
714    visitor->Visit("implementationVersion", &properties->implementationVersion) &&
715    visitor->Visit("description", &properties->description);
716}
717
718template <typename Visitor>
719inline bool Iterate(Visitor* visitor, VkFormatProperties* properties) {
720  return
721    visitor->Visit("linearTilingFeatures", &properties->linearTilingFeatures) &&
722    visitor->Visit("optimalTilingFeatures", &properties->optimalTilingFeatures) &&
723    visitor->Visit("bufferFeatures", &properties->bufferFeatures);
724}
725
726template <typename Visitor>
727inline bool Iterate(Visitor* visitor, VkJsonLayer* layer) {
728  return visitor->Visit("properties", &layer->properties) &&
729         visitor->Visit("extensions", &layer->extensions);
730}
731
732template <typename Visitor>
733inline bool Iterate(Visitor* visitor, VkJsonDeviceGroup* device_group) {
734  return visitor->Visit("devices", &device_group->device_inds) &&
735         visitor->Visit("subsetAllocation",
736                        &device_group->properties.subsetAllocation);
737}
738
739template <typename Visitor>
740inline bool Iterate(Visitor* visitor, VkJsonDevice* device) {
741  bool ret = true;
742  switch (device->properties.apiVersion ^
743          VK_VERSION_PATCH(device->properties.apiVersion)) {
744    case VK_API_VERSION_1_1:
745      ret &=
746          visitor->Visit("subgroupProperties", &device->subgroup_properties) &&
747          visitor->Visit("pointClippingProperties",
748                         &device->point_clipping_properties) &&
749          visitor->Visit("multiviewProperties",
750                         &device->multiview_properties) &&
751          visitor->Visit("idProperties", &device->id_properties) &&
752          visitor->Visit("maintenance3Properties",
753                         &device->maintenance3_properties) &&
754          visitor->Visit("16bitStorageFeatures",
755                         &device->bit16_storage_features) &&
756          visitor->Visit("multiviewFeatures", &device->multiview_features) &&
757          visitor->Visit("variablePointerFeatures",
758                         &device->variable_pointer_features) &&
759          visitor->Visit("protectedMemoryFeatures",
760                         &device->protected_memory_features) &&
761          visitor->Visit("samplerYcbcrConversionFeatures",
762                         &device->sampler_ycbcr_conversion_features) &&
763          visitor->Visit("shaderDrawParameterFeatures",
764                         &device->shader_draw_parameter_features) &&
765          visitor->Visit("externalFenceProperties",
766                         &device->external_fence_properties) &&
767          visitor->Visit("externalSemaphoreProperties",
768                         &device->external_semaphore_properties);
769    case VK_API_VERSION_1_0:
770      ret &= visitor->Visit("properties", &device->properties) &&
771             visitor->Visit("features", &device->features) &&
772             visitor->Visit("VK_KHR_variable_pointers",
773                            &device->ext_variable_pointer_features) &&
774             visitor->Visit("memory", &device->memory) &&
775             visitor->Visit("queues", &device->queues) &&
776             visitor->Visit("extensions", &device->extensions) &&
777             visitor->Visit("layers", &device->layers) &&
778             visitor->Visit("formats", &device->formats);
779  }
780  return ret;
781}
782
783template <typename Visitor>
784inline bool Iterate(Visitor* visitor, VkJsonInstance* instance) {
785  bool ret = true;
786  switch (instance->api_version ^ VK_VERSION_PATCH(instance->api_version)) {
787    case VK_API_VERSION_1_1:
788      ret &= visitor->Visit("deviceGroups", &instance->device_groups);
789    case VK_API_VERSION_1_0:
790      ret &= visitor->Visit("layers", &instance->layers) &&
791             visitor->Visit("extensions", &instance->extensions) &&
792             visitor->Visit("devices", &instance->devices);
793  }
794  return ret;
795}
796
797template <typename T>
798using EnableForArithmetic =
799    typename std::enable_if<std::is_arithmetic<T>::value, void>::type;
800
801template <typename T>
802using EnableForStruct =
803    typename std::enable_if<std::is_class<T>::value, void>::type;
804
805template <typename T>
806using EnableForEnum =
807    typename std::enable_if<std::is_enum<T>::value, void>::type;
808
809template <typename T, typename = EnableForStruct<T>, typename = void>
810Json::Value ToJsonValue(const T& value);
811
812template <typename T, typename = EnableForArithmetic<T>>
813inline Json::Value ToJsonValue(const T& value) {
814  return Json::Value(static_cast<double>(value));
815}
816
817inline Json::Value ToJsonValue(const uint64_t& value) {
818  char string[19] = {0};  // "0x" + 16 digits + terminal \0
819  snprintf(string, sizeof(string), "0x%016" PRIx64, value);
820  return Json::Value(string);
821}
822
823template <typename T, typename = EnableForEnum<T>, typename = void,
824          typename = void>
825inline Json::Value ToJsonValue(const T& value) {
826  return Json::Value(static_cast<double>(value));
827}
828
829template <typename T>
830inline Json::Value ArrayToJsonValue(uint32_t count, const T* values) {
831  Json::Value array(Json::arrayValue);
832  for (unsigned int i = 0; i < count; ++i) array.append(ToJsonValue(values[i]));
833  return array;
834}
835
836template <typename T, unsigned int N>
837inline Json::Value ToJsonValue(const T (&value)[N]) {
838  return ArrayToJsonValue(N, value);
839}
840
841template <size_t N>
842inline Json::Value ToJsonValue(const char (&value)[N]) {
843  assert(strlen(value) < N);
844  return Json::Value(value);
845}
846
847template <typename T>
848inline Json::Value ToJsonValue(const std::vector<T>& value) {
849  assert(value.size() <= std::numeric_limits<uint32_t>::max());
850  return ArrayToJsonValue(static_cast<uint32_t>(value.size()), value.data());
851}
852
853template <typename F, typename S>
854inline Json::Value ToJsonValue(const std::pair<F, S>& value) {
855  Json::Value array(Json::arrayValue);
856  array.append(ToJsonValue(value.first));
857  array.append(ToJsonValue(value.second));
858  return array;
859}
860
861template <typename F, typename S>
862inline Json::Value ToJsonValue(const std::map<F, S>& value) {
863  Json::Value array(Json::arrayValue);
864  for (auto& kv : value) array.append(ToJsonValue(kv));
865  return array;
866}
867
868class JsonWriterVisitor {
869 public:
870  JsonWriterVisitor() : object_(Json::objectValue) {}
871
872  ~JsonWriterVisitor() {}
873
874  template <typename T> bool Visit(const char* key, const T* value) {
875    object_[key] = ToJsonValue(*value);
876    return true;
877  }
878
879  template <typename T, uint32_t N>
880  bool VisitArray(const char* key, uint32_t count, const T (*value)[N]) {
881    assert(count <= N);
882    object_[key] = ArrayToJsonValue(count, *value);
883    return true;
884  }
885
886  Json::Value get_object() const { return object_; }
887
888 private:
889  Json::Value object_;
890};
891
892template <typename Visitor, typename T>
893inline void VisitForWrite(Visitor* visitor, const T& t) {
894  Iterate(visitor, const_cast<T*>(&t));
895}
896
897template <typename T, typename /*= EnableForStruct<T>*/, typename /*= void*/>
898Json::Value ToJsonValue(const T& value) {
899  JsonWriterVisitor visitor;
900  VisitForWrite(&visitor, value);
901  return visitor.get_object();
902}
903
904template <typename T, typename = EnableForStruct<T>>
905bool AsValue(Json::Value* json_value, T* t);
906
907inline bool AsValue(Json::Value* json_value, int32_t* value) {
908  if (json_value->type() != Json::realValue) return false;
909  double d = json_value->asDouble();
910  if (!IsIntegral(d) ||
911      d < static_cast<double>(std::numeric_limits<int32_t>::min()) ||
912      d > static_cast<double>(std::numeric_limits<int32_t>::max()))
913    return false;
914  *value = static_cast<int32_t>(d);
915  return true;
916}
917
918inline bool AsValue(Json::Value* json_value, uint64_t* value) {
919  if (json_value->type() != Json::stringValue) return false;
920  int result =
921      std::sscanf(json_value->asString().c_str(), "0x%016" PRIx64, value);
922  return result == 1;
923}
924
925inline bool AsValue(Json::Value* json_value, uint32_t* value) {
926  if (json_value->type() != Json::realValue) return false;
927  double d = json_value->asDouble();
928  if (!IsIntegral(d) || d < 0.0 ||
929      d > static_cast<double>(std::numeric_limits<uint32_t>::max()))
930    return false;
931  *value = static_cast<uint32_t>(d);
932  return true;
933}
934
935inline bool AsValue(Json::Value* json_value, uint8_t* value) {
936  uint32_t value32 = 0;
937  AsValue(json_value, &value32);
938  if (value32 > std::numeric_limits<uint8_t>::max())
939    return false;
940  *value = static_cast<uint8_t>(value32);
941  return true;
942}
943
944inline bool AsValue(Json::Value* json_value, float* value) {
945  if (json_value->type() != Json::realValue) return false;
946  *value = static_cast<float>(json_value->asDouble());
947  return true;
948}
949
950template <typename T>
951inline bool AsArray(Json::Value* json_value, uint32_t count, T* values) {
952  if (json_value->type() != Json::arrayValue || json_value->size() != count)
953    return false;
954  for (uint32_t i = 0; i < count; ++i) {
955    if (!AsValue(&(*json_value)[i], values + i)) return false;
956  }
957  return true;
958}
959
960template <typename T, unsigned int N>
961inline bool AsValue(Json::Value* json_value, T (*value)[N]) {
962  return AsArray(json_value, N, *value);
963}
964
965template <size_t N>
966inline bool AsValue(Json::Value* json_value, char (*value)[N]) {
967  if (json_value->type() != Json::stringValue) return false;
968  size_t len = json_value->asString().length();
969  if (len >= N)
970    return false;
971  memcpy(*value, json_value->asString().c_str(), len);
972  memset(*value + len, 0, N-len);
973  return true;
974}
975
976template <typename T, typename = EnableForEnum<T>, typename = void>
977inline bool AsValue(Json::Value* json_value, T* t) {
978  uint32_t value = 0;
979  if (!AsValue(json_value, &value))
980      return false;
981  if (!EnumTraits<T>::exist(value)) return false;
982  *t = static_cast<T>(value);
983  return true;
984}
985
986template <typename T>
987inline bool AsValue(Json::Value* json_value, std::vector<T>* value) {
988  if (json_value->type() != Json::arrayValue) return false;
989  int size = json_value->size();
990  value->resize(size);
991  return AsArray(json_value, size, value->data());
992}
993
994template <typename F, typename S>
995inline bool AsValue(Json::Value* json_value, std::pair<F, S>* value) {
996  if (json_value->type() != Json::arrayValue || json_value->size() != 2)
997    return false;
998  return AsValue(&(*json_value)[0], &value->first) &&
999         AsValue(&(*json_value)[1], &value->second);
1000}
1001
1002template <typename F, typename S>
1003inline bool AsValue(Json::Value* json_value, std::map<F, S>* value) {
1004  if (json_value->type() != Json::arrayValue) return false;
1005  int size = json_value->size();
1006  for (int i = 0; i < size; ++i) {
1007    std::pair<F, S> elem;
1008    if (!AsValue(&(*json_value)[i], &elem)) return false;
1009    if (!value->insert(elem).second)
1010      return false;
1011  }
1012  return true;
1013}
1014
1015template <typename T>
1016bool ReadValue(Json::Value* object, const char* key, T* value,
1017               std::string* errors) {
1018  Json::Value json_value = (*object)[key];
1019  if (!json_value) {
1020    if (errors)
1021      *errors = std::string(key) + " missing.";
1022    return false;
1023  }
1024  if (AsValue(&json_value, value)) return true;
1025  if (errors)
1026    *errors = std::string("Wrong type for ") + std::string(key) + ".";
1027  return false;
1028}
1029
1030template <typename Visitor, typename T>
1031inline bool VisitForRead(Visitor* visitor, T* t) {
1032  return Iterate(visitor, t);
1033}
1034
1035class JsonReaderVisitor {
1036 public:
1037  JsonReaderVisitor(Json::Value* object, std::string* errors)
1038      : object_(object), errors_(errors) {}
1039
1040  template <typename T> bool Visit(const char* key, T* value) const {
1041    return ReadValue(object_, key, value, errors_);
1042  }
1043
1044  template <typename T, uint32_t N>
1045  bool VisitArray(const char* key, uint32_t count, T (*value)[N]) {
1046    if (count > N)
1047      return false;
1048    Json::Value json_value = (*object_)[key];
1049    if (!json_value) {
1050      if (errors_)
1051        *errors_ = std::string(key) + " missing.";
1052      return false;
1053    }
1054    if (AsArray(&json_value, count, *value)) return true;
1055    if (errors_)
1056      *errors_ = std::string("Wrong type for ") + std::string(key) + ".";
1057    return false;
1058  }
1059
1060
1061 private:
1062  Json::Value* object_;
1063  std::string* errors_;
1064};
1065
1066template <typename T, typename /*= EnableForStruct<T>*/>
1067bool AsValue(Json::Value* json_value, T* t) {
1068  if (json_value->type() != Json::objectValue) return false;
1069  JsonReaderVisitor visitor(json_value, nullptr);
1070  return VisitForRead(&visitor, t);
1071}
1072
1073
1074template <typename T> std::string VkTypeToJson(const T& t) {
1075  JsonWriterVisitor visitor;
1076  VisitForWrite(&visitor, t);
1077  return visitor.get_object().toStyledString();
1078}
1079
1080template <typename T> bool VkTypeFromJson(const std::string& json,
1081                                          T* t,
1082                                          std::string* errors) {
1083  *t = T();
1084  Json::Value object(Json::objectValue);
1085  Json::Reader reader;
1086  reader.parse(json, object, false);
1087  if (!object) {
1088    if (errors) errors->assign(reader.getFormatedErrorMessages());
1089    return false;
1090  }
1091  return AsValue(&object, t);
1092}
1093
1094}  // anonymous namespace
1095
1096std::string VkJsonInstanceToJson(const VkJsonInstance& instance) {
1097  return VkTypeToJson(instance);
1098}
1099
1100bool VkJsonInstanceFromJson(const std::string& json,
1101                            VkJsonInstance* instance,
1102                            std::string* errors) {
1103  return VkTypeFromJson(json, instance, errors);
1104}
1105
1106std::string VkJsonDeviceToJson(const VkJsonDevice& device) {
1107  return VkTypeToJson(device);
1108}
1109
1110bool VkJsonDeviceFromJson(const std::string& json,
1111                          VkJsonDevice* device,
1112                          std::string* errors) {
1113  return VkTypeFromJson(json, device, errors);
1114};
1115
1116std::string VkJsonImageFormatPropertiesToJson(
1117    const VkImageFormatProperties& properties) {
1118  return VkTypeToJson(properties);
1119}
1120
1121bool VkJsonImageFormatPropertiesFromJson(const std::string& json,
1122                                         VkImageFormatProperties* properties,
1123                                         std::string* errors) {
1124  return VkTypeFromJson(json, properties, errors);
1125};
1126