vulkaninfo.c revision 9652e55fbd98d82c0013eb812892528ea367f035
1/*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
19 * Author: David Pinedo <david@lunarg.com>
20 * Author: Mark Lobodzinski <mark@lunarg.com>
21 * Author: Rene Lindsay <rene@lunarg.com>
22 */
23#include <assert.h>
24#include <inttypes.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#ifdef _WIN32
31#include <fcntl.h>
32#include <io.h>
33#endif // _WIN32
34
35#ifdef __linux__
36#include <X11/Xutil.h>
37#endif
38
39#include <vulkan/vulkan.h>
40
41#define ERR(err)                                                               \
42    printf("%s:%d: failed with %s\n", __FILE__, __LINE__,                      \
43           vk_result_string(err));
44
45#ifdef _WIN32
46
47#define snprintf _snprintf
48
49// Returns nonzero if the console is used only for this process. Will return
50// zero if another process (such as cmd.exe) is also attached.
51static int ConsoleIsExclusive(void) {
52    DWORD pids[2];
53    DWORD num_pids = GetConsoleProcessList(pids, ARRAYSIZE(pids));
54    return num_pids <= 1;
55}
56
57#define WAIT_FOR_CONSOLE_DESTROY                                               \
58    do {                                                                       \
59        if (ConsoleIsExclusive())                                              \
60            Sleep(INFINITE);                                                   \
61    } while (0)
62#else
63#define WAIT_FOR_CONSOLE_DESTROY
64#endif
65
66#define ERR_EXIT(err)                                                          \
67    do {                                                                       \
68        ERR(err);                                                              \
69        fflush(stdout);                                                        \
70        WAIT_FOR_CONSOLE_DESTROY;                                              \
71        exit(-1);                                                              \
72    } while (0)
73
74#if defined(NDEBUG) && defined(__GNUC__)
75#define U_ASSERT_ONLY __attribute__((unused))
76#else
77#define U_ASSERT_ONLY
78#endif
79
80#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
81
82#define MAX_GPUS 8
83
84#define MAX_QUEUE_TYPES 5
85#define APP_SHORT_NAME "vulkaninfo"
86
87struct app_gpu;
88
89struct app_dev {
90    struct app_gpu *gpu; /* point back to the GPU */
91
92    VkDevice obj;
93
94    VkFormatProperties format_props[VK_FORMAT_RANGE_SIZE];
95};
96
97struct layer_extension_list {
98    VkLayerProperties layer_properties;
99    uint32_t extension_count;
100    VkExtensionProperties *extension_properties;
101};
102
103struct app_instance {
104    VkInstance instance;
105    uint32_t global_layer_count;
106    struct layer_extension_list *global_layers;
107    uint32_t global_extension_count;
108    VkExtensionProperties *global_extensions; // Instance Extensions
109
110    PFN_vkGetPhysicalDeviceSurfaceSupportKHR
111        vkGetPhysicalDeviceSurfaceSupportKHR;
112    PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
113        vkGetPhysicalDeviceSurfaceCapabilitiesKHR;
114    PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
115        vkGetPhysicalDeviceSurfaceFormatsKHR;
116    PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
117        vkGetPhysicalDeviceSurfacePresentModesKHR;
118
119    VkSurfaceKHR surface;
120    int width, height;
121
122#ifdef VK_USE_PLATFORM_WIN32_KHR
123    HINSTANCE hInstance; // Windows Instance
124    HWND hWnd;           // window handle
125#endif
126
127#ifdef VK_USE_PLATFORM_XCB_KHR
128    xcb_connection_t *xcb_connection;
129    xcb_screen_t *xcb_screen;
130    xcb_window_t xcb_window;
131#endif
132
133#ifdef VK_USE_PLATFORM_XLIB_KHR
134    Display *xlib_display;
135    Window xlib_window;
136#endif
137
138#ifdef VK_USE_PLATFORM_ANDROID_KHR // TODO
139    ANativeWindow *window;
140#endif
141};
142
143struct app_gpu {
144    uint32_t id;
145    VkPhysicalDevice obj;
146
147    VkPhysicalDeviceProperties props;
148
149    uint32_t queue_count;
150    VkQueueFamilyProperties *queue_props;
151    VkDeviceQueueCreateInfo *queue_reqs;
152
153    VkPhysicalDeviceMemoryProperties memory_props;
154    VkPhysicalDeviceFeatures features;
155    VkPhysicalDevice limits;
156
157    uint32_t device_extension_count;
158    VkExtensionProperties *device_extensions;
159
160    struct app_dev dev;
161};
162
163static VKAPI_ATTR VkBool32 VKAPI_CALL
164dbg_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
165             uint64_t srcObject, size_t location, int32_t msgCode,
166             const char *pLayerPrefix, const char *pMsg, void *pUserData) {
167    char *message = (char *)malloc(strlen(pMsg) + 100);
168
169    assert(message);
170
171    if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
172        sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
173                pMsg);
174    } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
175        sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
176                pMsg);
177    } else if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
178        sprintf(message, "INFO: [%s] Code %d : %s", pLayerPrefix, msgCode,
179                pMsg);
180    } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
181        sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode,
182                pMsg);
183    }
184
185    printf("%s\n", message);
186    fflush(stdout);
187    free(message);
188
189    /*
190     * false indicates that layer should not bail-out of an
191     * API call that had validation failures. This may mean that the
192     * app dies inside the driver due to invalid parameter(s).
193     * That's what would happen without validation layers, so we'll
194     * keep that behavior here.
195     */
196    return false;
197}
198
199static const char *vk_result_string(VkResult err) {
200    switch (err) {
201#define STR(r)                                                                 \
202    case r:                                                                    \
203        return #r
204        STR(VK_SUCCESS);
205        STR(VK_NOT_READY);
206        STR(VK_TIMEOUT);
207        STR(VK_EVENT_SET);
208        STR(VK_EVENT_RESET);
209        STR(VK_ERROR_INITIALIZATION_FAILED);
210        STR(VK_ERROR_OUT_OF_HOST_MEMORY);
211        STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
212        STR(VK_ERROR_DEVICE_LOST);
213        STR(VK_ERROR_LAYER_NOT_PRESENT);
214        STR(VK_ERROR_EXTENSION_NOT_PRESENT);
215        STR(VK_ERROR_MEMORY_MAP_FAILED);
216        STR(VK_ERROR_INCOMPATIBLE_DRIVER);
217#undef STR
218    default:
219        return "UNKNOWN_RESULT";
220    }
221}
222
223static const char *vk_physical_device_type_string(VkPhysicalDeviceType type) {
224    switch (type) {
225#define STR(r)                                                                 \
226    case VK_PHYSICAL_DEVICE_TYPE_##r:                                          \
227        return #r
228        STR(OTHER);
229        STR(INTEGRATED_GPU);
230        STR(DISCRETE_GPU);
231        STR(VIRTUAL_GPU);
232#undef STR
233    default:
234        return "UNKNOWN_DEVICE";
235    }
236}
237
238static const char *vk_format_string(VkFormat fmt) {
239    switch (fmt) {
240#define STR(r)                                                                 \
241    case VK_FORMAT_##r:                                                        \
242        return #r
243        STR(UNDEFINED);
244        STR(R4G4_UNORM_PACK8);
245        STR(R4G4B4A4_UNORM_PACK16);
246        STR(B4G4R4A4_UNORM_PACK16);
247        STR(R5G6B5_UNORM_PACK16);
248        STR(B5G6R5_UNORM_PACK16);
249        STR(R5G5B5A1_UNORM_PACK16);
250        STR(B5G5R5A1_UNORM_PACK16);
251        STR(A1R5G5B5_UNORM_PACK16);
252        STR(R8_UNORM);
253        STR(R8_SNORM);
254        STR(R8_USCALED);
255        STR(R8_SSCALED);
256        STR(R8_UINT);
257        STR(R8_SINT);
258        STR(R8_SRGB);
259        STR(R8G8_UNORM);
260        STR(R8G8_SNORM);
261        STR(R8G8_USCALED);
262        STR(R8G8_SSCALED);
263        STR(R8G8_UINT);
264        STR(R8G8_SINT);
265        STR(R8G8_SRGB);
266        STR(R8G8B8_UNORM);
267        STR(R8G8B8_SNORM);
268        STR(R8G8B8_USCALED);
269        STR(R8G8B8_SSCALED);
270        STR(R8G8B8_UINT);
271        STR(R8G8B8_SINT);
272        STR(R8G8B8_SRGB);
273        STR(B8G8R8_UNORM);
274        STR(B8G8R8_SNORM);
275        STR(B8G8R8_USCALED);
276        STR(B8G8R8_SSCALED);
277        STR(B8G8R8_UINT);
278        STR(B8G8R8_SINT);
279        STR(B8G8R8_SRGB);
280        STR(R8G8B8A8_UNORM);
281        STR(R8G8B8A8_SNORM);
282        STR(R8G8B8A8_USCALED);
283        STR(R8G8B8A8_SSCALED);
284        STR(R8G8B8A8_UINT);
285        STR(R8G8B8A8_SINT);
286        STR(R8G8B8A8_SRGB);
287        STR(B8G8R8A8_UNORM);
288        STR(B8G8R8A8_SNORM);
289        STR(B8G8R8A8_USCALED);
290        STR(B8G8R8A8_SSCALED);
291        STR(B8G8R8A8_UINT);
292        STR(B8G8R8A8_SINT);
293        STR(B8G8R8A8_SRGB);
294        STR(A8B8G8R8_UNORM_PACK32);
295        STR(A8B8G8R8_SNORM_PACK32);
296        STR(A8B8G8R8_USCALED_PACK32);
297        STR(A8B8G8R8_SSCALED_PACK32);
298        STR(A8B8G8R8_UINT_PACK32);
299        STR(A8B8G8R8_SINT_PACK32);
300        STR(A8B8G8R8_SRGB_PACK32);
301        STR(A2R10G10B10_UNORM_PACK32);
302        STR(A2R10G10B10_SNORM_PACK32);
303        STR(A2R10G10B10_USCALED_PACK32);
304        STR(A2R10G10B10_SSCALED_PACK32);
305        STR(A2R10G10B10_UINT_PACK32);
306        STR(A2R10G10B10_SINT_PACK32);
307        STR(A2B10G10R10_UNORM_PACK32);
308        STR(A2B10G10R10_SNORM_PACK32);
309        STR(A2B10G10R10_USCALED_PACK32);
310        STR(A2B10G10R10_SSCALED_PACK32);
311        STR(A2B10G10R10_UINT_PACK32);
312        STR(A2B10G10R10_SINT_PACK32);
313        STR(R16_UNORM);
314        STR(R16_SNORM);
315        STR(R16_USCALED);
316        STR(R16_SSCALED);
317        STR(R16_UINT);
318        STR(R16_SINT);
319        STR(R16_SFLOAT);
320        STR(R16G16_UNORM);
321        STR(R16G16_SNORM);
322        STR(R16G16_USCALED);
323        STR(R16G16_SSCALED);
324        STR(R16G16_UINT);
325        STR(R16G16_SINT);
326        STR(R16G16_SFLOAT);
327        STR(R16G16B16_UNORM);
328        STR(R16G16B16_SNORM);
329        STR(R16G16B16_USCALED);
330        STR(R16G16B16_SSCALED);
331        STR(R16G16B16_UINT);
332        STR(R16G16B16_SINT);
333        STR(R16G16B16_SFLOAT);
334        STR(R16G16B16A16_UNORM);
335        STR(R16G16B16A16_SNORM);
336        STR(R16G16B16A16_USCALED);
337        STR(R16G16B16A16_SSCALED);
338        STR(R16G16B16A16_UINT);
339        STR(R16G16B16A16_SINT);
340        STR(R16G16B16A16_SFLOAT);
341        STR(R32_UINT);
342        STR(R32_SINT);
343        STR(R32_SFLOAT);
344        STR(R32G32_UINT);
345        STR(R32G32_SINT);
346        STR(R32G32_SFLOAT);
347        STR(R32G32B32_UINT);
348        STR(R32G32B32_SINT);
349        STR(R32G32B32_SFLOAT);
350        STR(R32G32B32A32_UINT);
351        STR(R32G32B32A32_SINT);
352        STR(R32G32B32A32_SFLOAT);
353        STR(R64_UINT);
354        STR(R64_SINT);
355        STR(R64_SFLOAT);
356        STR(R64G64_UINT);
357        STR(R64G64_SINT);
358        STR(R64G64_SFLOAT);
359        STR(R64G64B64_UINT);
360        STR(R64G64B64_SINT);
361        STR(R64G64B64_SFLOAT);
362        STR(R64G64B64A64_UINT);
363        STR(R64G64B64A64_SINT);
364        STR(R64G64B64A64_SFLOAT);
365        STR(B10G11R11_UFLOAT_PACK32);
366        STR(E5B9G9R9_UFLOAT_PACK32);
367        STR(D16_UNORM);
368        STR(X8_D24_UNORM_PACK32);
369        STR(D32_SFLOAT);
370        STR(S8_UINT);
371        STR(D16_UNORM_S8_UINT);
372        STR(D24_UNORM_S8_UINT);
373        STR(D32_SFLOAT_S8_UINT);
374        STR(BC1_RGB_UNORM_BLOCK);
375        STR(BC1_RGB_SRGB_BLOCK);
376        STR(BC2_UNORM_BLOCK);
377        STR(BC2_SRGB_BLOCK);
378        STR(BC3_UNORM_BLOCK);
379        STR(BC3_SRGB_BLOCK);
380        STR(BC4_UNORM_BLOCK);
381        STR(BC4_SNORM_BLOCK);
382        STR(BC5_UNORM_BLOCK);
383        STR(BC5_SNORM_BLOCK);
384        STR(BC6H_UFLOAT_BLOCK);
385        STR(BC6H_SFLOAT_BLOCK);
386        STR(BC7_UNORM_BLOCK);
387        STR(BC7_SRGB_BLOCK);
388        STR(ETC2_R8G8B8_UNORM_BLOCK);
389        STR(ETC2_R8G8B8A1_UNORM_BLOCK);
390        STR(ETC2_R8G8B8A8_UNORM_BLOCK);
391        STR(EAC_R11_UNORM_BLOCK);
392        STR(EAC_R11_SNORM_BLOCK);
393        STR(EAC_R11G11_UNORM_BLOCK);
394        STR(EAC_R11G11_SNORM_BLOCK);
395        STR(ASTC_4x4_UNORM_BLOCK);
396        STR(ASTC_4x4_SRGB_BLOCK);
397        STR(ASTC_5x4_UNORM_BLOCK);
398        STR(ASTC_5x4_SRGB_BLOCK);
399        STR(ASTC_5x5_UNORM_BLOCK);
400        STR(ASTC_5x5_SRGB_BLOCK);
401        STR(ASTC_6x5_UNORM_BLOCK);
402        STR(ASTC_6x5_SRGB_BLOCK);
403        STR(ASTC_6x6_UNORM_BLOCK);
404        STR(ASTC_6x6_SRGB_BLOCK);
405        STR(ASTC_8x5_UNORM_BLOCK);
406        STR(ASTC_8x5_SRGB_BLOCK);
407        STR(ASTC_8x6_UNORM_BLOCK);
408        STR(ASTC_8x6_SRGB_BLOCK);
409        STR(ASTC_8x8_UNORM_BLOCK);
410        STR(ASTC_8x8_SRGB_BLOCK);
411        STR(ASTC_10x5_UNORM_BLOCK);
412        STR(ASTC_10x5_SRGB_BLOCK);
413        STR(ASTC_10x6_UNORM_BLOCK);
414        STR(ASTC_10x6_SRGB_BLOCK);
415        STR(ASTC_10x8_UNORM_BLOCK);
416        STR(ASTC_10x8_SRGB_BLOCK);
417        STR(ASTC_10x10_UNORM_BLOCK);
418        STR(ASTC_10x10_SRGB_BLOCK);
419        STR(ASTC_12x10_UNORM_BLOCK);
420        STR(ASTC_12x10_SRGB_BLOCK);
421        STR(ASTC_12x12_UNORM_BLOCK);
422        STR(ASTC_12x12_SRGB_BLOCK);
423#undef STR
424    default:
425        return "UNKNOWN_FORMAT";
426    }
427}
428
429static void app_dev_init_formats(struct app_dev *dev) {
430    VkFormat f;
431
432    for (f = 0; f < VK_FORMAT_RANGE_SIZE; f++) {
433        const VkFormat fmt = f;
434
435        vkGetPhysicalDeviceFormatProperties(dev->gpu->obj, fmt,
436                                            &dev->format_props[f]);
437    }
438}
439
440static void extract_version(uint32_t version, uint32_t *major, uint32_t *minor,
441                            uint32_t *patch) {
442    *major = version >> 22;
443    *minor = (version >> 12) & 0x3ff;
444    *patch = version & 0xfff;
445}
446
447static void app_get_physical_device_layer_extensions(
448    struct app_gpu *gpu, char *layer_name, uint32_t *extension_count,
449    VkExtensionProperties **extension_properties) {
450    VkResult err;
451    uint32_t ext_count = 0;
452    VkExtensionProperties *ext_ptr = NULL;
453
454    /* repeat get until VK_INCOMPLETE goes away */
455    do {
456        err = vkEnumerateDeviceExtensionProperties(gpu->obj, layer_name,
457                                                   &ext_count, NULL);
458        assert(!err);
459
460        if (ext_ptr) {
461            free(ext_ptr);
462        }
463        ext_ptr = malloc(ext_count * sizeof(VkExtensionProperties));
464        err = vkEnumerateDeviceExtensionProperties(gpu->obj, layer_name,
465                                                   &ext_count, ext_ptr);
466    } while (err == VK_INCOMPLETE);
467    assert(!err);
468
469    *extension_count = ext_count;
470    *extension_properties = ext_ptr;
471}
472
473static void app_dev_init(struct app_dev *dev, struct app_gpu *gpu) {
474    VkDeviceCreateInfo info = {
475        .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
476        .pNext = NULL,
477        .queueCreateInfoCount = 0,
478        .pQueueCreateInfos = NULL,
479        .enabledLayerCount = 0,
480        .ppEnabledLayerNames = NULL,
481        .enabledExtensionCount = 0,
482        .ppEnabledExtensionNames = NULL,
483    };
484    VkResult U_ASSERT_ONLY err;
485
486    // Device extensions
487    app_get_physical_device_layer_extensions(
488        gpu, NULL, &gpu->device_extension_count, &gpu->device_extensions);
489
490    fflush(stdout);
491
492    /* request all queues */
493    info.queueCreateInfoCount = gpu->queue_count;
494    info.pQueueCreateInfos = gpu->queue_reqs;
495
496    info.enabledLayerCount = 0;
497    info.ppEnabledLayerNames = NULL;
498    info.enabledExtensionCount = 0;
499    info.ppEnabledExtensionNames = NULL;
500    dev->gpu = gpu;
501    err = vkCreateDevice(gpu->obj, &info, NULL, &dev->obj);
502    if (err)
503        ERR_EXIT(err);
504}
505
506static void app_dev_destroy(struct app_dev *dev) {
507    vkDestroyDevice(dev->obj, NULL);
508}
509
510static void
511app_get_global_layer_extensions(char *layer_name, uint32_t *extension_count,
512                                VkExtensionProperties **extension_properties) {
513    VkResult err;
514    uint32_t ext_count = 0;
515    VkExtensionProperties *ext_ptr = NULL;
516
517    /* repeat get until VK_INCOMPLETE goes away */
518    do {
519        // gets the extension count if the last parameter is NULL
520        err = vkEnumerateInstanceExtensionProperties(layer_name, &ext_count,
521                                                     NULL);
522        assert(!err);
523
524        if (ext_ptr) {
525            free(ext_ptr);
526        }
527        ext_ptr = malloc(ext_count * sizeof(VkExtensionProperties));
528        // gets the extension properties if the last parameter is not NULL
529        err = vkEnumerateInstanceExtensionProperties(layer_name, &ext_count,
530                                                     ext_ptr);
531    } while (err == VK_INCOMPLETE);
532    assert(!err);
533    *extension_count = ext_count;
534    *extension_properties = ext_ptr;
535}
536
537/* Gets a list of layer and instance extensions */
538static void app_get_instance_extensions(struct app_instance *inst) {
539    VkResult U_ASSERT_ONLY err;
540
541    uint32_t count = 0;
542
543    /* Scan layers */
544    VkLayerProperties *global_layer_properties = NULL;
545    struct layer_extension_list *global_layers = NULL;
546
547    do {
548        err = vkEnumerateInstanceLayerProperties(&count, NULL);
549        assert(!err);
550
551        if (global_layer_properties) {
552            free(global_layer_properties);
553        }
554        global_layer_properties = malloc(sizeof(VkLayerProperties) * count);
555        assert(global_layer_properties);
556
557        if (global_layers) {
558            free(global_layers);
559        }
560        global_layers = malloc(sizeof(struct layer_extension_list) * count);
561        assert(global_layers);
562
563        err =
564            vkEnumerateInstanceLayerProperties(&count, global_layer_properties);
565    } while (err == VK_INCOMPLETE);
566    assert(!err);
567
568    inst->global_layer_count = count;
569    inst->global_layers = global_layers;
570
571    for (uint32_t i = 0; i < inst->global_layer_count; i++) {
572        VkLayerProperties *src_info = &global_layer_properties[i];
573        struct layer_extension_list *dst_info = &inst->global_layers[i];
574        memcpy(&dst_info->layer_properties, src_info,
575               sizeof(VkLayerProperties));
576
577        // Save away layer extension info for report
578        // Gets layer extensions, if first parameter is not NULL
579        app_get_global_layer_extensions(src_info->layerName,
580                                        &dst_info->extension_count,
581                                        &dst_info->extension_properties);
582    }
583    free(global_layer_properties);
584
585    // Collect global extensions
586    inst->global_extension_count = 0;
587    // Gets instance extensions, if no layer was specified in the first
588    // paramteter
589    app_get_global_layer_extensions(NULL, &inst->global_extension_count,
590                                    &inst->global_extensions);
591}
592
593static void app_create_instance(struct app_instance *inst) {
594    app_get_instance_extensions(inst);
595
596//---Build a list of extensions to load---
597#define MAX_EXTENSIONS 4
598    uint32_t i = 0;
599    uint32_t ext_count = 0;
600    const char *ext_names[MAX_EXTENSIONS]; // array of string pointers to
601                                           // extension names
602    for (i = 0; (i < inst->global_extension_count); i++) {
603        const char *found_name = inst->global_extensions[i].extensionName;
604        if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, found_name)) {
605            ext_names[ext_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
606        }
607    }
608
609    if (ext_count)
610        for (i = 0; ((i < inst->global_extension_count) &&
611                     (ext_count < MAX_EXTENSIONS));
612             i++) {
613            const char *found_name = inst->global_extensions[i].extensionName;
614#ifdef VK_USE_PLATFORM_WIN32_KHR
615            if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, found_name)) {
616                ext_names[ext_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
617            }
618#endif
619#ifdef VK_USE_PLATFORM_XCB_KHR
620            if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, found_name)) {
621                ext_names[ext_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
622            }
623#endif
624#ifdef VK_USE_PLATFORM_XLIB_KHR
625            if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, found_name)) {
626                ext_names[ext_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
627            }
628#endif
629#ifdef VK_USE_PLATFORM_ANDROID_KHR
630            if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, found_name)) {
631                ext_names[ext_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
632            }
633#endif
634        }
635    // If we don't find the KHR_SURFACE extension and at least one other
636    // device-specific extension,
637    // then give up on reporting presentable surface formats."
638    if (ext_count < 2)
639        ext_count = 0;
640    //----------------------------------------
641
642    const VkApplicationInfo app_info = {
643        .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
644        .pNext = NULL,
645        .pApplicationName = APP_SHORT_NAME,
646        .applicationVersion = 1,
647        .pEngineName = APP_SHORT_NAME,
648        .engineVersion = 1,
649        .apiVersion = VK_API_VERSION_1_0,
650    };
651
652    VkInstanceCreateInfo inst_info = {
653        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
654        .pNext = NULL,
655        .pApplicationInfo = &app_info,
656        .enabledLayerCount = 0,
657        .ppEnabledLayerNames = NULL,
658        .enabledExtensionCount = ext_count,
659        .ppEnabledExtensionNames = ext_names,
660    };
661
662    VkDebugReportCallbackCreateInfoEXT dbg_info;
663    memset(&dbg_info, 0, sizeof(dbg_info));
664    dbg_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
665    dbg_info.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
666                     VK_DEBUG_REPORT_WARNING_BIT_EXT |
667                     VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
668    dbg_info.pfnCallback = dbg_callback;
669    inst_info.pNext = &dbg_info;
670
671    VkResult U_ASSERT_ONLY err;
672    err = vkCreateInstance(&inst_info, NULL, &inst->instance);
673    if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
674        printf("Cannot create Vulkan instance.\n");
675        ERR_EXIT(err);
676    } else if (err) {
677        ERR_EXIT(err);
678    }
679
680    if (ext_count > 0) {
681//--Load Extensions--
682#define GET_INSTANCE_PROC_ADDR(ENTRYPOINT)                                     \
683    {                                                                          \
684        inst->ENTRYPOINT =                                                     \
685            (void *)vkGetInstanceProcAddr(inst->instance, #ENTRYPOINT);        \
686    }
687        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceSupportKHR)
688        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
689        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceFormatsKHR)
690        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfacePresentModesKHR)
691#undef GET_INSTANCE_PROC_ADDR
692    }
693}
694
695//-----------------------------------------------------------
696
697static void app_destroy_instance(struct app_instance *inst) {
698    free(inst->global_extensions);
699    vkDestroyInstance(inst->instance, NULL);
700}
701
702static void app_gpu_init(struct app_gpu *gpu, uint32_t id,
703                         VkPhysicalDevice obj) {
704    uint32_t i;
705
706    memset(gpu, 0, sizeof(*gpu));
707
708    gpu->id = id;
709    gpu->obj = obj;
710
711    vkGetPhysicalDeviceProperties(gpu->obj, &gpu->props);
712
713    /* get queue count */
714    vkGetPhysicalDeviceQueueFamilyProperties(gpu->obj, &gpu->queue_count, NULL);
715
716    gpu->queue_props = malloc(sizeof(gpu->queue_props[0]) * gpu->queue_count);
717
718    if (!gpu->queue_props)
719        ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
720    vkGetPhysicalDeviceQueueFamilyProperties(gpu->obj, &gpu->queue_count,
721                                             gpu->queue_props);
722
723    /* set up queue requests */
724    gpu->queue_reqs = malloc(sizeof(*gpu->queue_reqs) * gpu->queue_count);
725    if (!gpu->queue_reqs)
726        ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
727    for (i = 0; i < gpu->queue_count; i++) {
728        float *queue_priorities =
729            malloc(gpu->queue_props[i].queueCount * sizeof(float));
730        memset(queue_priorities, 0,
731               gpu->queue_props[i].queueCount * sizeof(float));
732        gpu->queue_reqs[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
733        gpu->queue_reqs[i].pNext = NULL;
734        gpu->queue_reqs[i].queueFamilyIndex = i;
735        gpu->queue_reqs[i].queueCount = gpu->queue_props[i].queueCount;
736        gpu->queue_reqs[i].pQueuePriorities = queue_priorities;
737    }
738
739    vkGetPhysicalDeviceMemoryProperties(gpu->obj, &gpu->memory_props);
740
741    vkGetPhysicalDeviceFeatures(gpu->obj, &gpu->features);
742
743    app_dev_init(&gpu->dev, gpu);
744    app_dev_init_formats(&gpu->dev);
745}
746
747static void app_gpu_destroy(struct app_gpu *gpu) {
748    app_dev_destroy(&gpu->dev);
749    free(gpu->device_extensions);
750
751    for (uint32_t i = 0; i < gpu->queue_count; i++) {
752        free((void *)gpu->queue_reqs[i].pQueuePriorities);
753    }
754    free(gpu->queue_reqs);
755    free(gpu->queue_props);
756}
757
758// clang-format off
759
760//-----------------------------------------------------------
761
762//---------------------------Win32---------------------------
763#ifdef VK_USE_PLATFORM_WIN32_KHR
764
765// MS-Windows event handling function:
766LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
767    return (DefWindowProc(hWnd, uMsg, wParam, lParam));
768}
769
770static void app_create_win32_window(struct app_instance *inst) {
771    inst->hInstance = GetModuleHandle(NULL);
772
773    WNDCLASSEX win_class;
774
775    // Initialize the window class structure:
776    win_class.cbSize = sizeof(WNDCLASSEX);
777    win_class.style = CS_HREDRAW | CS_VREDRAW;
778    win_class.lpfnWndProc = WndProc;
779    win_class.cbClsExtra = 0;
780    win_class.cbWndExtra = 0;
781    win_class.hInstance = inst->hInstance;
782    win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
783    win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
784    win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
785    win_class.lpszMenuName = NULL;
786    win_class.lpszClassName = APP_SHORT_NAME;
787    win_class.hInstance = inst->hInstance;
788    win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
789    // Register window class:
790    if (!RegisterClassEx(&win_class)) {
791        // It didn't work, so try to give a useful error:
792        printf("Failed to register the window class!\n");
793        fflush(stdout);
794        exit(1);
795    }
796    // Create window with the registered class:
797    RECT wr = { 0, 0, inst->width, inst->height };
798    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
799    inst->hWnd = CreateWindowEx(0,
800        APP_SHORT_NAME,       // class name
801        APP_SHORT_NAME,       // app name
802        //WS_VISIBLE | WS_SYSMENU |
803        WS_OVERLAPPEDWINDOW,  // window style
804        100, 100,             // x/y coords
805        wr.right - wr.left,   // width
806        wr.bottom - wr.top,   // height
807        NULL,                 // handle to parent
808        NULL,                 // handle to menu
809        inst->hInstance,      // hInstance
810        NULL);                // no extra parameters
811    if (!inst->hWnd) {
812        // It didn't work, so try to give a useful error:
813        printf("Failed to create a window!\n");
814        fflush(stdout);
815        exit(1);
816    }
817}
818
819static void app_create_win32_surface(struct app_instance *inst, struct app_gpu *gpu) {
820    VkResult U_ASSERT_ONLY err;
821    VkWin32SurfaceCreateInfoKHR createInfo;
822    createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
823    createInfo.pNext = NULL;
824    createInfo.flags = 0;
825    createInfo.hinstance = inst->hInstance;
826    createInfo.hwnd = inst->hWnd;
827    err = vkCreateWin32SurfaceKHR(inst->instance, &createInfo, NULL, &inst->surface);
828    assert(!err);
829}
830
831static void app_destroy_win32_window(struct app_instance *inst) {
832    DestroyWindow(inst->hWnd);
833}
834#endif //VK_USE_PLATFORM_WIN32_KHR
835//-----------------------------------------------------------
836
837static void app_destroy_surface(struct app_instance *inst) { //same for all platforms
838    vkDestroySurfaceKHR(inst->instance, inst->surface, NULL);
839}
840
841//----------------------------XCB----------------------------
842
843#ifdef VK_USE_PLATFORM_XCB_KHR
844static void app_create_xcb_window(struct app_instance *inst) {
845  //--Init Connection--
846  const xcb_setup_t *setup;
847  xcb_screen_iterator_t iter;
848  int scr;
849
850  inst->xcb_connection = xcb_connect(NULL, &scr);
851  if (inst->xcb_connection == NULL) {
852      printf("XCB failed to connect to the X server.\nExiting ...\n");
853      fflush(stdout);
854      exit(1);
855  }
856
857  setup = xcb_get_setup(inst->xcb_connection);
858  iter = xcb_setup_roots_iterator(setup);
859  while (scr-- > 0)
860      xcb_screen_next(&iter);
861
862  inst->xcb_screen = iter.data;
863  //-------------------
864
865  inst->xcb_window = xcb_generate_id(inst->xcb_connection);
866  xcb_create_window(inst->xcb_connection, XCB_COPY_FROM_PARENT, inst->xcb_window,
867                    inst->xcb_screen->root, 0, 0, inst->width, inst->height, 0,
868                    XCB_WINDOW_CLASS_INPUT_OUTPUT, inst->xcb_screen->root_visual,
869                    0, NULL);
870
871  xcb_intern_atom_cookie_t cookie = xcb_intern_atom(inst->xcb_connection, 1, 12, "WM_PROTOCOLS");
872  xcb_intern_atom_reply_t *reply =  xcb_intern_atom_reply(inst->xcb_connection, cookie, 0);
873  free(reply);
874}
875
876static void app_create_xcb_surface(struct app_instance *inst, struct app_gpu *gpu) {
877    VkResult U_ASSERT_ONLY err;
878    VkXcbSurfaceCreateInfoKHR xcb_createInfo;
879    xcb_createInfo.sType      = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
880    xcb_createInfo.pNext      = NULL;
881    xcb_createInfo.flags      = 0;
882    xcb_createInfo.connection = inst->xcb_connection;
883    xcb_createInfo.window     = inst->xcb_window;
884    err = vkCreateXcbSurfaceKHR(inst->instance, &xcb_createInfo, NULL, &inst->surface);
885    assert(!err);
886}
887
888static void app_destroy_xcb_window(struct app_instance *inst) {
889    xcb_destroy_window(inst->xcb_connection, inst->xcb_window);
890    xcb_disconnect(inst->xcb_connection);
891}
892#endif //VK_USE_PLATFORM_XCB_KHR
893//-----------------------------------------------------------
894
895//----------------------------XLib---------------------------
896#ifdef VK_USE_PLATFORM_XLIB_KHR
897static void app_create_xlib_window(struct app_instance *inst) {
898    inst->xlib_display = XOpenDisplay(NULL);
899    long visualMask = VisualScreenMask;
900    int numberOfVisuals;
901
902    XVisualInfo vInfoTemplate={};
903    vInfoTemplate.screen = DefaultScreen(inst->xlib_display);
904    XVisualInfo *visualInfo = XGetVisualInfo(inst->xlib_display, visualMask,
905                                             &vInfoTemplate, &numberOfVisuals);
906    inst->xlib_window = XCreateWindow(
907                inst->xlib_display, RootWindow(inst->xlib_display, vInfoTemplate.screen), 0, 0,
908                inst->width, inst->height, 0, visualInfo->depth, InputOutput,
909                visualInfo->visual, 0, NULL);
910
911    XSync(inst->xlib_display,false);
912}
913
914static void app_create_xlib_surface(struct app_instance *inst, struct app_gpu *gpu) {
915    VkResult U_ASSERT_ONLY err;
916    VkXlibSurfaceCreateInfoKHR createInfo;
917    createInfo.sType  = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
918    createInfo.pNext  = NULL;
919    createInfo.flags  = 0;
920    createInfo.dpy    = inst->xlib_display;
921    createInfo.window = inst->xlib_window;
922    err = vkCreateXlibSurfaceKHR(inst->instance, &createInfo, NULL, &inst->surface);
923    assert(!err);
924}
925
926static void app_destroy_xlib_window(struct app_instance *inst) {
927    XDestroyWindow(inst->xlib_display, inst->xlib_window);
928    XCloseDisplay(inst->xlib_display);
929}
930#endif //VK_USE_PLATFORM_XLIB_KHR
931//-----------------------------------------------------------
932
933static int app_dump_surface_formats(struct app_instance *inst, struct app_gpu *gpu){
934    // Get the list of VkFormat's that are supported:
935  VkResult U_ASSERT_ONLY err;
936  uint32_t formatCount=0;
937  err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, NULL);
938  assert(!err);
939  VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
940  err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, surfFormats);
941  assert(!err);
942  printf("Format count = %d\n",formatCount);
943  uint32_t i;
944  for(i=0;i<formatCount;i++) printf("\t%s\n",vk_format_string(surfFormats[i].format));
945  printf("\n");
946  fflush(stdout);
947  return formatCount;
948}
949
950static void app_dev_dump_format_props(const struct app_dev *dev, VkFormat fmt)
951{
952    const VkFormatProperties *props = &dev->format_props[fmt];
953    struct {
954        const char *name;
955        VkFlags flags;
956    } features[3];
957    uint32_t i;
958
959    features[0].name  = "linearTiling   FormatFeatureFlags";
960    features[0].flags = props->linearTilingFeatures;
961    features[1].name  = "optimalTiling  FormatFeatureFlags";
962    features[1].flags = props->optimalTilingFeatures;
963    features[2].name  = "bufferFeatures FormatFeatureFlags";
964    features[2].flags = props->bufferFeatures;
965
966    printf("\nFORMAT_%s:", vk_format_string(fmt));
967    for (i = 0; i < ARRAY_SIZE(features); i++) {
968        printf("\n\t%s:", features[i].name);
969        if (features[i].flags == 0) {
970            printf("\n\t\tNone");
971        } else {
972            printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
973               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT"                  : ""),  //0x0001
974               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_BIT"                  : ""),  //0x0002
975               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT"           : ""),  //0x0004
976               ((features[i].flags & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT"           : ""),  //0x0008
977               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT"           : ""),  //0x0010
978               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT"    : ""),  //0x0020
979               ((features[i].flags & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_VERTEX_BUFFER_BIT"                  : ""),  //0x0040
980               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)               ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT"               : ""),  //0x0080
981               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)         ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT"         : ""),  //0x0100
982               ((features[i].flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)       ? "\n\t\tVK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT"       : ""),  //0x0200
983               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_SRC_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_SRC_BIT"                       : ""),  //0x0400
984               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_DST_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_DST_BIT"                       : ""),  //0x0800
985               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT"    : ""),  //0x1000
986               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG) ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG" : "")); //0x2000
987        }
988    }
989    printf("\n");
990}
991
992
993static void
994app_dev_dump(const struct app_dev *dev)
995{
996    printf("Format Properties:\n");
997    printf("==================");
998    VkFormat fmt;
999
1000    for (fmt = 0; fmt < VK_FORMAT_RANGE_SIZE; fmt++) {
1001        app_dev_dump_format_props(dev, fmt);
1002    }
1003}
1004
1005#ifdef _WIN32
1006#define PRINTF_SIZE_T_SPECIFIER    "%Iu"
1007#else
1008#define PRINTF_SIZE_T_SPECIFIER    "%zu"
1009#endif
1010
1011static void app_gpu_dump_features(const struct app_gpu *gpu)
1012{
1013    const VkPhysicalDeviceFeatures *features = &gpu->features;
1014
1015    printf("VkPhysicalDeviceFeatures:\n");
1016    printf("=========================\n");
1017
1018    printf("\trobustBufferAccess                      = %u\n", features->robustBufferAccess                     );
1019    printf("\tfullDrawIndexUint32                     = %u\n", features->fullDrawIndexUint32                    );
1020    printf("\timageCubeArray                          = %u\n", features->imageCubeArray                         );
1021    printf("\tindependentBlend                        = %u\n", features->independentBlend                       );
1022    printf("\tgeometryShader                          = %u\n", features->geometryShader                         );
1023    printf("\ttessellationShader                      = %u\n", features->tessellationShader                     );
1024    printf("\tsampleRateShading                       = %u\n", features->sampleRateShading                      );
1025    printf("\tdualSrcBlend                            = %u\n", features->dualSrcBlend                           );
1026    printf("\tlogicOp                                 = %u\n", features->logicOp                                );
1027    printf("\tmultiDrawIndirect                       = %u\n", features->multiDrawIndirect                      );
1028    printf("\tdrawIndirectFirstInstance               = %u\n", features->drawIndirectFirstInstance              );
1029    printf("\tdepthClamp                              = %u\n", features->depthClamp                             );
1030    printf("\tdepthBiasClamp                          = %u\n", features->depthBiasClamp                         );
1031    printf("\tfillModeNonSolid                        = %u\n", features->fillModeNonSolid                       );
1032    printf("\tdepthBounds                             = %u\n", features->depthBounds                            );
1033    printf("\twideLines                               = %u\n", features->wideLines                              );
1034    printf("\tlargePoints                             = %u\n", features->largePoints                            );
1035    printf("\ttextureCompressionETC2                  = %u\n", features->textureCompressionETC2                 );
1036    printf("\ttextureCompressionASTC_LDR              = %u\n", features->textureCompressionASTC_LDR             );
1037    printf("\ttextureCompressionBC                    = %u\n", features->textureCompressionBC                   );
1038    printf("\tocclusionQueryPrecise                   = %u\n", features->occlusionQueryPrecise                  );
1039    printf("\tpipelineStatisticsQuery                 = %u\n", features->pipelineStatisticsQuery                );
1040    printf("\tvertexSideEffects                       = %u\n", features->vertexPipelineStoresAndAtomics         );
1041    printf("\ttessellationSideEffects                 = %u\n", features->fragmentStoresAndAtomics               );
1042    printf("\tgeometrySideEffects                     = %u\n", features->shaderTessellationAndGeometryPointSize );
1043    printf("\tshaderImageGatherExtended               = %u\n", features->shaderImageGatherExtended              );
1044    printf("\tshaderStorageImageExtendedFormats       = %u\n", features->shaderStorageImageExtendedFormats      );
1045    printf("\tshaderStorageImageMultisample           = %u\n", features->shaderStorageImageMultisample          );
1046    printf("\tshaderStorageImageReadWithoutFormat     = %u\n", features->shaderStorageImageReadWithoutFormat    );
1047    printf("\tshaderStorageImageWriteWithoutFormat    = %u\n", features->shaderStorageImageWriteWithoutFormat   );
1048    printf("\tshaderUniformBufferArrayDynamicIndexing = %u\n", features->shaderUniformBufferArrayDynamicIndexing);
1049    printf("\tshaderSampledImageArrayDynamicIndexing  = %u\n", features->shaderSampledImageArrayDynamicIndexing );
1050    printf("\tshaderStorageBufferArrayDynamicIndexing = %u\n", features->shaderStorageBufferArrayDynamicIndexing);
1051    printf("\tshaderStorageImageArrayDynamicIndexing  = %u\n", features->shaderStorageImageArrayDynamicIndexing );
1052    printf("\tshaderClipDistance                      = %u\n", features->shaderClipDistance                     );
1053    printf("\tshaderCullDistance                      = %u\n", features->shaderCullDistance                     );
1054    printf("\tshaderFloat64                           = %u\n", features->shaderFloat64                          );
1055    printf("\tshaderInt64                             = %u\n", features->shaderInt64                            );
1056    printf("\tshaderInt16                             = %u\n", features->shaderInt16                            );
1057    printf("\tshaderResourceResidency                 = %u\n", features->shaderResourceResidency                );
1058    printf("\tshaderResourceMinLod                    = %u\n", features->shaderResourceMinLod                   );
1059    printf("\talphaToOne                              = %u\n", features->alphaToOne                             );
1060    printf("\tsparseBinding                           = %u\n", features->sparseBinding                          );
1061    printf("\tsparseResidencyBuffer                   = %u\n", features->sparseResidencyBuffer                  );
1062    printf("\tsparseResidencyImage2D                  = %u\n", features->sparseResidencyImage2D                 );
1063    printf("\tsparseResidencyImage3D                  = %u\n", features->sparseResidencyImage3D                 );
1064    printf("\tsparseResidency2Samples                 = %u\n", features->sparseResidency2Samples                );
1065    printf("\tsparseResidency4Samples                 = %u\n", features->sparseResidency4Samples                );
1066    printf("\tsparseResidency8Samples                 = %u\n", features->sparseResidency8Samples                );
1067    printf("\tsparseResidency16Samples                = %u\n", features->sparseResidency16Samples               );
1068    printf("\tsparseResidencyAliased                  = %u\n", features->sparseResidencyAliased                 );
1069    printf("\tvariableMultisampleRate                 = %u\n", features->variableMultisampleRate                );
1070    printf("\tiheritedQueries                         = %u\n", features->inheritedQueries                       );
1071}
1072
1073static void app_dump_sparse_props(const VkPhysicalDeviceSparseProperties *sparseProps)
1074{
1075
1076    printf("\tVkPhysicalDeviceSparseProperties:\n");
1077    printf("\t---------------------------------\n");
1078
1079    printf("\t\tresidencyStandard2DBlockShape            = %u\n", sparseProps->residencyStandard2DBlockShape           );
1080    printf("\t\tresidencyStandard2DMultisampleBlockShape = %u\n", sparseProps->residencyStandard2DMultisampleBlockShape);
1081    printf("\t\tresidencyStandard3DBlockShape            = %u\n", sparseProps->residencyStandard3DBlockShape           );
1082    printf("\t\tresidencyAlignedMipSize                  = %u\n", sparseProps->residencyAlignedMipSize                 );
1083    printf("\t\tresidencyNonResidentStrict               = %u\n", sparseProps->residencyNonResidentStrict              );
1084}
1085
1086static void app_dump_limits(const VkPhysicalDeviceLimits *limits)
1087{
1088    printf("\tVkPhysicalDeviceLimits:\n");
1089    printf("\t-----------------------\n");
1090    printf("\t\tmaxImageDimension1D                     = %u\n",                 limits->maxImageDimension1D                    );
1091    printf("\t\tmaxImageDimension2D                     = %u\n",                 limits->maxImageDimension2D                    );
1092    printf("\t\tmaxImageDimension3D                     = %u\n",                 limits->maxImageDimension3D                    );
1093    printf("\t\tmaxImageDimensionCube                   = %u\n",                 limits->maxImageDimensionCube                  );
1094    printf("\t\tmaxImageArrayLayers                     = %u\n",                 limits->maxImageArrayLayers                    );
1095    printf("\t\tmaxTexelBufferElements                  = 0x%" PRIxLEAST32 "\n", limits->maxTexelBufferElements                 );
1096    printf("\t\tmaxUniformBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxUniformBufferRange                  );
1097    printf("\t\tmaxStorageBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxStorageBufferRange                  );
1098    printf("\t\tmaxPushConstantsSize                    = %u\n",                 limits->maxPushConstantsSize                   );
1099    printf("\t\tmaxMemoryAllocationCount                = %u\n",                 limits->maxMemoryAllocationCount               );
1100    printf("\t\tmaxSamplerAllocationCount               = %u\n",                 limits->maxSamplerAllocationCount              );
1101    printf("\t\tbufferImageGranularity                  = 0x%" PRIxLEAST64 "\n", limits->bufferImageGranularity                 );
1102    printf("\t\tsparseAddressSpaceSize                  = 0x%" PRIxLEAST64 "\n", limits->sparseAddressSpaceSize                 );
1103    printf("\t\tmaxBoundDescriptorSets                  = %u\n",                 limits->maxBoundDescriptorSets                 );
1104    printf("\t\tmaxPerStageDescriptorSamplers           = %u\n",                 limits->maxPerStageDescriptorSamplers          );
1105    printf("\t\tmaxPerStageDescriptorUniformBuffers     = %u\n",                 limits->maxPerStageDescriptorUniformBuffers    );
1106    printf("\t\tmaxPerStageDescriptorStorageBuffers     = %u\n",                 limits->maxPerStageDescriptorStorageBuffers    );
1107    printf("\t\tmaxPerStageDescriptorSampledImages      = %u\n",                 limits->maxPerStageDescriptorSampledImages     );
1108    printf("\t\tmaxPerStageDescriptorStorageImages      = %u\n",                 limits->maxPerStageDescriptorStorageImages     );
1109    printf("\t\tmaxPerStageDescriptorInputAttachments   = %u\n",                 limits->maxPerStageDescriptorInputAttachments  );
1110    printf("\t\tmaxPerStageResources                    = %u\n",                 limits->maxPerStageResources                   );
1111    printf("\t\tmaxDescriptorSetSamplers                = %u\n",                 limits->maxDescriptorSetSamplers               );
1112    printf("\t\tmaxDescriptorSetUniformBuffers          = %u\n",                 limits->maxDescriptorSetUniformBuffers         );
1113    printf("\t\tmaxDescriptorSetUniformBuffersDynamic   = %u\n",                 limits->maxDescriptorSetUniformBuffersDynamic  );
1114    printf("\t\tmaxDescriptorSetStorageBuffers          = %u\n",                 limits->maxDescriptorSetStorageBuffers         );
1115    printf("\t\tmaxDescriptorSetStorageBuffersDynamic   = %u\n",                 limits->maxDescriptorSetStorageBuffersDynamic  );
1116    printf("\t\tmaxDescriptorSetSampledImages           = %u\n",                 limits->maxDescriptorSetSampledImages          );
1117    printf("\t\tmaxDescriptorSetStorageImages           = %u\n",                 limits->maxDescriptorSetStorageImages          );
1118    printf("\t\tmaxDescriptorSetInputAttachments        = %u\n",                 limits->maxDescriptorSetInputAttachments       );
1119    printf("\t\tmaxVertexInputAttributes                = %u\n",                 limits->maxVertexInputAttributes               );
1120    printf("\t\tmaxVertexInputBindings                  = %u\n",                 limits->maxVertexInputBindings                 );
1121    printf("\t\tmaxVertexInputAttributeOffset           = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputAttributeOffset          );
1122    printf("\t\tmaxVertexInputBindingStride             = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputBindingStride            );
1123    printf("\t\tmaxVertexOutputComponents               = %u\n",                 limits->maxVertexOutputComponents              );
1124    printf("\t\tmaxTessellationGenerationLevel          = %u\n",                 limits->maxTessellationGenerationLevel         );
1125    printf("\t\tmaxTessellationPatchSize                        = %u\n",                 limits->maxTessellationPatchSize                       );
1126    printf("\t\tmaxTessellationControlPerVertexInputComponents  = %u\n",                 limits->maxTessellationControlPerVertexInputComponents );
1127    printf("\t\tmaxTessellationControlPerVertexOutputComponents = %u\n",                 limits->maxTessellationControlPerVertexOutputComponents);
1128    printf("\t\tmaxTessellationControlPerPatchOutputComponents  = %u\n",                 limits->maxTessellationControlPerPatchOutputComponents );
1129    printf("\t\tmaxTessellationControlTotalOutputComponents     = %u\n",                 limits->maxTessellationControlTotalOutputComponents    );
1130    printf("\t\tmaxTessellationEvaluationInputComponents        = %u\n",                 limits->maxTessellationEvaluationInputComponents       );
1131    printf("\t\tmaxTessellationEvaluationOutputComponents       = %u\n",                 limits->maxTessellationEvaluationOutputComponents      );
1132    printf("\t\tmaxGeometryShaderInvocations            = %u\n",                 limits->maxGeometryShaderInvocations           );
1133    printf("\t\tmaxGeometryInputComponents              = %u\n",                 limits->maxGeometryInputComponents             );
1134    printf("\t\tmaxGeometryOutputComponents             = %u\n",                 limits->maxGeometryOutputComponents            );
1135    printf("\t\tmaxGeometryOutputVertices               = %u\n",                 limits->maxGeometryOutputVertices              );
1136    printf("\t\tmaxGeometryTotalOutputComponents        = %u\n",                 limits->maxGeometryTotalOutputComponents       );
1137    printf("\t\tmaxFragmentInputComponents              = %u\n",                 limits->maxFragmentInputComponents             );
1138    printf("\t\tmaxFragmentOutputAttachments            = %u\n",                 limits->maxFragmentOutputAttachments           );
1139    printf("\t\tmaxFragmentDualSrcAttachments           = %u\n",                 limits->maxFragmentDualSrcAttachments          );
1140    printf("\t\tmaxFragmentCombinedOutputResources      = %u\n",                 limits->maxFragmentCombinedOutputResources     );
1141    printf("\t\tmaxComputeSharedMemorySize              = 0x%" PRIxLEAST32 "\n", limits->maxComputeSharedMemorySize             );
1142    printf("\t\tmaxComputeWorkGroupCount[0]             = %u\n",                 limits->maxComputeWorkGroupCount[0]            );
1143    printf("\t\tmaxComputeWorkGroupCount[1]             = %u\n",                 limits->maxComputeWorkGroupCount[1]            );
1144    printf("\t\tmaxComputeWorkGroupCount[2]             = %u\n",                 limits->maxComputeWorkGroupCount[2]            );
1145    printf("\t\tmaxComputeWorkGroupInvocations          = %u\n",                 limits->maxComputeWorkGroupInvocations         );
1146    printf("\t\tmaxComputeWorkGroupSize[0]              = %u\n",                 limits->maxComputeWorkGroupSize[0]             );
1147    printf("\t\tmaxComputeWorkGroupSize[1]              = %u\n",                 limits->maxComputeWorkGroupSize[1]             );
1148    printf("\t\tmaxComputeWorkGroupSize[2]              = %u\n",                 limits->maxComputeWorkGroupSize[2]             );
1149    printf("\t\tsubPixelPrecisionBits                   = %u\n",                 limits->subPixelPrecisionBits                  );
1150    printf("\t\tsubTexelPrecisionBits                   = %u\n",                 limits->subTexelPrecisionBits                  );
1151    printf("\t\tmipmapPrecisionBits                     = %u\n",                 limits->mipmapPrecisionBits                    );
1152    printf("\t\tmaxDrawIndexedIndexValue                = %u\n",                 limits->maxDrawIndexedIndexValue               );
1153    printf("\t\tmaxDrawIndirectCount                    = %u\n",                 limits->maxDrawIndirectCount                   );
1154    printf("\t\tmaxSamplerLodBias                       = %f\n",                 limits->maxSamplerLodBias                      );
1155    printf("\t\tmaxSamplerAnisotropy                    = %f\n",                 limits->maxSamplerAnisotropy                   );
1156    printf("\t\tmaxViewports                            = %u\n",                 limits->maxViewports                           );
1157    printf("\t\tmaxViewportDimensions[0]                = %u\n",                 limits->maxViewportDimensions[0]               );
1158    printf("\t\tmaxViewportDimensions[1]                = %u\n",                 limits->maxViewportDimensions[1]               );
1159    printf("\t\tviewportBoundsRange[0]                  =%13f\n",                 limits->viewportBoundsRange[0]                 );
1160    printf("\t\tviewportBoundsRange[1]                  =%13f\n",                 limits->viewportBoundsRange[1]                 );
1161    printf("\t\tviewportSubPixelBits                    = %u\n",                 limits->viewportSubPixelBits                   );
1162    printf("\t\tminMemoryMapAlignment                   = " PRINTF_SIZE_T_SPECIFIER "\n", limits->minMemoryMapAlignment         );
1163    printf("\t\tminTexelBufferOffsetAlignment           = 0x%" PRIxLEAST64 "\n", limits->minTexelBufferOffsetAlignment          );
1164    printf("\t\tminUniformBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minUniformBufferOffsetAlignment        );
1165    printf("\t\tminStorageBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minStorageBufferOffsetAlignment        );
1166    printf("\t\tminTexelOffset                          =%3d\n",                 limits->minTexelOffset                         );
1167    printf("\t\tmaxTexelOffset                          =%3d\n",                 limits->maxTexelOffset                         );
1168    printf("\t\tminTexelGatherOffset                    =%3d\n",                 limits->minTexelGatherOffset                   );
1169    printf("\t\tmaxTexelGatherOffset                    =%3d\n",                 limits->maxTexelGatherOffset                   );
1170    printf("\t\tminInterpolationOffset                  =%9f\n",                 limits->minInterpolationOffset                 );
1171    printf("\t\tmaxInterpolationOffset                  =%9f\n",                 limits->maxInterpolationOffset                 );
1172    printf("\t\tsubPixelInterpolationOffsetBits         = %u\n",                 limits->subPixelInterpolationOffsetBits        );
1173    printf("\t\tmaxFramebufferWidth                     = %u\n",                 limits->maxFramebufferWidth                    );
1174    printf("\t\tmaxFramebufferHeight                    = %u\n",                 limits->maxFramebufferHeight                   );
1175    printf("\t\tmaxFramebufferLayers                    = %u\n",                 limits->maxFramebufferLayers                   );
1176    printf("\t\tframebufferColorSampleCounts            = %u\n",                 limits->framebufferColorSampleCounts           );
1177    printf("\t\tframebufferDepthSampleCounts            = %u\n",                 limits->framebufferDepthSampleCounts           );
1178    printf("\t\tframebufferStencilSampleCounts          = %u\n",                 limits->framebufferStencilSampleCounts         );
1179    printf("\t\tframebufferNoAttachmentsSampleCounts    = %u\n",                 limits->framebufferNoAttachmentsSampleCounts   );
1180    printf("\t\tmaxColorAttachments                     = %u\n",                 limits->maxColorAttachments                    );
1181    printf("\t\tsampledImageColorSampleCounts           = %u\n",                 limits->sampledImageColorSampleCounts          );
1182    printf("\t\tsampledImageDepthSampleCounts           = %u\n",                 limits->sampledImageDepthSampleCounts          );
1183    printf("\t\tsampledImageStencilSampleCounts         = %u\n",                 limits->sampledImageStencilSampleCounts        );
1184    printf("\t\tsampledImageIntegerSampleCounts         = %u\n",                 limits->sampledImageIntegerSampleCounts        );
1185    printf("\t\tstorageImageSampleCounts                = %u\n",                 limits->storageImageSampleCounts               );
1186    printf("\t\tmaxSampleMaskWords                      = %u\n",                 limits->maxSampleMaskWords                     );
1187    printf("\t\ttimestampComputeAndGraphics             = %u\n",                 limits->timestampComputeAndGraphics            );
1188    printf("\t\ttimestampPeriod                         = %f\n",                 limits->timestampPeriod                        );
1189    printf("\t\tmaxClipDistances                        = %u\n",                 limits->maxClipDistances                       );
1190    printf("\t\tmaxCullDistances                        = %u\n",                 limits->maxCullDistances                       );
1191    printf("\t\tmaxCombinedClipAndCullDistances         = %u\n",                 limits->maxCombinedClipAndCullDistances        );
1192    printf("\t\tdiscreteQueuePriorities                 = %u\n",                 limits->discreteQueuePriorities                );
1193    printf("\t\tpointSizeRange[0]                       = %f\n",                 limits->pointSizeRange[0]                      );
1194    printf("\t\tpointSizeRange[1]                       = %f\n",                 limits->pointSizeRange[1]                      );
1195    printf("\t\tlineWidthRange[0]                       = %f\n",                 limits->lineWidthRange[0]                      );
1196    printf("\t\tlineWidthRange[1]                       = %f\n",                 limits->lineWidthRange[1]                      );
1197    printf("\t\tpointSizeGranularity                    = %f\n",                 limits->pointSizeGranularity                   );
1198    printf("\t\tlineWidthGranularity                    = %f\n",                 limits->lineWidthGranularity                   );
1199    printf("\t\tstrictLines                             = %u\n",                 limits->strictLines                            );
1200    printf("\t\tstandardSampleLocations                 = %u\n",                 limits->standardSampleLocations                );
1201    printf("\t\toptimalBufferCopyOffsetAlignment        = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyOffsetAlignment       );
1202    printf("\t\toptimalBufferCopyRowPitchAlignment      = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyRowPitchAlignment     );
1203    printf("\t\tnonCoherentAtomSize                     = 0x%" PRIxLEAST64 "\n", limits->nonCoherentAtomSize                    );
1204}
1205
1206static void app_gpu_dump_props(const struct app_gpu *gpu)
1207{
1208    const VkPhysicalDeviceProperties *props = &gpu->props;
1209    const uint32_t apiVersion=props->apiVersion;
1210    const uint32_t major = VK_VERSION_MAJOR(apiVersion);
1211    const uint32_t minor = VK_VERSION_MINOR(apiVersion);
1212    const uint32_t patch = VK_VERSION_PATCH(apiVersion);
1213
1214    printf("VkPhysicalDeviceProperties:\n");
1215    printf("===========================\n");
1216    printf("\tapiVersion     = 0x%" PRIxLEAST32 "  (%d.%d.%d)\n", apiVersion, major, minor, patch);
1217    printf("\tdriverVersion  = %u (0x%" PRIxLEAST32 ")\n",props->driverVersion, props->driverVersion);
1218    printf("\tvendorID       = 0x%04x\n",                 props->vendorID);
1219    printf("\tdeviceID       = 0x%04x\n",                 props->deviceID);
1220    printf("\tdeviceType     = %s\n",                     vk_physical_device_type_string(props->deviceType));
1221    printf("\tdeviceName     = %s\n",                     props->deviceName);
1222
1223    app_dump_limits(&gpu->props.limits);
1224    app_dump_sparse_props(&gpu->props.sparseProperties);
1225
1226    fflush(stdout);
1227}
1228// clang-format on
1229
1230static void
1231app_dump_extensions(const char *indent, const char *layer_name,
1232                    const uint32_t extension_count,
1233                    const VkExtensionProperties *extension_properties) {
1234    uint32_t i;
1235    if (layer_name && (strlen(layer_name) > 0)) {
1236        printf("%s%s Extensions", indent, layer_name);
1237    } else {
1238        printf("%sExtensions", indent);
1239    }
1240    printf("\tcount = %d\n", extension_count);
1241    for (i = 0; i < extension_count; i++) {
1242        VkExtensionProperties const *ext_prop = &extension_properties[i];
1243
1244        printf("%s\t", indent);
1245        printf("%-36s: extension revision %2d\n", ext_prop->extensionName,
1246               ext_prop->specVersion);
1247    }
1248    fflush(stdout);
1249}
1250
1251// Returns true if the named extension is in the list of extensions.
1252static bool has_extension(const char *extension_name,
1253                          const uint32_t extension_count,
1254                          const VkExtensionProperties *extension_properties) {
1255    for (uint32_t i = 0; i < extension_count; i++) {
1256        if (!strcmp(extension_name, extension_properties[i].extensionName))
1257            return true;
1258    }
1259    return false;
1260}
1261
1262static void app_gpu_dump_queue_props(const struct app_gpu *gpu, uint32_t id) {
1263    const VkQueueFamilyProperties *props = &gpu->queue_props[id];
1264
1265    printf("VkQueueFamilyProperties[%d]:\n", id);
1266    printf("===========================\n");
1267    char *sep = ""; // separator character
1268    printf("\tqueueFlags         = ");
1269    if (props->queueFlags & VK_QUEUE_GRAPHICS_BIT) {
1270        printf("GRAPHICS");
1271        sep = " | ";
1272    }
1273    if (props->queueFlags & VK_QUEUE_COMPUTE_BIT) {
1274        printf("%sCOMPUTE", sep);
1275        sep = " | ";
1276    }
1277    if (props->queueFlags & VK_QUEUE_TRANSFER_BIT) {
1278        printf("%sTRANSFER", sep);
1279        sep = " | ";
1280    }
1281    if (props->queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) {
1282        printf("%sSPARSE", sep);
1283    }
1284    printf("\n");
1285
1286    printf("\tqueueCount         = %u\n", props->queueCount);
1287    printf("\ttimestampValidBits = %u\n", props->timestampValidBits);
1288    printf("\tminImageTransferGranularity = (%d, %d, %d)\n",
1289           props->minImageTransferGranularity.width,
1290           props->minImageTransferGranularity.height,
1291           props->minImageTransferGranularity.depth);
1292    fflush(stdout);
1293}
1294
1295static void app_gpu_dump_memory_props(const struct app_gpu *gpu) {
1296    const VkPhysicalDeviceMemoryProperties *props = &gpu->memory_props;
1297
1298    printf("VkPhysicalDeviceMemoryProperties:\n");
1299    printf("=================================\n");
1300    printf("\tmemoryTypeCount       = %u\n", props->memoryTypeCount);
1301    for (uint32_t i = 0; i < props->memoryTypeCount; i++) {
1302        printf("\tmemoryTypes[%u] : \n", i);
1303        printf("\t\theapIndex     = %u\n", props->memoryTypes[i].heapIndex);
1304        printf("\t\tpropertyFlags = 0x%" PRIxLEAST32 ":\n",
1305               props->memoryTypes[i].propertyFlags);
1306
1307        // Print each named flag, if it is set.
1308        VkFlags flags = props->memoryTypes[i].propertyFlags;
1309#define PRINT_FLAG(FLAG)                                                       \
1310    if (flags & FLAG)                                                          \
1311        printf("\t\t\t" #FLAG "\n");
1312        PRINT_FLAG(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
1313        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
1314        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1315        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
1316        PRINT_FLAG(VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT)
1317#undef PRINT_FLAG
1318    }
1319    printf("\n");
1320    printf("\tmemoryHeapCount       = %u\n", props->memoryHeapCount);
1321    for (uint32_t i = 0; i < props->memoryHeapCount; i++) {
1322        printf("\tmemoryHeaps[%u] : \n", i);
1323        const VkDeviceSize memSize = props->memoryHeaps[i].size;
1324        printf("\t\tsize          = " PRINTF_SIZE_T_SPECIFIER
1325               " (0x%" PRIxLEAST64 ")\n",
1326               (size_t)memSize, memSize);
1327
1328        VkMemoryHeapFlags heapFlags = props->memoryHeaps[i].flags;
1329        printf("\t\tflags: \n\t\t\t");
1330        printf((heapFlags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
1331                   ? "VK_MEMORY_HEAP_DEVICE_LOCAL_BIT\n"
1332                   : "None\n");
1333    }
1334    fflush(stdout);
1335}
1336
1337static void app_gpu_dump(const struct app_gpu *gpu) {
1338    uint32_t i;
1339
1340    printf("\nDevice Properties and Extensions :\n");
1341    printf("==================================\n");
1342    printf("GPU%u\n", gpu->id);
1343    app_gpu_dump_props(gpu);
1344    printf("\n");
1345    app_dump_extensions("", "Device", gpu->device_extension_count,
1346                        gpu->device_extensions);
1347    printf("\n");
1348    for (i = 0; i < gpu->queue_count; i++) {
1349        app_gpu_dump_queue_props(gpu, i);
1350        printf("\n");
1351    }
1352    app_gpu_dump_memory_props(gpu);
1353    printf("\n");
1354    app_gpu_dump_features(gpu);
1355    printf("\n");
1356    app_dev_dump(&gpu->dev);
1357}
1358
1359#ifdef _WIN32
1360// Enlarges the console window to have a large scrollback size.
1361static void ConsoleEnlarge() {
1362    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
1363
1364    // make the console window bigger
1365    CONSOLE_SCREEN_BUFFER_INFO csbi;
1366    COORD bufferSize;
1367    if (GetConsoleScreenBufferInfo(consoleHandle, &csbi)) {
1368        bufferSize.X = csbi.dwSize.X + 30;
1369        bufferSize.Y = 20000;
1370        SetConsoleScreenBufferSize(consoleHandle, bufferSize);
1371    }
1372
1373    SMALL_RECT r;
1374    r.Left = r.Top = 0;
1375    r.Right = csbi.dwSize.X - 1 + 30;
1376    r.Bottom = 50;
1377    SetConsoleWindowInfo(consoleHandle, true, &r);
1378
1379    // change the console window title
1380    SetConsoleTitle(TEXT(APP_SHORT_NAME));
1381}
1382#endif
1383
1384int main(int argc, char **argv) {
1385    unsigned int major, minor, patch;
1386    struct app_gpu gpus[MAX_GPUS];
1387    VkPhysicalDevice objs[MAX_GPUS];
1388    uint32_t gpu_count, i;
1389    VkResult err;
1390    struct app_instance inst;
1391
1392#ifdef _WIN32
1393    if (ConsoleIsExclusive())
1394        ConsoleEnlarge();
1395#endif
1396
1397    major = VK_VERSION_MAJOR(VK_API_VERSION_1_0);
1398    minor = VK_VERSION_MINOR(VK_API_VERSION_1_0);
1399    patch = VK_VERSION_PATCH(VK_HEADER_VERSION);
1400
1401    printf("===========\n");
1402    printf("VULKAN INFO\n");
1403    printf("===========\n\n");
1404    printf("Vulkan API Version: %d.%d.%d\n\n", major, minor, patch);
1405
1406    app_create_instance(&inst);
1407
1408    printf("\nInstance Extensions:\n");
1409    printf("====================\n");
1410    app_dump_extensions("", "Instance", inst.global_extension_count,
1411                        inst.global_extensions);
1412
1413    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, NULL);
1414    if (err)
1415        ERR_EXIT(err);
1416    if (gpu_count > MAX_GPUS) {
1417        printf("Too many GPUS found \n");
1418        ERR_EXIT(-1);
1419    }
1420    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, objs);
1421    if (err)
1422        ERR_EXIT(err);
1423
1424    for (i = 0; i < gpu_count; i++) {
1425        app_gpu_init(&gpus[i], i, objs[i]);
1426        printf("\n\n");
1427    }
1428
1429    //---Layer-Device-Extensions---
1430    printf("Layers: count = %d\n", inst.global_layer_count);
1431    printf("=======\n");
1432    for (uint32_t i = 0; i < inst.global_layer_count; i++) {
1433        uint32_t major, minor, patch;
1434        char spec_version[64], layer_version[64];
1435        VkLayerProperties const *layer_prop =
1436            &inst.global_layers[i].layer_properties;
1437
1438        extract_version(layer_prop->specVersion, &major, &minor, &patch);
1439        snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", major, minor,
1440                 patch);
1441        snprintf(layer_version, sizeof(layer_version), "%d",
1442                 layer_prop->implementationVersion);
1443        printf("%s (%s) Vulkan version %s, layer version %s\n",
1444               layer_prop->layerName, (char *)layer_prop->description,
1445               spec_version, layer_version);
1446
1447        app_dump_extensions("\t", "Layer",
1448                            inst.global_layers[i].extension_count,
1449                            inst.global_layers[i].extension_properties);
1450
1451        char *layerName = inst.global_layers[i].layer_properties.layerName;
1452        printf("\tDevices \tcount = %d\n", gpu_count);
1453        for (uint32_t j = 0; j < gpu_count; j++) {
1454            printf("\t\tGPU id       : %u (%s)\n", j, gpus[j].props.deviceName);
1455            uint32_t count = 0;
1456            VkExtensionProperties *props;
1457            app_get_physical_device_layer_extensions(&gpus[j], layerName,
1458                                                     &count, &props);
1459            app_dump_extensions("\t\t", "Layer-Device", count, props);
1460            free(props);
1461        }
1462        printf("\n");
1463    }
1464    fflush(stdout);
1465    //-----------------------------
1466
1467    printf("Presentable Surface formats:\n");
1468    printf("============================\n");
1469    inst.width = 256;
1470    inst.height = 256;
1471    int formatCount = 0;
1472
1473//--WIN32--
1474#ifdef VK_USE_PLATFORM_WIN32_KHR
1475    if (has_extension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
1476                      inst.global_extension_count, inst.global_extensions)) {
1477        app_create_win32_window(&inst);
1478        for (i = 0; i < gpu_count; i++) {
1479            app_create_win32_surface(&inst, &gpus[i]);
1480            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1481            printf("Surface type : %s\n", VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
1482            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1483            app_destroy_surface(&inst);
1484        }
1485        app_destroy_win32_window(&inst);
1486    }
1487#endif
1488//--XCB--
1489#ifdef VK_USE_PLATFORM_XCB_KHR
1490    if (has_extension(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
1491                      inst.global_extension_count, inst.global_extensions)) {
1492        app_create_xcb_window(&inst);
1493        for (i = 0; i < gpu_count; i++) {
1494            app_create_xcb_surface(&inst, &gpus[i]);
1495            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1496            printf("Surface type : %s\n", VK_KHR_XCB_SURFACE_EXTENSION_NAME);
1497            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1498            app_destroy_surface(&inst);
1499        }
1500        app_destroy_xcb_window(&inst);
1501    }
1502#endif
1503//--XLIB--
1504#ifdef VK_USE_PLATFORM_XLIB_KHR
1505    if (has_extension(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
1506                      inst.global_extension_count, inst.global_extensions)) {
1507        app_create_xlib_window(&inst);
1508        for (i = 0; i < gpu_count; i++) {
1509            app_create_xlib_surface(&inst, &gpus[i]);
1510            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1511            printf("Surface type : %s\n", VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
1512            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1513            app_destroy_surface(&inst);
1514        }
1515        app_destroy_xlib_window(&inst);
1516    }
1517#endif
1518    // TODO: Android / Wayland / MIR
1519    if (!formatCount)
1520        printf("None found\n");
1521    //---------
1522
1523    for (i = 0; i < gpu_count; i++) {
1524        app_gpu_dump(&gpus[i]);
1525        printf("\n\n");
1526    }
1527
1528    for (i = 0; i < gpu_count; i++)
1529        app_gpu_destroy(&gpus[i]);
1530
1531    app_destroy_instance(&inst);
1532
1533    fflush(stdout);
1534#ifdef _WIN32
1535    if (ConsoleIsExclusive())
1536        Sleep(INFINITE);
1537#endif
1538
1539    return 0;
1540}
1541