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