vulkaninfo.c revision ce165d8fd729f7ff9d2e12eac2865a6327073d1d
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) {
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
863    inst->xcb_screen = iter.data;
864    //-------------------
865
866    inst->xcb_window = xcb_generate_id(inst->xcb_connection);
867    xcb_create_window(inst->xcb_connection, XCB_COPY_FROM_PARENT, inst->xcb_window,
868                      inst->xcb_screen->root, 0, 0, inst->width, inst->height, 0,
869                      XCB_WINDOW_CLASS_INPUT_OUTPUT, inst->xcb_screen->root_visual,
870                      0, NULL);
871
872    xcb_intern_atom_cookie_t cookie = xcb_intern_atom(inst->xcb_connection, 1, 12, "WM_PROTOCOLS");
873    xcb_intern_atom_reply_t *reply =  xcb_intern_atom_reply(inst->xcb_connection, cookie, 0);
874    free(reply);
875}
876
877static void app_create_xcb_surface(struct app_instance *inst) {
878    VkResult U_ASSERT_ONLY err;
879    VkXcbSurfaceCreateInfoKHR xcb_createInfo;
880    xcb_createInfo.sType      = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
881    xcb_createInfo.pNext      = NULL;
882    xcb_createInfo.flags      = 0;
883    xcb_createInfo.connection = inst->xcb_connection;
884    xcb_createInfo.window     = inst->xcb_window;
885    err = vkCreateXcbSurfaceKHR(inst->instance, &xcb_createInfo, NULL, &inst->surface);
886    assert(!err);
887}
888
889static void app_destroy_xcb_window(struct app_instance *inst) {
890    xcb_destroy_window(inst->xcb_connection, inst->xcb_window);
891    xcb_disconnect(inst->xcb_connection);
892}
893#endif //VK_USE_PLATFORM_XCB_KHR
894//-----------------------------------------------------------
895
896//----------------------------XLib---------------------------
897#ifdef VK_USE_PLATFORM_XLIB_KHR
898static void app_create_xlib_window(struct app_instance *inst) {
899    inst->xlib_display = XOpenDisplay(NULL);
900    long visualMask = VisualScreenMask;
901    int numberOfVisuals;
902
903    XVisualInfo vInfoTemplate={};
904    vInfoTemplate.screen = DefaultScreen(inst->xlib_display);
905    XVisualInfo *visualInfo = XGetVisualInfo(inst->xlib_display, visualMask,
906                                             &vInfoTemplate, &numberOfVisuals);
907    inst->xlib_window = XCreateWindow(
908                inst->xlib_display, RootWindow(inst->xlib_display, vInfoTemplate.screen), 0, 0,
909                inst->width, inst->height, 0, visualInfo->depth, InputOutput,
910                visualInfo->visual, 0, NULL);
911
912    XSync(inst->xlib_display,false);
913}
914
915static void app_create_xlib_surface(struct app_instance *inst) {
916    VkResult U_ASSERT_ONLY err;
917    VkXlibSurfaceCreateInfoKHR createInfo;
918    createInfo.sType  = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
919    createInfo.pNext  = NULL;
920    createInfo.flags  = 0;
921    createInfo.dpy    = inst->xlib_display;
922    createInfo.window = inst->xlib_window;
923    err = vkCreateXlibSurfaceKHR(inst->instance, &createInfo, NULL, &inst->surface);
924    assert(!err);
925}
926
927static void app_destroy_xlib_window(struct app_instance *inst) {
928    XDestroyWindow(inst->xlib_display, inst->xlib_window);
929    XCloseDisplay(inst->xlib_display);
930}
931#endif //VK_USE_PLATFORM_XLIB_KHR
932//-----------------------------------------------------------
933
934static int app_dump_surface_formats(struct app_instance *inst, struct app_gpu *gpu){
935    // Get the list of VkFormat's that are supported:
936    VkResult U_ASSERT_ONLY err;
937    uint32_t formatCount = 0;
938    err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, NULL);
939    assert(!err);
940
941    VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
942    err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, surfFormats);
943    assert(!err);
944    printf("Format count = %d\n",formatCount);
945
946    for (uint32_t i = 0; i < formatCount; i++) {
947        printf("\t%s\n", vk_format_string(surfFormats[i].format));
948    }
949    printf("\n");
950    fflush(stdout);
951    return formatCount;
952}
953
954static void app_dev_dump_format_props(const struct app_dev *dev, VkFormat fmt)
955{
956    const VkFormatProperties *props = &dev->format_props[fmt];
957    struct {
958        const char *name;
959        VkFlags flags;
960    } features[3];
961
962    features[0].name  = "linearTiling   FormatFeatureFlags";
963    features[0].flags = props->linearTilingFeatures;
964    features[1].name  = "optimalTiling  FormatFeatureFlags";
965    features[1].flags = props->optimalTilingFeatures;
966    features[2].name  = "bufferFeatures FormatFeatureFlags";
967    features[2].flags = props->bufferFeatures;
968
969    printf("\nFORMAT_%s:", vk_format_string(fmt));
970    for (uint32_t i = 0; i < ARRAY_SIZE(features); i++) {
971        printf("\n\t%s:", features[i].name);
972        if (features[i].flags == 0) {
973            printf("\n\t\tNone");
974        } else {
975            printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
976               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT"                  : ""),  //0x0001
977               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_BIT"                  : ""),  //0x0002
978               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT"           : ""),  //0x0004
979               ((features[i].flags & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT"           : ""),  //0x0008
980               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT"           : ""),  //0x0010
981               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT"    : ""),  //0x0020
982               ((features[i].flags & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_VERTEX_BUFFER_BIT"                  : ""),  //0x0040
983               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)               ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT"               : ""),  //0x0080
984               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)         ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT"         : ""),  //0x0100
985               ((features[i].flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)       ? "\n\t\tVK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT"       : ""),  //0x0200
986               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_SRC_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_SRC_BIT"                       : ""),  //0x0400
987               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_DST_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_DST_BIT"                       : ""),  //0x0800
988               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT"    : ""),  //0x1000
989               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG) ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG" : "")); //0x2000
990        }
991    }
992    printf("\n");
993}
994
995
996static void
997app_dev_dump(const struct app_dev *dev)
998{
999    printf("Format Properties:\n");
1000    printf("==================");
1001    VkFormat fmt;
1002
1003    for (fmt = 0; fmt < VK_FORMAT_RANGE_SIZE; fmt++) {
1004        app_dev_dump_format_props(dev, fmt);
1005    }
1006}
1007
1008#ifdef _WIN32
1009#define PRINTF_SIZE_T_SPECIFIER    "%Iu"
1010#else
1011#define PRINTF_SIZE_T_SPECIFIER    "%zu"
1012#endif
1013
1014static void app_gpu_dump_features(const struct app_gpu *gpu)
1015{
1016    const VkPhysicalDeviceFeatures *features = &gpu->features;
1017
1018    printf("VkPhysicalDeviceFeatures:\n");
1019    printf("=========================\n");
1020
1021    printf("\trobustBufferAccess                      = %u\n", features->robustBufferAccess                     );
1022    printf("\tfullDrawIndexUint32                     = %u\n", features->fullDrawIndexUint32                    );
1023    printf("\timageCubeArray                          = %u\n", features->imageCubeArray                         );
1024    printf("\tindependentBlend                        = %u\n", features->independentBlend                       );
1025    printf("\tgeometryShader                          = %u\n", features->geometryShader                         );
1026    printf("\ttessellationShader                      = %u\n", features->tessellationShader                     );
1027    printf("\tsampleRateShading                       = %u\n", features->sampleRateShading                      );
1028    printf("\tdualSrcBlend                            = %u\n", features->dualSrcBlend                           );
1029    printf("\tlogicOp                                 = %u\n", features->logicOp                                );
1030    printf("\tmultiDrawIndirect                       = %u\n", features->multiDrawIndirect                      );
1031    printf("\tdrawIndirectFirstInstance               = %u\n", features->drawIndirectFirstInstance              );
1032    printf("\tdepthClamp                              = %u\n", features->depthClamp                             );
1033    printf("\tdepthBiasClamp                          = %u\n", features->depthBiasClamp                         );
1034    printf("\tfillModeNonSolid                        = %u\n", features->fillModeNonSolid                       );
1035    printf("\tdepthBounds                             = %u\n", features->depthBounds                            );
1036    printf("\twideLines                               = %u\n", features->wideLines                              );
1037    printf("\tlargePoints                             = %u\n", features->largePoints                            );
1038    printf("\ttextureCompressionETC2                  = %u\n", features->textureCompressionETC2                 );
1039    printf("\ttextureCompressionASTC_LDR              = %u\n", features->textureCompressionASTC_LDR             );
1040    printf("\ttextureCompressionBC                    = %u\n", features->textureCompressionBC                   );
1041    printf("\tocclusionQueryPrecise                   = %u\n", features->occlusionQueryPrecise                  );
1042    printf("\tpipelineStatisticsQuery                 = %u\n", features->pipelineStatisticsQuery                );
1043    printf("\tvertexSideEffects                       = %u\n", features->vertexPipelineStoresAndAtomics         );
1044    printf("\ttessellationSideEffects                 = %u\n", features->fragmentStoresAndAtomics               );
1045    printf("\tgeometrySideEffects                     = %u\n", features->shaderTessellationAndGeometryPointSize );
1046    printf("\tshaderImageGatherExtended               = %u\n", features->shaderImageGatherExtended              );
1047    printf("\tshaderStorageImageExtendedFormats       = %u\n", features->shaderStorageImageExtendedFormats      );
1048    printf("\tshaderStorageImageMultisample           = %u\n", features->shaderStorageImageMultisample          );
1049    printf("\tshaderStorageImageReadWithoutFormat     = %u\n", features->shaderStorageImageReadWithoutFormat    );
1050    printf("\tshaderStorageImageWriteWithoutFormat    = %u\n", features->shaderStorageImageWriteWithoutFormat   );
1051    printf("\tshaderUniformBufferArrayDynamicIndexing = %u\n", features->shaderUniformBufferArrayDynamicIndexing);
1052    printf("\tshaderSampledImageArrayDynamicIndexing  = %u\n", features->shaderSampledImageArrayDynamicIndexing );
1053    printf("\tshaderStorageBufferArrayDynamicIndexing = %u\n", features->shaderStorageBufferArrayDynamicIndexing);
1054    printf("\tshaderStorageImageArrayDynamicIndexing  = %u\n", features->shaderStorageImageArrayDynamicIndexing );
1055    printf("\tshaderClipDistance                      = %u\n", features->shaderClipDistance                     );
1056    printf("\tshaderCullDistance                      = %u\n", features->shaderCullDistance                     );
1057    printf("\tshaderFloat64                           = %u\n", features->shaderFloat64                          );
1058    printf("\tshaderInt64                             = %u\n", features->shaderInt64                            );
1059    printf("\tshaderInt16                             = %u\n", features->shaderInt16                            );
1060    printf("\tshaderResourceResidency                 = %u\n", features->shaderResourceResidency                );
1061    printf("\tshaderResourceMinLod                    = %u\n", features->shaderResourceMinLod                   );
1062    printf("\talphaToOne                              = %u\n", features->alphaToOne                             );
1063    printf("\tsparseBinding                           = %u\n", features->sparseBinding                          );
1064    printf("\tsparseResidencyBuffer                   = %u\n", features->sparseResidencyBuffer                  );
1065    printf("\tsparseResidencyImage2D                  = %u\n", features->sparseResidencyImage2D                 );
1066    printf("\tsparseResidencyImage3D                  = %u\n", features->sparseResidencyImage3D                 );
1067    printf("\tsparseResidency2Samples                 = %u\n", features->sparseResidency2Samples                );
1068    printf("\tsparseResidency4Samples                 = %u\n", features->sparseResidency4Samples                );
1069    printf("\tsparseResidency8Samples                 = %u\n", features->sparseResidency8Samples                );
1070    printf("\tsparseResidency16Samples                = %u\n", features->sparseResidency16Samples               );
1071    printf("\tsparseResidencyAliased                  = %u\n", features->sparseResidencyAliased                 );
1072    printf("\tvariableMultisampleRate                 = %u\n", features->variableMultisampleRate                );
1073    printf("\tiheritedQueries                         = %u\n", features->inheritedQueries                       );
1074}
1075
1076static void app_dump_sparse_props(const VkPhysicalDeviceSparseProperties *sparseProps)
1077{
1078
1079    printf("\tVkPhysicalDeviceSparseProperties:\n");
1080    printf("\t---------------------------------\n");
1081
1082    printf("\t\tresidencyStandard2DBlockShape            = %u\n", sparseProps->residencyStandard2DBlockShape           );
1083    printf("\t\tresidencyStandard2DMultisampleBlockShape = %u\n", sparseProps->residencyStandard2DMultisampleBlockShape);
1084    printf("\t\tresidencyStandard3DBlockShape            = %u\n", sparseProps->residencyStandard3DBlockShape           );
1085    printf("\t\tresidencyAlignedMipSize                  = %u\n", sparseProps->residencyAlignedMipSize                 );
1086    printf("\t\tresidencyNonResidentStrict               = %u\n", sparseProps->residencyNonResidentStrict              );
1087}
1088
1089static void app_dump_limits(const VkPhysicalDeviceLimits *limits)
1090{
1091    printf("\tVkPhysicalDeviceLimits:\n");
1092    printf("\t-----------------------\n");
1093    printf("\t\tmaxImageDimension1D                     = %u\n",                 limits->maxImageDimension1D                    );
1094    printf("\t\tmaxImageDimension2D                     = %u\n",                 limits->maxImageDimension2D                    );
1095    printf("\t\tmaxImageDimension3D                     = %u\n",                 limits->maxImageDimension3D                    );
1096    printf("\t\tmaxImageDimensionCube                   = %u\n",                 limits->maxImageDimensionCube                  );
1097    printf("\t\tmaxImageArrayLayers                     = %u\n",                 limits->maxImageArrayLayers                    );
1098    printf("\t\tmaxTexelBufferElements                  = 0x%" PRIxLEAST32 "\n", limits->maxTexelBufferElements                 );
1099    printf("\t\tmaxUniformBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxUniformBufferRange                  );
1100    printf("\t\tmaxStorageBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxStorageBufferRange                  );
1101    printf("\t\tmaxPushConstantsSize                    = %u\n",                 limits->maxPushConstantsSize                   );
1102    printf("\t\tmaxMemoryAllocationCount                = %u\n",                 limits->maxMemoryAllocationCount               );
1103    printf("\t\tmaxSamplerAllocationCount               = %u\n",                 limits->maxSamplerAllocationCount              );
1104    printf("\t\tbufferImageGranularity                  = 0x%" PRIxLEAST64 "\n", limits->bufferImageGranularity                 );
1105    printf("\t\tsparseAddressSpaceSize                  = 0x%" PRIxLEAST64 "\n", limits->sparseAddressSpaceSize                 );
1106    printf("\t\tmaxBoundDescriptorSets                  = %u\n",                 limits->maxBoundDescriptorSets                 );
1107    printf("\t\tmaxPerStageDescriptorSamplers           = %u\n",                 limits->maxPerStageDescriptorSamplers          );
1108    printf("\t\tmaxPerStageDescriptorUniformBuffers     = %u\n",                 limits->maxPerStageDescriptorUniformBuffers    );
1109    printf("\t\tmaxPerStageDescriptorStorageBuffers     = %u\n",                 limits->maxPerStageDescriptorStorageBuffers    );
1110    printf("\t\tmaxPerStageDescriptorSampledImages      = %u\n",                 limits->maxPerStageDescriptorSampledImages     );
1111    printf("\t\tmaxPerStageDescriptorStorageImages      = %u\n",                 limits->maxPerStageDescriptorStorageImages     );
1112    printf("\t\tmaxPerStageDescriptorInputAttachments   = %u\n",                 limits->maxPerStageDescriptorInputAttachments  );
1113    printf("\t\tmaxPerStageResources                    = %u\n",                 limits->maxPerStageResources                   );
1114    printf("\t\tmaxDescriptorSetSamplers                = %u\n",                 limits->maxDescriptorSetSamplers               );
1115    printf("\t\tmaxDescriptorSetUniformBuffers          = %u\n",                 limits->maxDescriptorSetUniformBuffers         );
1116    printf("\t\tmaxDescriptorSetUniformBuffersDynamic   = %u\n",                 limits->maxDescriptorSetUniformBuffersDynamic  );
1117    printf("\t\tmaxDescriptorSetStorageBuffers          = %u\n",                 limits->maxDescriptorSetStorageBuffers         );
1118    printf("\t\tmaxDescriptorSetStorageBuffersDynamic   = %u\n",                 limits->maxDescriptorSetStorageBuffersDynamic  );
1119    printf("\t\tmaxDescriptorSetSampledImages           = %u\n",                 limits->maxDescriptorSetSampledImages          );
1120    printf("\t\tmaxDescriptorSetStorageImages           = %u\n",                 limits->maxDescriptorSetStorageImages          );
1121    printf("\t\tmaxDescriptorSetInputAttachments        = %u\n",                 limits->maxDescriptorSetInputAttachments       );
1122    printf("\t\tmaxVertexInputAttributes                = %u\n",                 limits->maxVertexInputAttributes               );
1123    printf("\t\tmaxVertexInputBindings                  = %u\n",                 limits->maxVertexInputBindings                 );
1124    printf("\t\tmaxVertexInputAttributeOffset           = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputAttributeOffset          );
1125    printf("\t\tmaxVertexInputBindingStride             = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputBindingStride            );
1126    printf("\t\tmaxVertexOutputComponents               = %u\n",                 limits->maxVertexOutputComponents              );
1127    printf("\t\tmaxTessellationGenerationLevel          = %u\n",                 limits->maxTessellationGenerationLevel         );
1128    printf("\t\tmaxTessellationPatchSize                        = %u\n",                 limits->maxTessellationPatchSize                       );
1129    printf("\t\tmaxTessellationControlPerVertexInputComponents  = %u\n",                 limits->maxTessellationControlPerVertexInputComponents );
1130    printf("\t\tmaxTessellationControlPerVertexOutputComponents = %u\n",                 limits->maxTessellationControlPerVertexOutputComponents);
1131    printf("\t\tmaxTessellationControlPerPatchOutputComponents  = %u\n",                 limits->maxTessellationControlPerPatchOutputComponents );
1132    printf("\t\tmaxTessellationControlTotalOutputComponents     = %u\n",                 limits->maxTessellationControlTotalOutputComponents    );
1133    printf("\t\tmaxTessellationEvaluationInputComponents        = %u\n",                 limits->maxTessellationEvaluationInputComponents       );
1134    printf("\t\tmaxTessellationEvaluationOutputComponents       = %u\n",                 limits->maxTessellationEvaluationOutputComponents      );
1135    printf("\t\tmaxGeometryShaderInvocations            = %u\n",                 limits->maxGeometryShaderInvocations           );
1136    printf("\t\tmaxGeometryInputComponents              = %u\n",                 limits->maxGeometryInputComponents             );
1137    printf("\t\tmaxGeometryOutputComponents             = %u\n",                 limits->maxGeometryOutputComponents            );
1138    printf("\t\tmaxGeometryOutputVertices               = %u\n",                 limits->maxGeometryOutputVertices              );
1139    printf("\t\tmaxGeometryTotalOutputComponents        = %u\n",                 limits->maxGeometryTotalOutputComponents       );
1140    printf("\t\tmaxFragmentInputComponents              = %u\n",                 limits->maxFragmentInputComponents             );
1141    printf("\t\tmaxFragmentOutputAttachments            = %u\n",                 limits->maxFragmentOutputAttachments           );
1142    printf("\t\tmaxFragmentDualSrcAttachments           = %u\n",                 limits->maxFragmentDualSrcAttachments          );
1143    printf("\t\tmaxFragmentCombinedOutputResources      = %u\n",                 limits->maxFragmentCombinedOutputResources     );
1144    printf("\t\tmaxComputeSharedMemorySize              = 0x%" PRIxLEAST32 "\n", limits->maxComputeSharedMemorySize             );
1145    printf("\t\tmaxComputeWorkGroupCount[0]             = %u\n",                 limits->maxComputeWorkGroupCount[0]            );
1146    printf("\t\tmaxComputeWorkGroupCount[1]             = %u\n",                 limits->maxComputeWorkGroupCount[1]            );
1147    printf("\t\tmaxComputeWorkGroupCount[2]             = %u\n",                 limits->maxComputeWorkGroupCount[2]            );
1148    printf("\t\tmaxComputeWorkGroupInvocations          = %u\n",                 limits->maxComputeWorkGroupInvocations         );
1149    printf("\t\tmaxComputeWorkGroupSize[0]              = %u\n",                 limits->maxComputeWorkGroupSize[0]             );
1150    printf("\t\tmaxComputeWorkGroupSize[1]              = %u\n",                 limits->maxComputeWorkGroupSize[1]             );
1151    printf("\t\tmaxComputeWorkGroupSize[2]              = %u\n",                 limits->maxComputeWorkGroupSize[2]             );
1152    printf("\t\tsubPixelPrecisionBits                   = %u\n",                 limits->subPixelPrecisionBits                  );
1153    printf("\t\tsubTexelPrecisionBits                   = %u\n",                 limits->subTexelPrecisionBits                  );
1154    printf("\t\tmipmapPrecisionBits                     = %u\n",                 limits->mipmapPrecisionBits                    );
1155    printf("\t\tmaxDrawIndexedIndexValue                = %u\n",                 limits->maxDrawIndexedIndexValue               );
1156    printf("\t\tmaxDrawIndirectCount                    = %u\n",                 limits->maxDrawIndirectCount                   );
1157    printf("\t\tmaxSamplerLodBias                       = %f\n",                 limits->maxSamplerLodBias                      );
1158    printf("\t\tmaxSamplerAnisotropy                    = %f\n",                 limits->maxSamplerAnisotropy                   );
1159    printf("\t\tmaxViewports                            = %u\n",                 limits->maxViewports                           );
1160    printf("\t\tmaxViewportDimensions[0]                = %u\n",                 limits->maxViewportDimensions[0]               );
1161    printf("\t\tmaxViewportDimensions[1]                = %u\n",                 limits->maxViewportDimensions[1]               );
1162    printf("\t\tviewportBoundsRange[0]                  =%13f\n",                 limits->viewportBoundsRange[0]                 );
1163    printf("\t\tviewportBoundsRange[1]                  =%13f\n",                 limits->viewportBoundsRange[1]                 );
1164    printf("\t\tviewportSubPixelBits                    = %u\n",                 limits->viewportSubPixelBits                   );
1165    printf("\t\tminMemoryMapAlignment                   = " PRINTF_SIZE_T_SPECIFIER "\n", limits->minMemoryMapAlignment         );
1166    printf("\t\tminTexelBufferOffsetAlignment           = 0x%" PRIxLEAST64 "\n", limits->minTexelBufferOffsetAlignment          );
1167    printf("\t\tminUniformBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minUniformBufferOffsetAlignment        );
1168    printf("\t\tminStorageBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minStorageBufferOffsetAlignment        );
1169    printf("\t\tminTexelOffset                          =%3d\n",                 limits->minTexelOffset                         );
1170    printf("\t\tmaxTexelOffset                          =%3d\n",                 limits->maxTexelOffset                         );
1171    printf("\t\tminTexelGatherOffset                    =%3d\n",                 limits->minTexelGatherOffset                   );
1172    printf("\t\tmaxTexelGatherOffset                    =%3d\n",                 limits->maxTexelGatherOffset                   );
1173    printf("\t\tminInterpolationOffset                  =%9f\n",                 limits->minInterpolationOffset                 );
1174    printf("\t\tmaxInterpolationOffset                  =%9f\n",                 limits->maxInterpolationOffset                 );
1175    printf("\t\tsubPixelInterpolationOffsetBits         = %u\n",                 limits->subPixelInterpolationOffsetBits        );
1176    printf("\t\tmaxFramebufferWidth                     = %u\n",                 limits->maxFramebufferWidth                    );
1177    printf("\t\tmaxFramebufferHeight                    = %u\n",                 limits->maxFramebufferHeight                   );
1178    printf("\t\tmaxFramebufferLayers                    = %u\n",                 limits->maxFramebufferLayers                   );
1179    printf("\t\tframebufferColorSampleCounts            = %u\n",                 limits->framebufferColorSampleCounts           );
1180    printf("\t\tframebufferDepthSampleCounts            = %u\n",                 limits->framebufferDepthSampleCounts           );
1181    printf("\t\tframebufferStencilSampleCounts          = %u\n",                 limits->framebufferStencilSampleCounts         );
1182    printf("\t\tframebufferNoAttachmentsSampleCounts    = %u\n",                 limits->framebufferNoAttachmentsSampleCounts   );
1183    printf("\t\tmaxColorAttachments                     = %u\n",                 limits->maxColorAttachments                    );
1184    printf("\t\tsampledImageColorSampleCounts           = %u\n",                 limits->sampledImageColorSampleCounts          );
1185    printf("\t\tsampledImageDepthSampleCounts           = %u\n",                 limits->sampledImageDepthSampleCounts          );
1186    printf("\t\tsampledImageStencilSampleCounts         = %u\n",                 limits->sampledImageStencilSampleCounts        );
1187    printf("\t\tsampledImageIntegerSampleCounts         = %u\n",                 limits->sampledImageIntegerSampleCounts        );
1188    printf("\t\tstorageImageSampleCounts                = %u\n",                 limits->storageImageSampleCounts               );
1189    printf("\t\tmaxSampleMaskWords                      = %u\n",                 limits->maxSampleMaskWords                     );
1190    printf("\t\ttimestampComputeAndGraphics             = %u\n",                 limits->timestampComputeAndGraphics            );
1191    printf("\t\ttimestampPeriod                         = %f\n",                 limits->timestampPeriod                        );
1192    printf("\t\tmaxClipDistances                        = %u\n",                 limits->maxClipDistances                       );
1193    printf("\t\tmaxCullDistances                        = %u\n",                 limits->maxCullDistances                       );
1194    printf("\t\tmaxCombinedClipAndCullDistances         = %u\n",                 limits->maxCombinedClipAndCullDistances        );
1195    printf("\t\tdiscreteQueuePriorities                 = %u\n",                 limits->discreteQueuePriorities                );
1196    printf("\t\tpointSizeRange[0]                       = %f\n",                 limits->pointSizeRange[0]                      );
1197    printf("\t\tpointSizeRange[1]                       = %f\n",                 limits->pointSizeRange[1]                      );
1198    printf("\t\tlineWidthRange[0]                       = %f\n",                 limits->lineWidthRange[0]                      );
1199    printf("\t\tlineWidthRange[1]                       = %f\n",                 limits->lineWidthRange[1]                      );
1200    printf("\t\tpointSizeGranularity                    = %f\n",                 limits->pointSizeGranularity                   );
1201    printf("\t\tlineWidthGranularity                    = %f\n",                 limits->lineWidthGranularity                   );
1202    printf("\t\tstrictLines                             = %u\n",                 limits->strictLines                            );
1203    printf("\t\tstandardSampleLocations                 = %u\n",                 limits->standardSampleLocations                );
1204    printf("\t\toptimalBufferCopyOffsetAlignment        = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyOffsetAlignment       );
1205    printf("\t\toptimalBufferCopyRowPitchAlignment      = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyRowPitchAlignment     );
1206    printf("\t\tnonCoherentAtomSize                     = 0x%" PRIxLEAST64 "\n", limits->nonCoherentAtomSize                    );
1207}
1208
1209static void app_gpu_dump_props(const struct app_gpu *gpu)
1210{
1211    const VkPhysicalDeviceProperties *props = &gpu->props;
1212    const uint32_t apiVersion=props->apiVersion;
1213    const uint32_t major = VK_VERSION_MAJOR(apiVersion);
1214    const uint32_t minor = VK_VERSION_MINOR(apiVersion);
1215    const uint32_t patch = VK_VERSION_PATCH(apiVersion);
1216
1217    printf("VkPhysicalDeviceProperties:\n");
1218    printf("===========================\n");
1219    printf("\tapiVersion     = 0x%" PRIxLEAST32 "  (%d.%d.%d)\n", apiVersion, major, minor, patch);
1220    printf("\tdriverVersion  = %u (0x%" PRIxLEAST32 ")\n",props->driverVersion, props->driverVersion);
1221    printf("\tvendorID       = 0x%04x\n",                 props->vendorID);
1222    printf("\tdeviceID       = 0x%04x\n",                 props->deviceID);
1223    printf("\tdeviceType     = %s\n",                     vk_physical_device_type_string(props->deviceType));
1224    printf("\tdeviceName     = %s\n",                     props->deviceName);
1225
1226    app_dump_limits(&gpu->props.limits);
1227    app_dump_sparse_props(&gpu->props.sparseProperties);
1228
1229    fflush(stdout);
1230}
1231// clang-format on
1232
1233static void
1234app_dump_extensions(const char *indent, const char *layer_name,
1235                    const uint32_t extension_count,
1236                    const VkExtensionProperties *extension_properties) {
1237    uint32_t i;
1238    if (layer_name && (strlen(layer_name) > 0)) {
1239        printf("%s%s Extensions", indent, layer_name);
1240    } else {
1241        printf("%sExtensions", indent);
1242    }
1243    printf("\tcount = %d\n", extension_count);
1244    for (i = 0; i < extension_count; i++) {
1245        VkExtensionProperties const *ext_prop = &extension_properties[i];
1246
1247        printf("%s\t", indent);
1248        printf("%-36s: extension revision %2d\n", ext_prop->extensionName,
1249               ext_prop->specVersion);
1250    }
1251    fflush(stdout);
1252}
1253
1254// Returns true if the named extension is in the list of extensions.
1255static bool has_extension(const char *extension_name,
1256                          const uint32_t extension_count,
1257                          const VkExtensionProperties *extension_properties) {
1258    for (uint32_t i = 0; i < extension_count; i++) {
1259        if (!strcmp(extension_name, extension_properties[i].extensionName))
1260            return true;
1261    }
1262    return false;
1263}
1264
1265static void app_gpu_dump_queue_props(const struct app_gpu *gpu, uint32_t id) {
1266    const VkQueueFamilyProperties *props = &gpu->queue_props[id];
1267
1268    printf("VkQueueFamilyProperties[%d]:\n", id);
1269    printf("===========================\n");
1270    char *sep = ""; // separator character
1271    printf("\tqueueFlags         = ");
1272    if (props->queueFlags & VK_QUEUE_GRAPHICS_BIT) {
1273        printf("GRAPHICS");
1274        sep = " | ";
1275    }
1276    if (props->queueFlags & VK_QUEUE_COMPUTE_BIT) {
1277        printf("%sCOMPUTE", sep);
1278        sep = " | ";
1279    }
1280    if (props->queueFlags & VK_QUEUE_TRANSFER_BIT) {
1281        printf("%sTRANSFER", sep);
1282        sep = " | ";
1283    }
1284    if (props->queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) {
1285        printf("%sSPARSE", sep);
1286    }
1287    printf("\n");
1288
1289    printf("\tqueueCount         = %u\n", props->queueCount);
1290    printf("\ttimestampValidBits = %u\n", props->timestampValidBits);
1291    printf("\tminImageTransferGranularity = (%d, %d, %d)\n",
1292           props->minImageTransferGranularity.width,
1293           props->minImageTransferGranularity.height,
1294           props->minImageTransferGranularity.depth);
1295    fflush(stdout);
1296}
1297
1298static void app_gpu_dump_memory_props(const struct app_gpu *gpu) {
1299    const VkPhysicalDeviceMemoryProperties *props = &gpu->memory_props;
1300
1301    printf("VkPhysicalDeviceMemoryProperties:\n");
1302    printf("=================================\n");
1303    printf("\tmemoryTypeCount       = %u\n", props->memoryTypeCount);
1304    for (uint32_t i = 0; i < props->memoryTypeCount; i++) {
1305        printf("\tmemoryTypes[%u] : \n", i);
1306        printf("\t\theapIndex     = %u\n", props->memoryTypes[i].heapIndex);
1307        printf("\t\tpropertyFlags = 0x%" PRIxLEAST32 ":\n",
1308               props->memoryTypes[i].propertyFlags);
1309
1310        // Print each named flag, if it is set.
1311        VkFlags flags = props->memoryTypes[i].propertyFlags;
1312#define PRINT_FLAG(FLAG)                                                       \
1313    if (flags & FLAG)                                                          \
1314        printf("\t\t\t" #FLAG "\n");
1315        PRINT_FLAG(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
1316        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
1317        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1318        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
1319        PRINT_FLAG(VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT)
1320#undef PRINT_FLAG
1321    }
1322    printf("\n");
1323    printf("\tmemoryHeapCount       = %u\n", props->memoryHeapCount);
1324    for (uint32_t i = 0; i < props->memoryHeapCount; i++) {
1325        printf("\tmemoryHeaps[%u] : \n", i);
1326        const VkDeviceSize memSize = props->memoryHeaps[i].size;
1327        printf("\t\tsize          = " PRINTF_SIZE_T_SPECIFIER
1328               " (0x%" PRIxLEAST64 ")\n",
1329               (size_t)memSize, memSize);
1330
1331        VkMemoryHeapFlags heapFlags = props->memoryHeaps[i].flags;
1332        printf("\t\tflags: \n\t\t\t");
1333        printf((heapFlags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
1334                   ? "VK_MEMORY_HEAP_DEVICE_LOCAL_BIT\n"
1335                   : "None\n");
1336    }
1337    fflush(stdout);
1338}
1339
1340static void app_gpu_dump(const struct app_gpu *gpu) {
1341    uint32_t i;
1342
1343    printf("\nDevice Properties and Extensions :\n");
1344    printf("==================================\n");
1345    printf("GPU%u\n", gpu->id);
1346    app_gpu_dump_props(gpu);
1347    printf("\n");
1348    app_dump_extensions("", "Device", gpu->device_extension_count,
1349                        gpu->device_extensions);
1350    printf("\n");
1351    for (i = 0; i < gpu->queue_count; i++) {
1352        app_gpu_dump_queue_props(gpu, i);
1353        printf("\n");
1354    }
1355    app_gpu_dump_memory_props(gpu);
1356    printf("\n");
1357    app_gpu_dump_features(gpu);
1358    printf("\n");
1359    app_dev_dump(&gpu->dev);
1360}
1361
1362#ifdef _WIN32
1363// Enlarges the console window to have a large scrollback size.
1364static void ConsoleEnlarge() {
1365    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
1366
1367    // make the console window bigger
1368    CONSOLE_SCREEN_BUFFER_INFO csbi;
1369    COORD bufferSize;
1370    if (GetConsoleScreenBufferInfo(consoleHandle, &csbi)) {
1371        bufferSize.X = csbi.dwSize.X + 30;
1372        bufferSize.Y = 20000;
1373        SetConsoleScreenBufferSize(consoleHandle, bufferSize);
1374    }
1375
1376    SMALL_RECT r;
1377    r.Left = r.Top = 0;
1378    r.Right = csbi.dwSize.X - 1 + 30;
1379    r.Bottom = 50;
1380    SetConsoleWindowInfo(consoleHandle, true, &r);
1381
1382    // change the console window title
1383    SetConsoleTitle(TEXT(APP_SHORT_NAME));
1384}
1385#endif
1386
1387int main(int argc, char **argv) {
1388    unsigned int major, minor, patch;
1389    struct app_gpu gpus[MAX_GPUS];
1390    VkPhysicalDevice objs[MAX_GPUS];
1391    uint32_t gpu_count, i;
1392    VkResult err;
1393    struct app_instance inst;
1394
1395#ifdef _WIN32
1396    if (ConsoleIsExclusive())
1397        ConsoleEnlarge();
1398#endif
1399
1400    major = VK_VERSION_MAJOR(VK_API_VERSION_1_0);
1401    minor = VK_VERSION_MINOR(VK_API_VERSION_1_0);
1402    patch = VK_VERSION_PATCH(VK_HEADER_VERSION);
1403
1404    printf("===========\n");
1405    printf("VULKAN INFO\n");
1406    printf("===========\n\n");
1407    printf("Vulkan API Version: %d.%d.%d\n\n", major, minor, patch);
1408
1409    app_create_instance(&inst);
1410
1411    printf("\nInstance Extensions:\n");
1412    printf("====================\n");
1413    app_dump_extensions("", "Instance", inst.global_extension_count,
1414                        inst.global_extensions);
1415
1416    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, NULL);
1417    if (err)
1418        ERR_EXIT(err);
1419    if (gpu_count > MAX_GPUS) {
1420        printf("Too many GPUS found \n");
1421        ERR_EXIT(-1);
1422    }
1423    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, objs);
1424    if (err)
1425        ERR_EXIT(err);
1426
1427    for (i = 0; i < gpu_count; i++) {
1428        app_gpu_init(&gpus[i], i, objs[i]);
1429        printf("\n\n");
1430    }
1431
1432    //---Layer-Device-Extensions---
1433    printf("Layers: count = %d\n", inst.global_layer_count);
1434    printf("=======\n");
1435    for (uint32_t i = 0; i < inst.global_layer_count; i++) {
1436        uint32_t major, minor, patch;
1437        char spec_version[64], layer_version[64];
1438        VkLayerProperties const *layer_prop =
1439            &inst.global_layers[i].layer_properties;
1440
1441        extract_version(layer_prop->specVersion, &major, &minor, &patch);
1442        snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", major, minor,
1443                 patch);
1444        snprintf(layer_version, sizeof(layer_version), "%d",
1445                 layer_prop->implementationVersion);
1446        printf("%s (%s) Vulkan version %s, layer version %s\n",
1447               layer_prop->layerName, (char *)layer_prop->description,
1448               spec_version, layer_version);
1449
1450        app_dump_extensions("\t", "Layer",
1451                            inst.global_layers[i].extension_count,
1452                            inst.global_layers[i].extension_properties);
1453
1454        char *layerName = inst.global_layers[i].layer_properties.layerName;
1455        printf("\tDevices \tcount = %d\n", gpu_count);
1456        for (uint32_t j = 0; j < gpu_count; j++) {
1457            printf("\t\tGPU id       : %u (%s)\n", j, gpus[j].props.deviceName);
1458            uint32_t count = 0;
1459            VkExtensionProperties *props;
1460            app_get_physical_device_layer_extensions(&gpus[j], layerName,
1461                                                     &count, &props);
1462            app_dump_extensions("\t\t", "Layer-Device", count, props);
1463            free(props);
1464        }
1465        printf("\n");
1466    }
1467    fflush(stdout);
1468    //-----------------------------
1469
1470    printf("Presentable Surface formats:\n");
1471    printf("============================\n");
1472    inst.width = 256;
1473    inst.height = 256;
1474    int formatCount = 0;
1475
1476//--WIN32--
1477#ifdef VK_USE_PLATFORM_WIN32_KHR
1478    if (has_extension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
1479                      inst.global_extension_count, inst.global_extensions)) {
1480        app_create_win32_window(&inst);
1481        for (i = 0; i < gpu_count; i++) {
1482            app_create_win32_surface(&inst);
1483            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1484            printf("Surface type : %s\n", VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
1485            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1486            app_destroy_surface(&inst);
1487        }
1488        app_destroy_win32_window(&inst);
1489    }
1490#endif
1491//--XCB--
1492#ifdef VK_USE_PLATFORM_XCB_KHR
1493    if (has_extension(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
1494                      inst.global_extension_count, inst.global_extensions)) {
1495        app_create_xcb_window(&inst);
1496        for (i = 0; i < gpu_count; i++) {
1497            app_create_xcb_surface(&inst);
1498            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1499            printf("Surface type : %s\n", VK_KHR_XCB_SURFACE_EXTENSION_NAME);
1500            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1501            app_destroy_surface(&inst);
1502        }
1503        app_destroy_xcb_window(&inst);
1504    }
1505#endif
1506//--XLIB--
1507#ifdef VK_USE_PLATFORM_XLIB_KHR
1508    if (has_extension(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
1509                      inst.global_extension_count, inst.global_extensions)) {
1510        app_create_xlib_window(&inst);
1511        for (i = 0; i < gpu_count; i++) {
1512            app_create_xlib_surface(&inst);
1513            printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1514            printf("Surface type : %s\n", VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
1515            formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1516            app_destroy_surface(&inst);
1517        }
1518        app_destroy_xlib_window(&inst);
1519    }
1520#endif
1521    // TODO: Android / Wayland / MIR
1522    if (!formatCount)
1523        printf("None found\n");
1524    //---------
1525
1526    for (i = 0; i < gpu_count; i++) {
1527        app_gpu_dump(&gpus[i]);
1528        printf("\n\n");
1529    }
1530
1531    for (i = 0; i < gpu_count; i++)
1532        app_gpu_destroy(&gpus[i]);
1533
1534    app_destroy_instance(&inst);
1535
1536    fflush(stdout);
1537#ifdef _WIN32
1538    if (ConsoleIsExclusive())
1539        Sleep(INFINITE);
1540#endif
1541
1542    return 0;
1543}
1544