vulkaninfo.c revision 785034443420fe897a68fa78f2a72edb1a91dd53
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 paramteter
588    app_get_global_layer_extensions(NULL, &inst->global_extension_count,
589                                    &inst->global_extensions);
590}
591
592static void app_create_instance(struct app_instance *inst) {
593    app_get_instance_extensions(inst);
594
595//---Build a list of extensions to load---
596#define MAX_EXTENSIONS 4
597    uint32_t i = 0;
598    uint32_t ext_count = 0;
599    const char *ext_names[MAX_EXTENSIONS]; // array of string pointers to
600                                           // extension names
601    for (i = 0; (i < inst->global_extension_count); i++) {
602        const char *found_name = inst->global_extensions[i].extensionName;
603        if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, found_name))
604            {ext_names[ext_count++] = VK_KHR_SURFACE_EXTENSION_NAME;}
605    }
606
607    if (ext_count)
608        for (i = 0; ((i < inst->global_extension_count) &&
609                     (ext_count < MAX_EXTENSIONS));
610             i++) {
611            const char *found_name = inst->global_extensions[i].extensionName;
612#ifdef VK_USE_PLATFORM_WIN32_KHR
613            if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, found_name))
614                {ext_names[ext_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;}
615#endif
616#ifdef VK_USE_PLATFORM_XCB_KHR
617            if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, found_name))
618                {ext_names[ext_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;}
619#endif
620#ifdef VK_USE_PLATFORM_XLIB_KHR
621            if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, found_name))
622                {ext_names[ext_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;}
623#endif
624#ifdef VK_USE_PLATFORM_ANDROID_KHR
625            if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, found_name))
626                {ext_names[ext_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;}
627#endif
628        }
629    // If we don't find the KHR_SURFACE extension and at least one other
630    // device-specific extension,
631    // then give up on reporting presentable surface formats."
632    if (ext_count < 2)
633        ext_count = 0;
634    //----------------------------------------
635
636    const VkApplicationInfo app_info = {
637        .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
638        .pNext = NULL,
639        .pApplicationName = APP_SHORT_NAME,
640        .applicationVersion = 1,
641        .pEngineName = APP_SHORT_NAME,
642        .engineVersion = 1,
643        .apiVersion = VK_API_VERSION_1_0,
644    };
645
646    VkInstanceCreateInfo inst_info = {
647        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
648        .pNext = NULL,
649        .pApplicationInfo = &app_info,
650        .enabledLayerCount = 0,
651        .ppEnabledLayerNames = NULL,
652        .enabledExtensionCount = ext_count,
653        .ppEnabledExtensionNames = ext_names,
654    };
655
656    VkDebugReportCallbackCreateInfoEXT dbg_info;
657    memset(&dbg_info, 0, sizeof(dbg_info));
658    dbg_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
659    dbg_info.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
660                     VK_DEBUG_REPORT_WARNING_BIT_EXT |
661                     VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
662    dbg_info.pfnCallback = dbg_callback;
663    inst_info.pNext = &dbg_info;
664
665    VkResult U_ASSERT_ONLY err;
666    err = vkCreateInstance(&inst_info, NULL, &inst->instance);
667    if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
668        printf("Cannot create Vulkan instance.\n");
669        ERR_EXIT(err);
670    } else if (err) {
671        ERR_EXIT(err);
672    }
673
674    if (ext_count > 0) {
675//--Load Extensions--
676#define GET_INSTANCE_PROC_ADDR(ENTRYPOINT)                                     \
677    {                                                                          \
678        inst->ENTRYPOINT =                                                     \
679            (void *)vkGetInstanceProcAddr(inst->instance, #ENTRYPOINT);        \
680    }
681        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceSupportKHR)
682        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
683        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfaceFormatsKHR)
684        GET_INSTANCE_PROC_ADDR(vkGetPhysicalDeviceSurfacePresentModesKHR)
685#undef GET_INSTANCE_PROC_ADDR
686    }
687}
688
689//-----------------------------------------------------------
690
691static void app_destroy_instance(struct app_instance *inst) {
692    free(inst->global_extensions);
693    vkDestroyInstance(inst->instance, NULL);
694}
695
696static void app_gpu_init(struct app_gpu *gpu, uint32_t id,
697                         VkPhysicalDevice obj) {
698    uint32_t i;
699
700    memset(gpu, 0, sizeof(*gpu));
701
702    gpu->id = id;
703    gpu->obj = obj;
704
705    vkGetPhysicalDeviceProperties(gpu->obj, &gpu->props);
706
707    /* get queue count */
708    vkGetPhysicalDeviceQueueFamilyProperties(gpu->obj, &gpu->queue_count, NULL);
709
710    gpu->queue_props = malloc(sizeof(gpu->queue_props[0]) * gpu->queue_count);
711
712    if (!gpu->queue_props)
713        ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
714    vkGetPhysicalDeviceQueueFamilyProperties(gpu->obj, &gpu->queue_count,
715                                             gpu->queue_props);
716
717    /* set up queue requests */
718    gpu->queue_reqs = malloc(sizeof(*gpu->queue_reqs) * gpu->queue_count);
719    if (!gpu->queue_reqs)
720        ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
721    for (i = 0; i < gpu->queue_count; i++) {
722        float *queue_priorities =
723            malloc(gpu->queue_props[i].queueCount * sizeof(float));
724        memset(queue_priorities, 0,
725               gpu->queue_props[i].queueCount * sizeof(float));
726        gpu->queue_reqs[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
727        gpu->queue_reqs[i].pNext = NULL;
728        gpu->queue_reqs[i].queueFamilyIndex = i;
729        gpu->queue_reqs[i].queueCount = gpu->queue_props[i].queueCount;
730        gpu->queue_reqs[i].pQueuePriorities = queue_priorities;
731    }
732
733    vkGetPhysicalDeviceMemoryProperties(gpu->obj, &gpu->memory_props);
734
735    vkGetPhysicalDeviceFeatures(gpu->obj, &gpu->features);
736
737    app_dev_init(&gpu->dev, gpu);
738    app_dev_init_formats(&gpu->dev);
739}
740
741static void app_gpu_destroy(struct app_gpu *gpu) {
742    app_dev_destroy(&gpu->dev);
743    free(gpu->device_extensions);
744
745    for (uint32_t i = 0; i < gpu->queue_count; i++) {
746        free((void *)gpu->queue_reqs[i].pQueuePriorities);
747    }
748    free(gpu->queue_reqs);
749    free(gpu->queue_props);
750}
751
752// clang-format off
753
754//-----------------------------------------------------------
755
756//---------------------------Win32---------------------------
757#ifdef VK_USE_PLATFORM_WIN32_KHR
758
759// MS-Windows event handling function:
760LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
761    return (DefWindowProc(hWnd, uMsg, wParam, lParam));
762}
763
764static void app_create_win32_window(struct app_instance *inst) {
765    inst->hInstance = GetModuleHandle(NULL);
766
767    WNDCLASSEX win_class;
768
769    // Initialize the window class structure:
770    win_class.cbSize = sizeof(WNDCLASSEX);
771    win_class.style = CS_HREDRAW | CS_VREDRAW;
772    win_class.lpfnWndProc = WndProc;
773    win_class.cbClsExtra = 0;
774    win_class.cbWndExtra = 0;
775    win_class.hInstance = inst->hInstance;
776    win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
777    win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
778    win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
779    win_class.lpszMenuName = NULL;
780    win_class.lpszClassName = APP_SHORT_NAME;
781    win_class.hInstance = inst->hInstance;
782    win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
783    // Register window class:
784    if (!RegisterClassEx(&win_class)) {
785        // It didn't work, so try to give a useful error:
786        printf("Failed to register the window class!\n");
787        fflush(stdout);
788        exit(1);
789    }
790    // Create window with the registered class:
791    RECT wr = { 0, 0, inst->width, inst->height };
792    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
793    inst->hWnd = CreateWindowEx(0,
794        APP_SHORT_NAME,       // class name
795        APP_SHORT_NAME,       // app name
796        //WS_VISIBLE | WS_SYSMENU |
797        WS_OVERLAPPEDWINDOW,  // window style
798        100, 100,             // x/y coords
799        wr.right - wr.left,   // width
800        wr.bottom - wr.top,   // height
801        NULL,                 // handle to parent
802        NULL,                 // handle to menu
803        inst->hInstance,      // hInstance
804        NULL);                // no extra parameters
805    if (!inst->hWnd) {
806        // It didn't work, so try to give a useful error:
807        printf("Failed to create a window!\n");
808        fflush(stdout);
809        exit(1);
810    }
811}
812
813static void app_create_win32_surface(struct app_instance *inst, struct app_gpu *gpu) {
814    VkResult U_ASSERT_ONLY err;
815    VkWin32SurfaceCreateInfoKHR createInfo;
816    createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
817    createInfo.pNext = NULL;
818    createInfo.flags = 0;
819    createInfo.hinstance = inst->hInstance;
820    createInfo.hwnd = inst->hWnd;
821    err = vkCreateWin32SurfaceKHR(inst->instance, &createInfo, NULL, &inst->surface);
822    assert(!err);
823}
824
825static void app_destroy_win32_window(struct app_instance *inst) {
826    DestroyWindow(inst->hWnd);
827}
828#endif //VK_USE_PLATFORM_WIN32_KHR
829//-----------------------------------------------------------
830
831static void app_destroy_surface(struct app_instance *inst) { //same for all platforms
832    vkDestroySurfaceKHR(inst->instance, inst->surface, NULL);
833}
834
835//----------------------------XCB----------------------------
836
837#ifdef VK_USE_PLATFORM_XCB_KHR
838static void app_create_xcb_window(struct app_instance *inst) {
839  //--Init Connection--
840  const xcb_setup_t *setup;
841  xcb_screen_iterator_t iter;
842  int scr;
843
844  inst->xcb_connection = xcb_connect(NULL, &scr);
845  if (inst->xcb_connection == NULL) {
846      printf("XCB failed to connect to the X server.\nExiting ...\n");
847      fflush(stdout);
848      exit(1);
849  }
850
851  setup = xcb_get_setup(inst->xcb_connection);
852  iter = xcb_setup_roots_iterator(setup);
853  while (scr-- > 0)
854      xcb_screen_next(&iter);
855
856  inst->xcb_screen = iter.data;
857  //-------------------
858
859  inst->xcb_window = xcb_generate_id(inst->xcb_connection);
860  xcb_create_window(inst->xcb_connection, XCB_COPY_FROM_PARENT, inst->xcb_window,
861                    inst->xcb_screen->root, 0, 0, inst->width, inst->height, 0,
862                    XCB_WINDOW_CLASS_INPUT_OUTPUT, inst->xcb_screen->root_visual,
863                    0, NULL);
864
865  xcb_intern_atom_cookie_t cookie = xcb_intern_atom(inst->xcb_connection, 1, 12, "WM_PROTOCOLS");
866  xcb_intern_atom_reply_t *reply =  xcb_intern_atom_reply(inst->xcb_connection, cookie, 0);
867  free(reply);
868}
869
870static void app_create_xcb_surface(struct app_instance *inst, struct app_gpu *gpu) {
871    VkResult U_ASSERT_ONLY err;
872    VkXcbSurfaceCreateInfoKHR xcb_createInfo;
873    xcb_createInfo.sType      = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
874    xcb_createInfo.pNext      = NULL;
875    xcb_createInfo.flags      = 0;
876    xcb_createInfo.connection = inst->xcb_connection;
877    xcb_createInfo.window     = inst->xcb_window;
878    err = vkCreateXcbSurfaceKHR(inst->instance, &xcb_createInfo, NULL, &inst->surface);
879    assert(!err);
880}
881
882static void app_destroy_xcb_window(struct app_instance *inst) {
883    xcb_destroy_window(inst->xcb_connection, inst->xcb_window);
884    xcb_disconnect(inst->xcb_connection);
885}
886#endif //VK_USE_PLATFORM_XCB_KHR
887//-----------------------------------------------------------
888
889//----------------------------XLib---------------------------
890#ifdef VK_USE_PLATFORM_XLIB_KHR
891static void app_create_xlib_window(struct app_instance *inst) {
892    inst->xlib_display = XOpenDisplay(NULL);
893    long visualMask = VisualScreenMask;
894    int numberOfVisuals;
895
896    XVisualInfo vInfoTemplate={};
897    vInfoTemplate.screen = DefaultScreen(inst->xlib_display);
898    XVisualInfo *visualInfo = XGetVisualInfo(inst->xlib_display, visualMask,
899                                             &vInfoTemplate, &numberOfVisuals);
900    inst->xlib_window = XCreateWindow(
901                inst->xlib_display, RootWindow(inst->xlib_display, vInfoTemplate.screen), 0, 0,
902                inst->width, inst->height, 0, visualInfo->depth, InputOutput,
903                visualInfo->visual, 0, NULL);
904
905    XSync(inst->xlib_display,false);
906}
907
908static void app_create_xlib_surface(struct app_instance *inst, struct app_gpu *gpu) {
909    VkResult U_ASSERT_ONLY err;
910    VkXlibSurfaceCreateInfoKHR createInfo;
911    createInfo.sType  = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
912    createInfo.pNext  = NULL;
913    createInfo.flags  = 0;
914    createInfo.dpy    = inst->xlib_display;
915    createInfo.window = inst->xlib_window;
916    err = vkCreateXlibSurfaceKHR(inst->instance, &createInfo, NULL, &inst->surface);
917    assert(!err);
918}
919
920static void app_destroy_xlib_window(struct app_instance *inst) {
921    XDestroyWindow(inst->xlib_display, inst->xlib_window);
922    XCloseDisplay(inst->xlib_display);
923}
924#endif //VK_USE_PLATFORM_XLIB_KHR
925//-----------------------------------------------------------
926
927static int app_dump_surface_formats(struct app_instance *inst, struct app_gpu *gpu){
928    // Get the list of VkFormat's that are supported:
929  VkResult U_ASSERT_ONLY err;
930  uint32_t formatCount=0;
931  err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, NULL);
932  assert(!err);
933  VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
934  err = inst->vkGetPhysicalDeviceSurfaceFormatsKHR(gpu->obj, inst->surface, &formatCount, surfFormats);
935  assert(!err);
936  printf("Format count = %d\n",formatCount);
937  uint32_t i;
938  for(i=0;i<formatCount;i++) printf("\t%s\n",vk_format_string(surfFormats[i].format));
939  printf("\n");
940  fflush(stdout);
941  return formatCount;
942}
943
944static void app_dev_dump_format_props(const struct app_dev *dev, VkFormat fmt)
945{
946    const VkFormatProperties *props = &dev->format_props[fmt];
947    struct {
948        const char *name;
949        VkFlags flags;
950    } features[3];
951    uint32_t i;
952
953    features[0].name  = "linearTiling   FormatFeatureFlags";
954    features[0].flags = props->linearTilingFeatures;
955    features[1].name  = "optimalTiling  FormatFeatureFlags";
956    features[1].flags = props->optimalTilingFeatures;
957    features[2].name  = "bufferFeatures FormatFeatureFlags";
958    features[2].flags = props->bufferFeatures;
959
960    printf("\nFORMAT_%s:", vk_format_string(fmt));
961    for (i = 0; i < ARRAY_SIZE(features); i++) {
962        printf("\n\t%s:", features[i].name);
963        if (features[i].flags == 0) {
964            printf("\n\t\tNone");
965        } else {
966            printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
967               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT"                  : ""),  //0x0001
968               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_BIT"                  : ""),  //0x0002
969               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT"           : ""),  //0x0004
970               ((features[i].flags & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT"           : ""),  //0x0008
971               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT)           ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT"           : ""),  //0x0010
972               ((features[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT"    : ""),  //0x0020
973               ((features[i].flags & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)                  ? "\n\t\tVK_FORMAT_FEATURE_VERTEX_BUFFER_BIT"                  : ""),  //0x0040
974               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)               ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT"               : ""),  //0x0080
975               ((features[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)         ? "\n\t\tVK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT"         : ""),  //0x0100
976               ((features[i].flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)       ? "\n\t\tVK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT"       : ""),  //0x0200
977               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_SRC_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_SRC_BIT"                       : ""),  //0x0400
978               ((features[i].flags & VK_FORMAT_FEATURE_BLIT_DST_BIT)                       ? "\n\t\tVK_FORMAT_FEATURE_BLIT_DST_BIT"                       : ""),  //0x0800
979               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)    ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT"    : ""),  //0x1000
980               ((features[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG) ? "\n\t\tVK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_IMG" : "")); //0x2000
981        }
982    }
983    printf("\n");
984}
985
986
987static void
988app_dev_dump(const struct app_dev *dev)
989{
990    printf("Format Properties:\n");
991    printf("==================");
992    VkFormat fmt;
993
994    for (fmt = 0; fmt < VK_FORMAT_RANGE_SIZE; fmt++) {
995        app_dev_dump_format_props(dev, fmt);
996    }
997}
998
999#ifdef _WIN32
1000#define PRINTF_SIZE_T_SPECIFIER    "%Iu"
1001#else
1002#define PRINTF_SIZE_T_SPECIFIER    "%zu"
1003#endif
1004
1005static void app_gpu_dump_features(const struct app_gpu *gpu)
1006{
1007    const VkPhysicalDeviceFeatures *features = &gpu->features;
1008
1009    printf("VkPhysicalDeviceFeatures:\n");
1010    printf("=========================\n");
1011
1012    printf("\trobustBufferAccess                      = %u\n", features->robustBufferAccess                     );
1013    printf("\tfullDrawIndexUint32                     = %u\n", features->fullDrawIndexUint32                    );
1014    printf("\timageCubeArray                          = %u\n", features->imageCubeArray                         );
1015    printf("\tindependentBlend                        = %u\n", features->independentBlend                       );
1016    printf("\tgeometryShader                          = %u\n", features->geometryShader                         );
1017    printf("\ttessellationShader                      = %u\n", features->tessellationShader                     );
1018    printf("\tsampleRateShading                       = %u\n", features->sampleRateShading                      );
1019    printf("\tdualSrcBlend                            = %u\n", features->dualSrcBlend                           );
1020    printf("\tlogicOp                                 = %u\n", features->logicOp                                );
1021    printf("\tmultiDrawIndirect                       = %u\n", features->multiDrawIndirect                      );
1022    printf("\tdrawIndirectFirstInstance               = %u\n", features->drawIndirectFirstInstance              );
1023    printf("\tdepthClamp                              = %u\n", features->depthClamp                             );
1024    printf("\tdepthBiasClamp                          = %u\n", features->depthBiasClamp                         );
1025    printf("\tfillModeNonSolid                        = %u\n", features->fillModeNonSolid                       );
1026    printf("\tdepthBounds                             = %u\n", features->depthBounds                            );
1027    printf("\twideLines                               = %u\n", features->wideLines                              );
1028    printf("\tlargePoints                             = %u\n", features->largePoints                            );
1029    printf("\ttextureCompressionETC2                  = %u\n", features->textureCompressionETC2                 );
1030    printf("\ttextureCompressionASTC_LDR              = %u\n", features->textureCompressionASTC_LDR             );
1031    printf("\ttextureCompressionBC                    = %u\n", features->textureCompressionBC                   );
1032    printf("\tocclusionQueryPrecise                   = %u\n", features->occlusionQueryPrecise                  );
1033    printf("\tpipelineStatisticsQuery                 = %u\n", features->pipelineStatisticsQuery                );
1034    printf("\tvertexSideEffects                       = %u\n", features->vertexPipelineStoresAndAtomics         );
1035    printf("\ttessellationSideEffects                 = %u\n", features->fragmentStoresAndAtomics               );
1036    printf("\tgeometrySideEffects                     = %u\n", features->shaderTessellationAndGeometryPointSize );
1037    printf("\tshaderImageGatherExtended               = %u\n", features->shaderImageGatherExtended              );
1038    printf("\tshaderStorageImageExtendedFormats       = %u\n", features->shaderStorageImageExtendedFormats      );
1039    printf("\tshaderStorageImageMultisample           = %u\n", features->shaderStorageImageMultisample          );
1040    printf("\tshaderStorageImageReadWithoutFormat     = %u\n", features->shaderStorageImageReadWithoutFormat    );
1041    printf("\tshaderStorageImageWriteWithoutFormat    = %u\n", features->shaderStorageImageWriteWithoutFormat   );
1042    printf("\tshaderUniformBufferArrayDynamicIndexing = %u\n", features->shaderUniformBufferArrayDynamicIndexing);
1043    printf("\tshaderSampledImageArrayDynamicIndexing  = %u\n", features->shaderSampledImageArrayDynamicIndexing );
1044    printf("\tshaderStorageBufferArrayDynamicIndexing = %u\n", features->shaderStorageBufferArrayDynamicIndexing);
1045    printf("\tshaderStorageImageArrayDynamicIndexing  = %u\n", features->shaderStorageImageArrayDynamicIndexing );
1046    printf("\tshaderClipDistance                      = %u\n", features->shaderClipDistance                     );
1047    printf("\tshaderCullDistance                      = %u\n", features->shaderCullDistance                     );
1048    printf("\tshaderFloat64                           = %u\n", features->shaderFloat64                          );
1049    printf("\tshaderInt64                             = %u\n", features->shaderInt64                            );
1050    printf("\tshaderInt16                             = %u\n", features->shaderInt16                            );
1051    printf("\tshaderResourceResidency                 = %u\n", features->shaderResourceResidency                );
1052    printf("\tshaderResourceMinLod                    = %u\n", features->shaderResourceMinLod                   );
1053    printf("\talphaToOne                              = %u\n", features->alphaToOne                             );
1054    printf("\tsparseBinding                           = %u\n", features->sparseBinding                          );
1055    printf("\tsparseResidencyBuffer                   = %u\n", features->sparseResidencyBuffer                  );
1056    printf("\tsparseResidencyImage2D                  = %u\n", features->sparseResidencyImage2D                 );
1057    printf("\tsparseResidencyImage3D                  = %u\n", features->sparseResidencyImage3D                 );
1058    printf("\tsparseResidency2Samples                 = %u\n", features->sparseResidency2Samples                );
1059    printf("\tsparseResidency4Samples                 = %u\n", features->sparseResidency4Samples                );
1060    printf("\tsparseResidency8Samples                 = %u\n", features->sparseResidency8Samples                );
1061    printf("\tsparseResidency16Samples                = %u\n", features->sparseResidency16Samples               );
1062    printf("\tsparseResidencyAliased                  = %u\n", features->sparseResidencyAliased                 );
1063    printf("\tvariableMultisampleRate                 = %u\n", features->variableMultisampleRate                );
1064    printf("\tiheritedQueries                         = %u\n", features->inheritedQueries                       );
1065}
1066
1067static void app_dump_sparse_props(const VkPhysicalDeviceSparseProperties *sparseProps)
1068{
1069
1070    printf("\tVkPhysicalDeviceSparseProperties:\n");
1071    printf("\t---------------------------------\n");
1072
1073    printf("\t\tresidencyStandard2DBlockShape            = %u\n", sparseProps->residencyStandard2DBlockShape           );
1074    printf("\t\tresidencyStandard2DMultisampleBlockShape = %u\n", sparseProps->residencyStandard2DMultisampleBlockShape);
1075    printf("\t\tresidencyStandard3DBlockShape            = %u\n", sparseProps->residencyStandard3DBlockShape           );
1076    printf("\t\tresidencyAlignedMipSize                  = %u\n", sparseProps->residencyAlignedMipSize                 );
1077    printf("\t\tresidencyNonResidentStrict               = %u\n", sparseProps->residencyNonResidentStrict              );
1078}
1079
1080static void app_dump_limits(const VkPhysicalDeviceLimits *limits)
1081{
1082    printf("\tVkPhysicalDeviceLimits:\n");
1083    printf("\t-----------------------\n");
1084    printf("\t\tmaxImageDimension1D                     = %u\n",                 limits->maxImageDimension1D                    );
1085    printf("\t\tmaxImageDimension2D                     = %u\n",                 limits->maxImageDimension2D                    );
1086    printf("\t\tmaxImageDimension3D                     = %u\n",                 limits->maxImageDimension3D                    );
1087    printf("\t\tmaxImageDimensionCube                   = %u\n",                 limits->maxImageDimensionCube                  );
1088    printf("\t\tmaxImageArrayLayers                     = %u\n",                 limits->maxImageArrayLayers                    );
1089    printf("\t\tmaxTexelBufferElements                  = 0x%" PRIxLEAST32 "\n", limits->maxTexelBufferElements                 );
1090    printf("\t\tmaxUniformBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxUniformBufferRange                  );
1091    printf("\t\tmaxStorageBufferRange                   = 0x%" PRIxLEAST32 "\n", limits->maxStorageBufferRange                  );
1092    printf("\t\tmaxPushConstantsSize                    = %u\n",                 limits->maxPushConstantsSize                   );
1093    printf("\t\tmaxMemoryAllocationCount                = %u\n",                 limits->maxMemoryAllocationCount               );
1094    printf("\t\tmaxSamplerAllocationCount               = %u\n",                 limits->maxSamplerAllocationCount              );
1095    printf("\t\tbufferImageGranularity                  = 0x%" PRIxLEAST64 "\n", limits->bufferImageGranularity                 );
1096    printf("\t\tsparseAddressSpaceSize                  = 0x%" PRIxLEAST64 "\n", limits->sparseAddressSpaceSize                 );
1097    printf("\t\tmaxBoundDescriptorSets                  = %u\n",                 limits->maxBoundDescriptorSets                 );
1098    printf("\t\tmaxPerStageDescriptorSamplers           = %u\n",                 limits->maxPerStageDescriptorSamplers          );
1099    printf("\t\tmaxPerStageDescriptorUniformBuffers     = %u\n",                 limits->maxPerStageDescriptorUniformBuffers    );
1100    printf("\t\tmaxPerStageDescriptorStorageBuffers     = %u\n",                 limits->maxPerStageDescriptorStorageBuffers    );
1101    printf("\t\tmaxPerStageDescriptorSampledImages      = %u\n",                 limits->maxPerStageDescriptorSampledImages     );
1102    printf("\t\tmaxPerStageDescriptorStorageImages      = %u\n",                 limits->maxPerStageDescriptorStorageImages     );
1103    printf("\t\tmaxPerStageDescriptorInputAttachments   = %u\n",                 limits->maxPerStageDescriptorInputAttachments  );
1104    printf("\t\tmaxPerStageResources                    = %u\n",                 limits->maxPerStageResources                   );
1105    printf("\t\tmaxDescriptorSetSamplers                = %u\n",                 limits->maxDescriptorSetSamplers               );
1106    printf("\t\tmaxDescriptorSetUniformBuffers          = %u\n",                 limits->maxDescriptorSetUniformBuffers         );
1107    printf("\t\tmaxDescriptorSetUniformBuffersDynamic   = %u\n",                 limits->maxDescriptorSetUniformBuffersDynamic  );
1108    printf("\t\tmaxDescriptorSetStorageBuffers          = %u\n",                 limits->maxDescriptorSetStorageBuffers         );
1109    printf("\t\tmaxDescriptorSetStorageBuffersDynamic   = %u\n",                 limits->maxDescriptorSetStorageBuffersDynamic  );
1110    printf("\t\tmaxDescriptorSetSampledImages           = %u\n",                 limits->maxDescriptorSetSampledImages          );
1111    printf("\t\tmaxDescriptorSetStorageImages           = %u\n",                 limits->maxDescriptorSetStorageImages          );
1112    printf("\t\tmaxDescriptorSetInputAttachments        = %u\n",                 limits->maxDescriptorSetInputAttachments       );
1113    printf("\t\tmaxVertexInputAttributes                = %u\n",                 limits->maxVertexInputAttributes               );
1114    printf("\t\tmaxVertexInputBindings                  = %u\n",                 limits->maxVertexInputBindings                 );
1115    printf("\t\tmaxVertexInputAttributeOffset           = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputAttributeOffset          );
1116    printf("\t\tmaxVertexInputBindingStride             = 0x%" PRIxLEAST32 "\n", limits->maxVertexInputBindingStride            );
1117    printf("\t\tmaxVertexOutputComponents               = %u\n",                 limits->maxVertexOutputComponents              );
1118    printf("\t\tmaxTessellationGenerationLevel          = %u\n",                 limits->maxTessellationGenerationLevel         );
1119    printf("\t\tmaxTessellationPatchSize                        = %u\n",                 limits->maxTessellationPatchSize                       );
1120    printf("\t\tmaxTessellationControlPerVertexInputComponents  = %u\n",                 limits->maxTessellationControlPerVertexInputComponents );
1121    printf("\t\tmaxTessellationControlPerVertexOutputComponents = %u\n",                 limits->maxTessellationControlPerVertexOutputComponents);
1122    printf("\t\tmaxTessellationControlPerPatchOutputComponents  = %u\n",                 limits->maxTessellationControlPerPatchOutputComponents );
1123    printf("\t\tmaxTessellationControlTotalOutputComponents     = %u\n",                 limits->maxTessellationControlTotalOutputComponents    );
1124    printf("\t\tmaxTessellationEvaluationInputComponents        = %u\n",                 limits->maxTessellationEvaluationInputComponents       );
1125    printf("\t\tmaxTessellationEvaluationOutputComponents       = %u\n",                 limits->maxTessellationEvaluationOutputComponents      );
1126    printf("\t\tmaxGeometryShaderInvocations            = %u\n",                 limits->maxGeometryShaderInvocations           );
1127    printf("\t\tmaxGeometryInputComponents              = %u\n",                 limits->maxGeometryInputComponents             );
1128    printf("\t\tmaxGeometryOutputComponents             = %u\n",                 limits->maxGeometryOutputComponents            );
1129    printf("\t\tmaxGeometryOutputVertices               = %u\n",                 limits->maxGeometryOutputVertices              );
1130    printf("\t\tmaxGeometryTotalOutputComponents        = %u\n",                 limits->maxGeometryTotalOutputComponents       );
1131    printf("\t\tmaxFragmentInputComponents              = %u\n",                 limits->maxFragmentInputComponents             );
1132    printf("\t\tmaxFragmentOutputAttachments            = %u\n",                 limits->maxFragmentOutputAttachments           );
1133    printf("\t\tmaxFragmentDualSrcAttachments           = %u\n",                 limits->maxFragmentDualSrcAttachments          );
1134    printf("\t\tmaxFragmentCombinedOutputResources      = %u\n",                 limits->maxFragmentCombinedOutputResources     );
1135    printf("\t\tmaxComputeSharedMemorySize              = 0x%" PRIxLEAST32 "\n", limits->maxComputeSharedMemorySize             );
1136    printf("\t\tmaxComputeWorkGroupCount[0]             = %u\n",                 limits->maxComputeWorkGroupCount[0]            );
1137    printf("\t\tmaxComputeWorkGroupCount[1]             = %u\n",                 limits->maxComputeWorkGroupCount[1]            );
1138    printf("\t\tmaxComputeWorkGroupCount[2]             = %u\n",                 limits->maxComputeWorkGroupCount[2]            );
1139    printf("\t\tmaxComputeWorkGroupInvocations          = %u\n",                 limits->maxComputeWorkGroupInvocations         );
1140    printf("\t\tmaxComputeWorkGroupSize[0]              = %u\n",                 limits->maxComputeWorkGroupSize[0]             );
1141    printf("\t\tmaxComputeWorkGroupSize[1]              = %u\n",                 limits->maxComputeWorkGroupSize[1]             );
1142    printf("\t\tmaxComputeWorkGroupSize[2]              = %u\n",                 limits->maxComputeWorkGroupSize[2]             );
1143    printf("\t\tsubPixelPrecisionBits                   = %u\n",                 limits->subPixelPrecisionBits                  );
1144    printf("\t\tsubTexelPrecisionBits                   = %u\n",                 limits->subTexelPrecisionBits                  );
1145    printf("\t\tmipmapPrecisionBits                     = %u\n",                 limits->mipmapPrecisionBits                    );
1146    printf("\t\tmaxDrawIndexedIndexValue                = %u\n",                 limits->maxDrawIndexedIndexValue               );
1147    printf("\t\tmaxDrawIndirectCount                    = %u\n",                 limits->maxDrawIndirectCount                   );
1148    printf("\t\tmaxSamplerLodBias                       = %f\n",                 limits->maxSamplerLodBias                      );
1149    printf("\t\tmaxSamplerAnisotropy                    = %f\n",                 limits->maxSamplerAnisotropy                   );
1150    printf("\t\tmaxViewports                            = %u\n",                 limits->maxViewports                           );
1151    printf("\t\tmaxViewportDimensions[0]                = %u\n",                 limits->maxViewportDimensions[0]               );
1152    printf("\t\tmaxViewportDimensions[1]                = %u\n",                 limits->maxViewportDimensions[1]               );
1153    printf("\t\tviewportBoundsRange[0]                  =%13f\n",                 limits->viewportBoundsRange[0]                 );
1154    printf("\t\tviewportBoundsRange[1]                  =%13f\n",                 limits->viewportBoundsRange[1]                 );
1155    printf("\t\tviewportSubPixelBits                    = %u\n",                 limits->viewportSubPixelBits                   );
1156    printf("\t\tminMemoryMapAlignment                   = " PRINTF_SIZE_T_SPECIFIER "\n", limits->minMemoryMapAlignment         );
1157    printf("\t\tminTexelBufferOffsetAlignment           = 0x%" PRIxLEAST64 "\n", limits->minTexelBufferOffsetAlignment          );
1158    printf("\t\tminUniformBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minUniformBufferOffsetAlignment        );
1159    printf("\t\tminStorageBufferOffsetAlignment         = 0x%" PRIxLEAST64 "\n", limits->minStorageBufferOffsetAlignment        );
1160    printf("\t\tminTexelOffset                          =%3d\n",                 limits->minTexelOffset                         );
1161    printf("\t\tmaxTexelOffset                          =%3d\n",                 limits->maxTexelOffset                         );
1162    printf("\t\tminTexelGatherOffset                    =%3d\n",                 limits->minTexelGatherOffset                   );
1163    printf("\t\tmaxTexelGatherOffset                    =%3d\n",                 limits->maxTexelGatherOffset                   );
1164    printf("\t\tminInterpolationOffset                  =%9f\n",                 limits->minInterpolationOffset                 );
1165    printf("\t\tmaxInterpolationOffset                  =%9f\n",                 limits->maxInterpolationOffset                 );
1166    printf("\t\tsubPixelInterpolationOffsetBits         = %u\n",                 limits->subPixelInterpolationOffsetBits        );
1167    printf("\t\tmaxFramebufferWidth                     = %u\n",                 limits->maxFramebufferWidth                    );
1168    printf("\t\tmaxFramebufferHeight                    = %u\n",                 limits->maxFramebufferHeight                   );
1169    printf("\t\tmaxFramebufferLayers                    = %u\n",                 limits->maxFramebufferLayers                   );
1170    printf("\t\tframebufferColorSampleCounts            = %u\n",                 limits->framebufferColorSampleCounts           );
1171    printf("\t\tframebufferDepthSampleCounts            = %u\n",                 limits->framebufferDepthSampleCounts           );
1172    printf("\t\tframebufferStencilSampleCounts          = %u\n",                 limits->framebufferStencilSampleCounts         );
1173    printf("\t\tframebufferNoAttachmentsSampleCounts    = %u\n",                 limits->framebufferNoAttachmentsSampleCounts   );
1174    printf("\t\tmaxColorAttachments                     = %u\n",                 limits->maxColorAttachments                    );
1175    printf("\t\tsampledImageColorSampleCounts           = %u\n",                 limits->sampledImageColorSampleCounts          );
1176    printf("\t\tsampledImageDepthSampleCounts           = %u\n",                 limits->sampledImageDepthSampleCounts          );
1177    printf("\t\tsampledImageStencilSampleCounts         = %u\n",                 limits->sampledImageStencilSampleCounts        );
1178    printf("\t\tsampledImageIntegerSampleCounts         = %u\n",                 limits->sampledImageIntegerSampleCounts        );
1179    printf("\t\tstorageImageSampleCounts                = %u\n",                 limits->storageImageSampleCounts               );
1180    printf("\t\tmaxSampleMaskWords                      = %u\n",                 limits->maxSampleMaskWords                     );
1181    printf("\t\ttimestampComputeAndGraphics             = %u\n",                 limits->timestampComputeAndGraphics            );
1182    printf("\t\ttimestampPeriod                         = %f\n",                 limits->timestampPeriod                        );
1183    printf("\t\tmaxClipDistances                        = %u\n",                 limits->maxClipDistances                       );
1184    printf("\t\tmaxCullDistances                        = %u\n",                 limits->maxCullDistances                       );
1185    printf("\t\tmaxCombinedClipAndCullDistances         = %u\n",                 limits->maxCombinedClipAndCullDistances        );
1186    printf("\t\tdiscreteQueuePriorities                 = %u\n",                 limits->discreteQueuePriorities                );
1187    printf("\t\tpointSizeRange[0]                       = %f\n",                 limits->pointSizeRange[0]                      );
1188    printf("\t\tpointSizeRange[1]                       = %f\n",                 limits->pointSizeRange[1]                      );
1189    printf("\t\tlineWidthRange[0]                       = %f\n",                 limits->lineWidthRange[0]                      );
1190    printf("\t\tlineWidthRange[1]                       = %f\n",                 limits->lineWidthRange[1]                      );
1191    printf("\t\tpointSizeGranularity                    = %f\n",                 limits->pointSizeGranularity                   );
1192    printf("\t\tlineWidthGranularity                    = %f\n",                 limits->lineWidthGranularity                   );
1193    printf("\t\tstrictLines                             = %u\n",                 limits->strictLines                            );
1194    printf("\t\tstandardSampleLocations                 = %u\n",                 limits->standardSampleLocations                );
1195    printf("\t\toptimalBufferCopyOffsetAlignment        = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyOffsetAlignment       );
1196    printf("\t\toptimalBufferCopyRowPitchAlignment      = 0x%" PRIxLEAST64 "\n", limits->optimalBufferCopyRowPitchAlignment     );
1197    printf("\t\tnonCoherentAtomSize                     = 0x%" PRIxLEAST64 "\n", limits->nonCoherentAtomSize                    );
1198}
1199
1200static void app_gpu_dump_props(const struct app_gpu *gpu)
1201{
1202    const VkPhysicalDeviceProperties *props = &gpu->props;
1203    const uint32_t apiVersion=props->apiVersion;
1204    const uint32_t major = VK_VERSION_MAJOR(apiVersion);
1205    const uint32_t minor = VK_VERSION_MINOR(apiVersion);
1206    const uint32_t patch = VK_VERSION_PATCH(apiVersion);
1207
1208    printf("VkPhysicalDeviceProperties:\n");
1209    printf("===========================\n");
1210    printf("\tapiVersion     = 0x%" PRIxLEAST32 "  (%d.%d.%d)\n", apiVersion, major, minor, patch);
1211    printf("\tdriverVersion  = %u (0x%" PRIxLEAST32 ")\n",props->driverVersion, props->driverVersion);
1212    printf("\tvendorID       = 0x%04x\n",                 props->vendorID);
1213    printf("\tdeviceID       = 0x%04x\n",                 props->deviceID);
1214    printf("\tdeviceType     = %s\n",                     vk_physical_device_type_string(props->deviceType));
1215    printf("\tdeviceName     = %s\n",                     props->deviceName);
1216
1217    app_dump_limits(&gpu->props.limits);
1218    app_dump_sparse_props(&gpu->props.sparseProperties);
1219
1220    fflush(stdout);
1221}
1222// clang-format on
1223
1224static void
1225app_dump_extensions(const char *indent, const char *layer_name,
1226                    const uint32_t extension_count,
1227                    const VkExtensionProperties *extension_properties) {
1228    uint32_t i;
1229    if (layer_name && (strlen(layer_name) > 0)) {
1230        printf("%s%s Extensions", indent, layer_name);
1231    } else {
1232        printf("%sExtensions", indent);
1233    }
1234    printf("\tcount = %d\n", extension_count);
1235    for (i = 0; i < extension_count; i++) {
1236        VkExtensionProperties const *ext_prop = &extension_properties[i];
1237
1238        printf("%s\t", indent);
1239        printf("%-36s: extension revision %2d\n", ext_prop->extensionName,
1240               ext_prop->specVersion);
1241    }
1242    fflush(stdout);
1243}
1244
1245static void app_gpu_dump_queue_props(const struct app_gpu *gpu, uint32_t id) {
1246    const VkQueueFamilyProperties *props = &gpu->queue_props[id];
1247
1248    printf("VkQueueFamilyProperties[%d]:\n", id);
1249    printf("===========================\n");
1250    char *sep = ""; // separator character
1251    printf("\tqueueFlags         = ");
1252    if (props->queueFlags & VK_QUEUE_GRAPHICS_BIT) {
1253        printf("GRAPHICS");
1254        sep = " | ";
1255    }
1256    if (props->queueFlags & VK_QUEUE_COMPUTE_BIT) {
1257        printf("%sCOMPUTE", sep);
1258        sep = " | ";
1259    }
1260    if (props->queueFlags & VK_QUEUE_TRANSFER_BIT) {
1261        printf("%sTRANSFER", sep);
1262        sep = " | ";
1263    }
1264    if (props->queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) {
1265        printf("%sSPARSE", sep);
1266    }
1267    printf("\n");
1268
1269    printf("\tqueueCount         = %u\n", props->queueCount);
1270    printf("\ttimestampValidBits = %u\n", props->timestampValidBits);
1271    printf("\tminImageTransferGranularity = (%d, %d, %d)\n",
1272           props->minImageTransferGranularity.width,
1273           props->minImageTransferGranularity.height,
1274           props->minImageTransferGranularity.depth);
1275    fflush(stdout);
1276}
1277
1278static void app_gpu_dump_memory_props(const struct app_gpu *gpu) {
1279    const VkPhysicalDeviceMemoryProperties *props = &gpu->memory_props;
1280
1281    printf("VkPhysicalDeviceMemoryProperties:\n");
1282    printf("=================================\n");
1283    printf("\tmemoryTypeCount       = %u\n", props->memoryTypeCount);
1284    for (uint32_t i = 0; i < props->memoryTypeCount; i++) {
1285        printf("\tmemoryTypes[%u] : \n", i);
1286        printf("\t\theapIndex     = %u\n", props->memoryTypes[i].heapIndex);
1287        printf("\t\tpropertyFlags = 0x%" PRIxLEAST32 ":\n",
1288               props->memoryTypes[i].propertyFlags);
1289
1290        // Print each named flag, if it is set.
1291        VkFlags flags = props->memoryTypes[i].propertyFlags;
1292#define PRINT_FLAG(FLAG)                                                       \
1293    if (flags & FLAG)                                                          \
1294        printf("\t\t\t" #FLAG "\n");
1295        PRINT_FLAG(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
1296        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
1297        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1298        PRINT_FLAG(VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
1299        PRINT_FLAG(VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT)
1300#undef PRINT_FLAG
1301    }
1302    printf("\n");
1303    printf("\tmemoryHeapCount       = %u\n", props->memoryHeapCount);
1304    for (uint32_t i = 0; i < props->memoryHeapCount; i++) {
1305        printf("\tmemoryHeaps[%u] : \n", i);
1306        const VkDeviceSize memSize = props->memoryHeaps[i].size;
1307        printf("\t\tsize          = " PRINTF_SIZE_T_SPECIFIER
1308               " (0x%" PRIxLEAST64 ")\n",
1309               (size_t)memSize, memSize);
1310
1311        VkMemoryHeapFlags heapFlags = props->memoryHeaps[i].flags;
1312        printf("\t\tflags: \n\t\t\t");
1313        printf((heapFlags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
1314                   ? "VK_MEMORY_HEAP_DEVICE_LOCAL_BIT\n"
1315                   : "None\n");
1316    }
1317    fflush(stdout);
1318}
1319
1320static void app_gpu_dump(const struct app_gpu *gpu) {
1321    uint32_t i;
1322
1323    printf("\nDevice Properties and Extensions :\n");
1324    printf(  "==================================\n");
1325    printf("GPU%u\n", gpu->id);
1326    app_gpu_dump_props(gpu);
1327    printf("\n");
1328    app_dump_extensions("", "Device", gpu->device_extension_count,
1329                        gpu->device_extensions);
1330    printf("\n");
1331    for (i = 0; i < gpu->queue_count; i++) {
1332        app_gpu_dump_queue_props(gpu, i);
1333        printf("\n");
1334    }
1335    app_gpu_dump_memory_props(gpu);
1336    printf("\n");
1337    app_gpu_dump_features(gpu);
1338    printf("\n");
1339    app_dev_dump(&gpu->dev);
1340}
1341
1342#ifdef _WIN32
1343// Enlarges the console window to have a large scrollback size.
1344static void ConsoleEnlarge() {
1345    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
1346
1347    // make the console window bigger
1348    CONSOLE_SCREEN_BUFFER_INFO csbi;
1349    COORD bufferSize;
1350    if (GetConsoleScreenBufferInfo(consoleHandle, &csbi)) {
1351        bufferSize.X = csbi.dwSize.X + 30;
1352        bufferSize.Y = 20000;
1353        SetConsoleScreenBufferSize(consoleHandle, bufferSize);
1354    }
1355
1356    SMALL_RECT r;
1357    r.Left = r.Top = 0;
1358    r.Right = csbi.dwSize.X - 1 + 30;
1359    r.Bottom = 50;
1360    SetConsoleWindowInfo(consoleHandle, true, &r);
1361
1362    // change the console window title
1363    SetConsoleTitle(TEXT(APP_SHORT_NAME));
1364}
1365#endif
1366
1367int main(int argc, char **argv) {
1368    unsigned int major, minor, patch;
1369    struct app_gpu gpus[MAX_GPUS];
1370    VkPhysicalDevice objs[MAX_GPUS];
1371    uint32_t gpu_count, i;
1372    VkResult err;
1373    struct app_instance inst;
1374
1375#ifdef _WIN32
1376    if (ConsoleIsExclusive())
1377        ConsoleEnlarge();
1378#endif
1379
1380    major = VK_VERSION_MAJOR(VK_API_VERSION_1_0);
1381    minor = VK_VERSION_MINOR(VK_API_VERSION_1_0);
1382    patch = VK_VERSION_PATCH(VK_HEADER_VERSION);
1383
1384    printf("===========\n");
1385    printf("VULKAN INFO\n");
1386    printf("===========\n\n");
1387    printf("Vulkan API Version: %d.%d.%d\n\n", major, minor, patch);
1388
1389    app_create_instance(&inst);
1390
1391    printf("\nInstance Extensions:\n");
1392    printf(  "====================\n");
1393    app_dump_extensions("", "Instance", inst.global_extension_count,
1394                        inst.global_extensions);
1395
1396    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, NULL);
1397    if (err)
1398        ERR_EXIT(err);
1399    if (gpu_count > MAX_GPUS) {
1400        printf("Too many GPUS found \n");
1401        ERR_EXIT(-1);
1402    }
1403    err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, objs);
1404    if (err)
1405        ERR_EXIT(err);
1406
1407    for (i = 0; i < gpu_count; i++) {
1408        app_gpu_init(&gpus[i], i, objs[i]);
1409        printf("\n\n");
1410    }
1411
1412    //---Layer-Device-Extensions---
1413    printf("Layers: count = %d\n", inst.global_layer_count);
1414    printf("=======\n");
1415    for (uint32_t i = 0; i < inst.global_layer_count; i++) {
1416        uint32_t major, minor, patch;
1417        char spec_version[64], layer_version[64];
1418        VkLayerProperties const *layer_prop =
1419            &inst.global_layers[i].layer_properties;
1420
1421        extract_version(layer_prop->specVersion, &major, &minor, &patch);
1422        snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", major, minor,
1423                 patch);
1424        snprintf(layer_version, sizeof(layer_version), "%d",
1425                 layer_prop->implementationVersion);
1426        printf("%s (%s) Vulkan version %s, layer version %s\n",
1427               layer_prop->layerName, (char *)layer_prop->description,
1428               spec_version, layer_version);
1429
1430        app_dump_extensions("\t","Layer",
1431                            inst.global_layers[i].extension_count,
1432                            inst.global_layers[i].extension_properties);
1433
1434        char* layerName=inst.global_layers[i].layer_properties.layerName;
1435        printf("\tDevices \tcount = %d\n",gpu_count);
1436        for (uint32_t j = 0; j < gpu_count; j++) {
1437            printf("\t\tGPU id       : %u (%s)\n", j, gpus[j].props.deviceName);
1438            uint32_t count=0;
1439            VkExtensionProperties* props;
1440            app_get_physical_device_layer_extensions(&gpus[j], layerName,
1441                                                     &count, &props);
1442            app_dump_extensions("\t\t","Layer-Device",count,props);
1443            free(props);
1444        }
1445        printf("\n");
1446    }
1447    fflush(stdout);
1448    //-----------------------------
1449
1450    printf("Presentable Surface formats:\n");
1451    printf("============================\n");
1452    inst.width = 256;
1453    inst.height = 256;
1454    int formatCount = 0;
1455
1456//--WIN32--
1457#ifdef VK_USE_PLATFORM_WIN32_KHR
1458    app_create_win32_window(&inst);
1459    for (i = 0; i < gpu_count; i++) {
1460        app_create_win32_surface(&inst, &gpus[i]);
1461        printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1462        printf("Surface type : %s\n", VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
1463        formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1464        app_destroy_surface(&inst);
1465    }
1466    app_destroy_win32_window(&inst);
1467#endif
1468//--XCB--
1469#ifdef VK_USE_PLATFORM_XCB_KHR
1470    app_create_xcb_window(&inst);
1471    for (i = 0; i < gpu_count; i++) {
1472        app_create_xcb_surface(&inst, &gpus[i]);
1473        printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1474        printf("Surface type : %s\n", VK_KHR_XCB_SURFACE_EXTENSION_NAME);
1475        formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1476        app_destroy_surface(&inst);
1477    }
1478    app_destroy_xcb_window(&inst);
1479#endif
1480//--XLIB--
1481#ifdef VK_USE_PLATFORM_XLIB_KHR
1482    app_create_xlib_window(&inst);
1483    for (i = 0; i < gpu_count; i++) {
1484        app_create_xlib_surface(&inst, &gpus[i]);
1485        printf("GPU id       : %u (%s)\n", i, gpus[i].props.deviceName);
1486        printf("Surface type : %s\n", VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
1487        formatCount += app_dump_surface_formats(&inst, &gpus[i]);
1488        app_destroy_surface(&inst);
1489    }
1490    app_destroy_xlib_window(&inst);
1491#endif
1492    // TODO: Android / Wayland / MIR
1493    if (!formatCount)
1494        printf("None found\n");
1495    //---------
1496
1497    for (i = 0; i < gpu_count; i++) {
1498        app_gpu_dump(&gpus[i]);
1499        printf("\n\n");
1500    }
1501
1502    for (i = 0; i < gpu_count; i++)
1503        app_gpu_destroy(&gpus[i]);
1504
1505    app_destroy_instance(&inst);
1506
1507    fflush(stdout);
1508#ifdef _WIN32
1509    if (ConsoleIsExclusive())
1510        Sleep(INFINITE);
1511#endif
1512
1513    return 0;
1514}
1515