blob: 2e7bfbd5cc0def05b8a0ebab031cdf29c3c14cf8 [file] [log] [blame]
Chia-I Wu46c29dd2014-12-02 21:09:20 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu46c29dd2014-12-02 21:09:20 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Chia-I Wud4bae362014-07-29 11:15:00 +080024#include <stdlib.h>
25#include <stdio.h>
26#include <stdbool.h>
27#include <string.h>
Chia-I Wu46c29dd2014-12-02 21:09:20 +080028#include <assert.h>
Chia-I Wud4bae362014-07-29 11:15:00 +080029
Ian Elliottea95f5c2015-04-17 21:23:34 -060030#ifdef _WIN32
31#include <Windows.h>
David Pinedo18fb9232015-04-21 14:45:16 -060032#include <fcntl.h>
33#include <io.h>
Ian Elliottea95f5c2015-04-17 21:23:34 -060034#endif
35
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060036#include <vulkan.h>
Chia-I Wu46c29dd2014-12-02 21:09:20 +080037
38#define ERR(err) printf("%s:%d: failed with %s\n", \
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039 __FILE__, __LINE__, vk_result_string(err));
Chia-I Wu46c29dd2014-12-02 21:09:20 +080040
David Pinedo18fb9232015-04-21 14:45:16 -060041#ifdef _WIN32
42
43bool consoleCreated = false;
44
45#define WAIT_FOR_CONSOLE_DESTROY \
46 do { \
47 if (consoleCreated) \
48 Sleep(INFINITE); \
49 } while (0)
50#else
51 #define WAIT_FOR_CONSOLE_DESTROY
52#endif
53
54
55#define ERR_EXIT(err) \
56 do { \
57 ERR(err); \
58 fflush(stdout); \
59 WAIT_FOR_CONSOLE_DESTROY; \
60 exit(-1); \
61 } while (0)
Chia-I Wu46c29dd2014-12-02 21:09:20 +080062
Tony Barbour22a30862015-04-22 09:02:32 -060063#if defined(NDEBUG) && defined(__GNUC__)
64#define U_ASSERT_ONLY __attribute__((unused))
65#else
66#define U_ASSERT_ONLY
67#endif
68
Chia-I Wu46c29dd2014-12-02 21:09:20 +080069#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
70
71#define MAX_GPUS 8
72
73#define MAX_QUEUE_TYPES 5
Ian Elliott4e19ed02015-04-28 10:52:52 -060074#define APP_SHORT_NAME "vulkaninfo"
Chia-I Wu46c29dd2014-12-02 21:09:20 +080075
76struct app_gpu;
77
78struct app_dev {
79 struct app_gpu *gpu; /* point back to the GPU */
80
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060081 VkDevice obj;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080082
Chia-I Wu46c29dd2014-12-02 21:09:20 +080083
Tony Barbour8205d902015-04-16 15:59:00 -060084 VkFormatProperties format_props[VK_NUM_FORMAT];
Chia-I Wu46c29dd2014-12-02 21:09:20 +080085};
86
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -060087struct app_instance {
88 VkInstance instance;
89 uint32_t global_extension_count;
90 VkExtensionProperties *global_extensions;
91};
92
Chia-I Wu46c29dd2014-12-02 21:09:20 +080093struct app_gpu {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060094 uint32_t id;
Tony Barbour8205d902015-04-16 15:59:00 -060095 VkPhysicalDevice obj;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080096
Tony Barbour8205d902015-04-16 15:59:00 -060097 VkPhysicalDeviceProperties props;
98 VkPhysicalDevicePerformance perf;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080099
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600100 uint32_t queue_count;
Tony Barbour8205d902015-04-16 15:59:00 -0600101 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600102 VkDeviceQueueCreateInfo *queue_reqs;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800103
Tony Barbour8205d902015-04-16 15:59:00 -0600104 VkPhysicalDeviceMemoryProperties memory_props;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800105
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600106 uint32_t device_extension_count;
107 VkExtensionProperties *device_extensions;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800108
109 struct app_dev dev;
110};
111
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600112static const char *vk_result_string(VkResult err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800113{
114 switch (err) {
115#define STR(r) case r: return #r
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600116 STR(VK_SUCCESS);
117 STR(VK_UNSUPPORTED);
118 STR(VK_NOT_READY);
119 STR(VK_TIMEOUT);
120 STR(VK_EVENT_SET);
121 STR(VK_EVENT_RESET);
122 STR(VK_ERROR_UNKNOWN);
123 STR(VK_ERROR_UNAVAILABLE);
124 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbour8205d902015-04-16 15:59:00 -0600125 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
126 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600127 STR(VK_ERROR_DEVICE_ALREADY_CREATED);
128 STR(VK_ERROR_DEVICE_LOST);
129 STR(VK_ERROR_INVALID_POINTER);
130 STR(VK_ERROR_INVALID_VALUE);
131 STR(VK_ERROR_INVALID_HANDLE);
132 STR(VK_ERROR_INVALID_ORDINAL);
133 STR(VK_ERROR_INVALID_MEMORY_SIZE);
134 STR(VK_ERROR_INVALID_EXTENSION);
135 STR(VK_ERROR_INVALID_FLAGS);
136 STR(VK_ERROR_INVALID_ALIGNMENT);
137 STR(VK_ERROR_INVALID_FORMAT);
138 STR(VK_ERROR_INVALID_IMAGE);
139 STR(VK_ERROR_INVALID_DESCRIPTOR_SET_DATA);
140 STR(VK_ERROR_INVALID_QUEUE_TYPE);
141 STR(VK_ERROR_INVALID_OBJECT_TYPE);
142 STR(VK_ERROR_UNSUPPORTED_SHADER_IL_VERSION);
143 STR(VK_ERROR_BAD_SHADER_CODE);
144 STR(VK_ERROR_BAD_PIPELINE_DATA);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600145 STR(VK_ERROR_NOT_MAPPABLE);
146 STR(VK_ERROR_MEMORY_MAP_FAILED);
147 STR(VK_ERROR_MEMORY_UNMAP_FAILED);
148 STR(VK_ERROR_INCOMPATIBLE_DEVICE);
149 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
150 STR(VK_ERROR_INCOMPLETE_COMMAND_BUFFER);
151 STR(VK_ERROR_BUILDING_COMMAND_BUFFER);
152 STR(VK_ERROR_MEMORY_NOT_BOUND);
153 STR(VK_ERROR_INCOMPATIBLE_QUEUE);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800154#undef STR
155 default: return "UNKNOWN_RESULT";
156 }
157}
Chia-I Wud4bae362014-07-29 11:15:00 +0800158
Tony Barbour8205d902015-04-16 15:59:00 -0600159static const char *vk_physical_device_type_string(VkPhysicalDeviceType type)
Chia-I Wud4bae362014-07-29 11:15:00 +0800160{
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800161 switch (type) {
Tony Barbour8205d902015-04-16 15:59:00 -0600162#define STR(r) case VK_PHYSICAL_DEVICE_TYPE_ ##r: return #r
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800163 STR(OTHER);
Tony Barbour8205d902015-04-16 15:59:00 -0600164 STR(INTEGRATED_GPU);
165 STR(DISCRETE_GPU);
166 STR(VIRTUAL_GPU);
Chia-I Wud4bae362014-07-29 11:15:00 +0800167#undef STR
Tony Barbour8205d902015-04-16 15:59:00 -0600168 default: return "UNKNOWN_DEVICE";
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800169 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800170}
171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600172static const char *vk_format_string(VkFormat fmt)
Chia-I Wud4bae362014-07-29 11:15:00 +0800173{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700174 switch (fmt) {
Tony Barbour8205d902015-04-16 15:59:00 -0600175#define STR(r) case VK_FORMAT_ ##r: return #r
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800176 STR(UNDEFINED);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700177 STR(R4G4_UNORM);
178 STR(R4G4_USCALED);
179 STR(R4G4B4A4_UNORM);
180 STR(R4G4B4A4_USCALED);
181 STR(R5G6B5_UNORM);
182 STR(R5G6B5_USCALED);
183 STR(R5G5B5A1_UNORM);
184 STR(R5G5B5A1_USCALED);
185 STR(R8_UNORM);
186 STR(R8_SNORM);
187 STR(R8_USCALED);
188 STR(R8_SSCALED);
189 STR(R8_UINT);
190 STR(R8_SINT);
191 STR(R8_SRGB);
192 STR(R8G8_UNORM);
193 STR(R8G8_SNORM);
194 STR(R8G8_USCALED);
195 STR(R8G8_SSCALED);
196 STR(R8G8_UINT);
197 STR(R8G8_SINT);
198 STR(R8G8_SRGB);
199 STR(R8G8B8_UNORM);
200 STR(R8G8B8_SNORM);
201 STR(R8G8B8_USCALED);
202 STR(R8G8B8_SSCALED);
203 STR(R8G8B8_UINT);
204 STR(R8G8B8_SINT);
205 STR(R8G8B8_SRGB);
206 STR(R8G8B8A8_UNORM);
207 STR(R8G8B8A8_SNORM);
208 STR(R8G8B8A8_USCALED);
209 STR(R8G8B8A8_SSCALED);
210 STR(R8G8B8A8_UINT);
211 STR(R8G8B8A8_SINT);
212 STR(R8G8B8A8_SRGB);
213 STR(R10G10B10A2_UNORM);
214 STR(R10G10B10A2_SNORM);
215 STR(R10G10B10A2_USCALED);
216 STR(R10G10B10A2_SSCALED);
217 STR(R10G10B10A2_UINT);
218 STR(R10G10B10A2_SINT);
219 STR(R16_UNORM);
220 STR(R16_SNORM);
221 STR(R16_USCALED);
222 STR(R16_SSCALED);
223 STR(R16_UINT);
224 STR(R16_SINT);
225 STR(R16_SFLOAT);
226 STR(R16G16_UNORM);
227 STR(R16G16_SNORM);
228 STR(R16G16_USCALED);
229 STR(R16G16_SSCALED);
230 STR(R16G16_UINT);
231 STR(R16G16_SINT);
232 STR(R16G16_SFLOAT);
233 STR(R16G16B16_UNORM);
234 STR(R16G16B16_SNORM);
235 STR(R16G16B16_USCALED);
236 STR(R16G16B16_SSCALED);
237 STR(R16G16B16_UINT);
238 STR(R16G16B16_SINT);
239 STR(R16G16B16_SFLOAT);
240 STR(R16G16B16A16_UNORM);
241 STR(R16G16B16A16_SNORM);
242 STR(R16G16B16A16_USCALED);
243 STR(R16G16B16A16_SSCALED);
244 STR(R16G16B16A16_UINT);
245 STR(R16G16B16A16_SINT);
246 STR(R16G16B16A16_SFLOAT);
247 STR(R32_UINT);
248 STR(R32_SINT);
249 STR(R32_SFLOAT);
250 STR(R32G32_UINT);
251 STR(R32G32_SINT);
252 STR(R32G32_SFLOAT);
253 STR(R32G32B32_UINT);
254 STR(R32G32B32_SINT);
255 STR(R32G32B32_SFLOAT);
256 STR(R32G32B32A32_UINT);
257 STR(R32G32B32A32_SINT);
258 STR(R32G32B32A32_SFLOAT);
259 STR(R64_SFLOAT);
260 STR(R64G64_SFLOAT);
261 STR(R64G64B64_SFLOAT);
262 STR(R64G64B64A64_SFLOAT);
263 STR(R11G11B10_UFLOAT);
264 STR(R9G9B9E5_UFLOAT);
265 STR(D16_UNORM);
266 STR(D24_UNORM);
267 STR(D32_SFLOAT);
268 STR(S8_UINT);
269 STR(D16_UNORM_S8_UINT);
270 STR(D24_UNORM_S8_UINT);
271 STR(D32_SFLOAT_S8_UINT);
Courtney Goeltzenleuchterfe8a2e12015-03-03 11:30:36 -0700272 STR(BC1_RGB_UNORM);
273 STR(BC1_RGB_SRGB);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700274 STR(BC2_UNORM);
275 STR(BC2_SRGB);
276 STR(BC3_UNORM);
277 STR(BC3_SRGB);
278 STR(BC4_UNORM);
279 STR(BC4_SNORM);
280 STR(BC5_UNORM);
281 STR(BC5_SNORM);
282 STR(BC6H_UFLOAT);
283 STR(BC6H_SFLOAT);
284 STR(BC7_UNORM);
285 STR(BC7_SRGB);
286 STR(ETC2_R8G8B8_UNORM);
287 STR(ETC2_R8G8B8A1_UNORM);
288 STR(ETC2_R8G8B8A8_UNORM);
289 STR(EAC_R11_UNORM);
290 STR(EAC_R11_SNORM);
291 STR(EAC_R11G11_UNORM);
292 STR(EAC_R11G11_SNORM);
293 STR(ASTC_4x4_UNORM);
294 STR(ASTC_4x4_SRGB);
Courtney Goeltzenleuchterfe8a2e12015-03-03 11:30:36 -0700295 STR(ASTC_5x4_UNORM);
296 STR(ASTC_5x4_SRGB);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700297 STR(ASTC_5x5_UNORM);
298 STR(ASTC_5x5_SRGB);
299 STR(ASTC_6x5_UNORM);
300 STR(ASTC_6x5_SRGB);
301 STR(ASTC_6x6_UNORM);
302 STR(ASTC_6x6_SRGB);
303 STR(ASTC_8x5_UNORM);
304 STR(ASTC_8x5_SRGB);
305 STR(ASTC_8x6_UNORM);
306 STR(ASTC_8x6_SRGB);
307 STR(ASTC_8x8_UNORM);
308 STR(ASTC_8x8_SRGB);
309 STR(ASTC_10x5_UNORM);
310 STR(ASTC_10x5_SRGB);
311 STR(ASTC_10x6_UNORM);
312 STR(ASTC_10x6_SRGB);
313 STR(ASTC_10x8_UNORM);
314 STR(ASTC_10x8_SRGB);
315 STR(ASTC_10x10_UNORM);
316 STR(ASTC_10x10_SRGB);
317 STR(ASTC_12x10_UNORM);
318 STR(ASTC_12x10_SRGB);
319 STR(ASTC_12x12_UNORM);
320 STR(ASTC_12x12_SRGB);
321 STR(B5G6R5_UNORM);
322 STR(B5G6R5_USCALED);
323 STR(B8G8R8_UNORM);
324 STR(B8G8R8_SNORM);
325 STR(B8G8R8_USCALED);
326 STR(B8G8R8_SSCALED);
327 STR(B8G8R8_UINT);
328 STR(B8G8R8_SINT);
329 STR(B8G8R8_SRGB);
330 STR(B8G8R8A8_UNORM);
331 STR(B8G8R8A8_SNORM);
332 STR(B8G8R8A8_USCALED);
333 STR(B8G8R8A8_SSCALED);
334 STR(B8G8R8A8_UINT);
335 STR(B8G8R8A8_SINT);
336 STR(B8G8R8A8_SRGB);
337 STR(B10G10R10A2_UNORM);
338 STR(B10G10R10A2_SNORM);
339 STR(B10G10R10A2_USCALED);
340 STR(B10G10R10A2_SSCALED);
341 STR(B10G10R10A2_UINT);
342 STR(B10G10R10A2_SINT);
Chia-I Wud4bae362014-07-29 11:15:00 +0800343#undef STR
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700344 default: return "UNKNOWN_FORMAT";
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800345 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800346}
347
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800348static void app_dev_init_formats(struct app_dev *dev)
349{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600350 VkFormat f;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800351
Tony Barbour8205d902015-04-16 15:59:00 -0600352 for (f = 0; f < VK_NUM_FORMAT; f++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600353 const VkFormat fmt = f;
354 VkResult err;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800355
Chris Forbesd7576302015-06-21 22:55:02 +1200356 err = vkGetPhysicalDeviceFormatInfo(dev->gpu->obj, fmt, &dev->format_props[f]);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700357 if (err) {
358 memset(&dev->format_props[f], 0,
359 sizeof(dev->format_props[f]));
360 }
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800361 }
362}
363
364static void app_dev_init(struct app_dev *dev, struct app_gpu *gpu)
365{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600366 VkDeviceCreateInfo info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600367 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800368 .pNext = NULL,
369 .queueRecordCount = 0,
370 .pRequestedQueues = NULL,
371 .extensionCount = 0,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600372 .pEnabledExtensions = NULL,
Jon Ashburn80d3b712015-05-18 09:06:15 -0600373 .flags = 0,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800374 };
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600375 VkResult U_ASSERT_ONLY err;
376 // Extensions to enable
377 VkExtensionProperties *enable_extension_list;
378 static char *known_extensions[] = {
Courtney Goeltzenleuchtere880e9b2015-06-25 16:23:45 -0600379 "VK_WSI_LunarG",
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600380 "Validation",
381 };
Tony Barbour426b9052015-06-24 16:06:58 -0600382
383 uint32_t extCount;
384 err = vkGetPhysicalDeviceExtensionCount(gpu->obj, &extCount);
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600385 assert(!err);
386
387 enable_extension_list = malloc(sizeof(VkExtensionProperties) * extCount);
388 if (!enable_extension_list) {
389 ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
390 }
391
Courtney Goeltzenleuchter9e42b882015-06-25 16:24:36 -0600392 fflush(stdout);
393
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600394 VkExtensionProperties extProp;
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600395 gpu->device_extension_count = 0;
396 bool32_t U_ASSERT_ONLY extFound = 0; // TODO : Need to enhance this if/when we enable multiple extensions
397 for (uint32_t i = 0; i < ARRAY_SIZE(known_extensions); i++) {
398 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour426b9052015-06-24 16:06:58 -0600399 err = vkGetPhysicalDeviceExtensionProperties(
400 gpu->obj, j, &extProp);
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600401 if (!strcmp(known_extensions[i], extProp.name)) {
402 extFound = 1;
403 memcpy(&enable_extension_list[gpu->device_extension_count], &extProp, sizeof(extProp));
404 gpu->device_extension_count++;
Courtney Goeltzenleuchter9e42b882015-06-25 16:24:36 -0600405 printf("%s: %s\n", extProp.name, extProp.description);
406 fflush(stdout);
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600407 }
408 }
409 }
410 assert(extFound);
411
412 gpu->device_extensions = enable_extension_list;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800413
414 /* request all queues */
415 info.queueRecordCount = gpu->queue_count;
416 info.pRequestedQueues = gpu->queue_reqs;
417
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600418 info.extensionCount = extCount;
419 info.pEnabledExtensions = enable_extension_list;
420 dev->gpu = gpu;
421 err = vkCreateDevice(gpu->obj, &info, &dev->obj);
422 if (err)
423 ERR_EXIT(err);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800424
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800425}
426
427static void app_dev_destroy(struct app_dev *dev)
428{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600429 vkDestroyDevice(dev->obj);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800430}
431
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600432static void app_create_instance(struct app_instance *inst)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800433{
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600434 const VkApplicationInfo app_info = {
435 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
436 .pNext = NULL,
437 .pAppName = APP_SHORT_NAME,
438 .appVersion = 1,
439 .pEngineName = APP_SHORT_NAME,
440 .engineVersion = 1,
441 .apiVersion = VK_API_VERSION,
442 };
443 VkInstanceCreateInfo inst_info = {
444 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
445 .pNext = NULL,
446 .pAppInfo = &app_info,
447 .pAllocCb = NULL,
448 .extensionCount = 0,
449 .pEnabledExtensions = NULL,
450 };
Tony Barbour22a30862015-04-22 09:02:32 -0600451 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600452 // Global Extensions to enable
Ian Elliottaae1a572015-02-04 16:48:37 -0700453 static char *known_extensions[] = {
Tobin Ehlis97b4c5d2015-04-17 14:05:23 -0600454 "VK_WSI_LunarG",
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800455 };
Tony Barbour426b9052015-06-24 16:06:58 -0600456
Tobin Ehlis3536b442015-04-16 18:04:57 -0600457 uint32_t extCount = 0;
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600458 VkExtensionProperties extProp;
459 VkExtensionProperties *enable_extension_list;
460 uint32_t global_extension_count = 0;
461
Tony Barbour426b9052015-06-24 16:06:58 -0600462 err = vkGetGlobalExtensionCount(&extCount);
Tobin Ehlis3536b442015-04-16 18:04:57 -0600463 assert(!err);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800464
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600465 enable_extension_list = malloc(sizeof(VkExtensionProperties) * extCount);
466 if (!enable_extension_list) {
467 ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
468 }
469
Tony Barbour22a30862015-04-22 09:02:32 -0600470 bool32_t U_ASSERT_ONLY extFound = 0; // TODO : Need to enhance this if/when we enable multiple extensions
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600471 for (uint32_t i = 0; i < ARRAY_SIZE(known_extensions); i++) {
472 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour426b9052015-06-24 16:06:58 -0600473 err = vkGetGlobalExtensionProperties(j, &extProp);
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600474 if (!strcmp(known_extensions[i], extProp.name)) {
475 extFound = 1;
476 memcpy(&enable_extension_list[global_extension_count], &extProp, sizeof(extProp));
477 global_extension_count++;
478 }
Tobin Ehlis3536b442015-04-16 18:04:57 -0600479 }
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800480 }
Tobin Ehlis3536b442015-04-16 18:04:57 -0600481 assert(extFound);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800482
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600483 inst_info.extensionCount = global_extension_count;
484 inst_info.pEnabledExtensions = enable_extension_list;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800485
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600486 err = vkCreateInstance(&inst_info, &inst->instance);
487 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
488 printf("Cannot create Vulkan instance.\n");
489 ERR_EXIT(err);
490 } else if (err) {
491 ERR_EXIT(err);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800492 }
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600493
494 inst->global_extension_count = global_extension_count;
495 inst->global_extensions = enable_extension_list;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800496}
497
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600498static void app_destroy_instance(struct app_instance *inst)
499{
500 free(inst->global_extensions);
501 vkDestroyInstance(inst->instance);
502}
503
504
Tony Barbour8205d902015-04-16 15:59:00 -0600505static void app_gpu_init(struct app_gpu *gpu, uint32_t id, VkPhysicalDevice obj)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800506{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600507 VkResult err;
Ian Elliottaae1a572015-02-04 16:48:37 -0700508 uint32_t i;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800509
510 memset(gpu, 0, sizeof(*gpu));
511
512 gpu->id = id;
513 gpu->obj = obj;
Tony Barbour426b9052015-06-24 16:06:58 -0600514
515 err = vkGetPhysicalDeviceProperties(gpu->obj, &gpu->props);
516 if (err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800517 ERR_EXIT(err);
518
Tony Barbour426b9052015-06-24 16:06:58 -0600519 err = vkGetPhysicalDevicePerformance(gpu->obj, &gpu->perf);
520 if (err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800521 ERR_EXIT(err);
522
523 /* get queue count */
Tony Barbour426b9052015-06-24 16:06:58 -0600524 err = vkGetPhysicalDeviceQueueCount(gpu->obj, &gpu->queue_count);
525 if (err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800526 ERR_EXIT(err);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800527
528 gpu->queue_props =
529 malloc(sizeof(gpu->queue_props[0]) * gpu->queue_count);
Tony Barbour426b9052015-06-24 16:06:58 -0600530
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800531 if (!gpu->queue_props)
Tony Barbour8205d902015-04-16 15:59:00 -0600532 ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
Tony Barbour426b9052015-06-24 16:06:58 -0600533 err = vkGetPhysicalDeviceQueueProperties(gpu->obj, gpu->queue_count, gpu->queue_props);
534 if (err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800535 ERR_EXIT(err);
536
537 /* set up queue requests */
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800538 gpu->queue_reqs = malloc(sizeof(*gpu->queue_reqs) * gpu->queue_count);
539 if (!gpu->queue_reqs)
Tony Barbour8205d902015-04-16 15:59:00 -0600540 ERR_EXIT(VK_ERROR_OUT_OF_HOST_MEMORY);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800541 for (i = 0; i < gpu->queue_count; i++) {
542 gpu->queue_reqs[i].queueNodeIndex = i;
543 gpu->queue_reqs[i].queueCount = gpu->queue_props[i].queueCount;
544 }
545
Tony Barbour426b9052015-06-24 16:06:58 -0600546 err = vkGetPhysicalDeviceMemoryProperties(gpu->obj, &gpu->memory_props);
547 if (err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800548 ERR_EXIT(err);
549
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800550 app_dev_init(&gpu->dev, gpu);
551 app_dev_init_formats(&gpu->dev);
552}
553
554static void app_gpu_destroy(struct app_gpu *gpu)
555{
556 app_dev_destroy(&gpu->dev);
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600557 free(gpu->device_extensions);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800558 free(gpu->queue_reqs);
559 free(gpu->queue_props);
560}
561
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600562static void app_dev_dump_format_props(const struct app_dev *dev, VkFormat fmt)
Chia-I Wud4bae362014-07-29 11:15:00 +0800563{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600564 const VkFormatProperties *props = &dev->format_props[fmt];
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800565 struct {
566 const char *name;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600567 VkFlags flags;
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800568 } tilings[2];
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600569 uint32_t i;
Chia-I Wud4bae362014-07-29 11:15:00 +0800570
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800571 if (!props->linearTilingFeatures && !props->optimalTilingFeatures)
572 return;
Chia-I Wud4bae362014-07-29 11:15:00 +0800573
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800574 tilings[0].name = "linear";
575 tilings[0].flags = props->linearTilingFeatures;
576 tilings[1].name = "optimal";
577 tilings[1].flags = props->optimalTilingFeatures;
Chia-I Wud4bae362014-07-29 11:15:00 +0800578
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600579 printf("FORMAT_%s\n", vk_format_string(fmt));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800580 for (i = 0; i < ARRAY_SIZE(tilings); i++) {
581 if (!tilings[i].flags)
582 continue;
Chia-I Wud4bae362014-07-29 11:15:00 +0800583
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800584 printf("\t%s tiling image =%s%s%s\n", tilings[i].name,
Tony Barbour8205d902015-04-16 15:59:00 -0600585 (tilings[i].flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) ? " sampled" : "",
586 (tilings[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) ? " storage" : "",
587 (tilings[i].flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT) ? " atomic" : "");
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600588 printf("\t%s tiling texel =%s%s%s\n", tilings[i].name,
Tony Barbour8205d902015-04-16 15:59:00 -0600589 (tilings[i].flags & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT) ? " TBO" : "",
590 (tilings[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT) ? " IBO" : "",
591 (tilings[i].flags & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT) ? " atomic" : "");
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600592 printf("\t%s tiling attachment =%s%s%s\n", tilings[i].name,
Tony Barbour8205d902015-04-16 15:59:00 -0600593 (tilings[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) ? " color" : "",
594 (tilings[i].flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT) ? " blend" : "",
595 (tilings[i].flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) ? " depth/stencil" : "");
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600596 printf("\t%s tiling vertex = %u\n", tilings[i].name,
Tony Barbour8205d902015-04-16 15:59:00 -0600597 (bool) (tilings[i].flags & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800598 printf("\t%s tiling conversion = %u\n", tilings[i].name,
Tony Barbour8205d902015-04-16 15:59:00 -0600599 (bool) (tilings[i].flags & VK_FORMAT_FEATURE_CONVERSION_BIT));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800600 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800601}
602
Chia-I Wud4bae362014-07-29 11:15:00 +0800603
604static void
605app_dev_dump(const struct app_dev *dev)
606{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600607 VkFormat fmt;
Chia-I Wud4bae362014-07-29 11:15:00 +0800608
Tony Barbour8205d902015-04-16 15:59:00 -0600609 for (fmt = 0; fmt < VK_NUM_FORMAT; fmt++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700610 app_dev_dump_format_props(dev, fmt);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800611 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800612}
613
Mike Stroyan009d8ca2015-05-27 13:09:15 -0600614#ifdef _WIN32
615#define PRINTF_SIZE_T_SPECIFIER "%Iu"
616#else
617#define PRINTF_SIZE_T_SPECIFIER "%zu"
618#endif
619
Chia-I Wud4bae362014-07-29 11:15:00 +0800620static void app_gpu_dump_props(const struct app_gpu *gpu)
621{
Tony Barbour8205d902015-04-16 15:59:00 -0600622 const VkPhysicalDeviceProperties *props = &gpu->props;
Chia-I Wud4bae362014-07-29 11:15:00 +0800623
Tony Barbour8205d902015-04-16 15:59:00 -0600624 printf("VkPhysicalDeviceProperties\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800625 printf("\tapiVersion = %u\n", props->apiVersion);
626 printf("\tdriverVersion = %u\n", props->driverVersion);
627 printf("\tvendorId = 0x%04x\n", props->vendorId);
628 printf("\tdeviceId = 0x%04x\n", props->deviceId);
Tony Barbour8205d902015-04-16 15:59:00 -0600629 printf("\tdeviceType = %s\n", vk_physical_device_type_string(props->deviceType));
630 printf("\tdeviceName = %s\n", props->deviceName);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800631 printf("\tmaxBoundDescriptorSets = %u\n", props->maxBoundDescriptorSets);
632 printf("\tmaxThreadGroupSize = %u\n", props->maxThreadGroupSize);
633 printf("\ttimestampFrequency = %lu\n", props->timestampFrequency);
634 printf("\tmultiColorAttachmentClears = %u\n", props->multiColorAttachmentClears);
Chia-I Wud4bae362014-07-29 11:15:00 +0800635}
636
637static void app_gpu_dump_perf(const struct app_gpu *gpu)
638{
Tony Barbour8205d902015-04-16 15:59:00 -0600639 const VkPhysicalDevicePerformance *perf = &gpu->perf;
Chia-I Wud4bae362014-07-29 11:15:00 +0800640
Tony Barbour8205d902015-04-16 15:59:00 -0600641 printf("VkPhysicalDevicePerformance\n");
642 printf("\tmaxGpuClock = %f\n", perf->maxDeviceClock);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800643 printf("\taluPerClock = %f\n", perf->aluPerClock);
644 printf("\ttexPerClock = %f\n", perf->texPerClock);
645 printf("\tprimsPerClock = %f\n", perf->primsPerClock);
646 printf("\tpixelsPerClock = %f\n", perf->pixelsPerClock);
Chia-I Wud4bae362014-07-29 11:15:00 +0800647}
648
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600649static void app_gpu_dump_instance_extensions(const struct app_instance *inst)
650{
651 uint32_t i;
652 printf("Extensions");
653 printf("\tcount = %d\n", inst->global_extension_count);
654 printf("\t");
655 for (i=0; i< inst->global_extension_count; i++) {
656 if (i>0)
657 printf(", "); // separator between extension names
658 printf("%s", inst->global_extensions[i].name);
659 }
660 printf("\n");
661}
662
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600663static void app_gpu_dump_extensions(const struct app_gpu *gpu)
664{
Ian Elliottaae1a572015-02-04 16:48:37 -0700665 uint32_t i;
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600666 printf("Extensions");
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600667 printf("\tcount = %d\n", gpu->device_extension_count);
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600668 printf("\t");
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600669 for (i=0; i< gpu->device_extension_count; i++) {
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600670 if (i>0)
671 printf(", "); // separator between extension names
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600672 printf("%s(%d): %s", gpu->device_extensions[i].name,
673 gpu->device_extensions[i].version,
674 gpu->device_extensions[i].description);
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600675 }
676 printf("\n");
677}
678
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600679static void app_gpu_dump_queue_props(const struct app_gpu *gpu, uint32_t id)
Chia-I Wud4bae362014-07-29 11:15:00 +0800680{
Tony Barbour8205d902015-04-16 15:59:00 -0600681 const VkPhysicalDeviceQueueProperties *props = &gpu->queue_props[id];
Chia-I Wud4bae362014-07-29 11:15:00 +0800682
Tony Barbour8205d902015-04-16 15:59:00 -0600683 printf("VkPhysicalDeviceQueueProperties[%d]\n", id);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800684 printf("\tqueueFlags = %c%c%c%c\n",
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 (props->queueFlags & VK_QUEUE_GRAPHICS_BIT) ? 'G' : '.',
686 (props->queueFlags & VK_QUEUE_COMPUTE_BIT) ? 'C' : '.',
687 (props->queueFlags & VK_QUEUE_DMA_BIT) ? 'D' : '.',
688 (props->queueFlags & VK_QUEUE_EXTENDED_BIT) ? 'X' : '.');
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800689 printf("\tqueueCount = %u\n", props->queueCount);
690 printf("\tmaxAtomicCounters = %u\n", props->maxAtomicCounters);
691 printf("\tsupportsTimestamps = %u\n", props->supportsTimestamps);
Chia-I Wud4bae362014-07-29 11:15:00 +0800692}
693
694static void app_gpu_dump_memory_props(const struct app_gpu *gpu)
695{
Tony Barbour8205d902015-04-16 15:59:00 -0600696 const VkPhysicalDeviceMemoryProperties *props = &gpu->memory_props;
Chia-I Wud4bae362014-07-29 11:15:00 +0800697
Tony Barbour8205d902015-04-16 15:59:00 -0600698 printf("VkPhysicalDeviceMemoryProperties\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800699 printf("\tsupportsMigration = %u\n", props->supportsMigration);
Chia-I Wud4bae362014-07-29 11:15:00 +0800700}
701
702static void app_gpu_dump(const struct app_gpu *gpu)
703{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600704 uint32_t i;
Chia-I Wuf5c46f42014-08-05 15:33:40 +0800705
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800706 printf("GPU%u\n", gpu->id);
707 app_gpu_dump_props(gpu);
708 printf("\n");
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600709 app_gpu_dump_extensions(gpu);
710 printf("\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800711 app_gpu_dump_perf(gpu);
712 printf("\n");
713 for (i = 0; i < gpu->queue_count; i++) {
714 app_gpu_dump_queue_props(gpu, i);
715 printf("\n");
716 }
717 app_gpu_dump_memory_props(gpu);
718 printf("\n");
719 app_dev_dump(&gpu->dev);
Chia-I Wud4bae362014-07-29 11:15:00 +0800720}
721
Chia-I Wud4bae362014-07-29 11:15:00 +0800722int main(int argc, char **argv)
723{
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800724 struct app_gpu gpus[MAX_GPUS];
Tony Barbour8205d902015-04-16 15:59:00 -0600725 VkPhysicalDevice objs[MAX_GPUS];
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600726 uint32_t gpu_count, i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600727 VkResult err;
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600728 struct app_instance inst;
Chia-I Wud4bae362014-07-29 11:15:00 +0800729
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600730 app_create_instance(&inst);
731 app_gpu_dump_instance_extensions(&inst);
Jon Ashburn29669a42015-04-04 14:52:07 -0600732
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600733 err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, NULL);
Jon Ashburn07b309a2015-04-15 11:31:12 -0600734 if (err)
735 ERR_EXIT(err);
736 if (gpu_count > MAX_GPUS) {
David Pinedo18fb9232015-04-21 14:45:16 -0600737 printf("Too many GPUS found \n");
738 ERR_EXIT(VK_ERROR_UNKNOWN);
Jon Ashburn07b309a2015-04-15 11:31:12 -0600739 }
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600740 err = vkEnumeratePhysicalDevices(inst.instance, &gpu_count, objs);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800741 if (err)
742 ERR_EXIT(err);
Chia-I Wud4bae362014-07-29 11:15:00 +0800743
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800744 for (i = 0; i < gpu_count; i++) {
745 app_gpu_init(&gpus[i], i, objs[i]);
746 app_gpu_dump(&gpus[i]);
747 printf("\n\n");
748 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800749
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800750 for (i = 0; i < gpu_count; i++)
751 app_gpu_destroy(&gpus[i]);
Chia-I Wud4bae362014-07-29 11:15:00 +0800752
Courtney Goeltzenleuchter3c1ccf52015-06-04 16:20:06 -0600753 app_destroy_instance(&inst);
Chia-I Wu0b9a7372014-08-06 12:09:19 +0800754
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800755 return 0;
Chia-I Wud4bae362014-07-29 11:15:00 +0800756}
Ian Elliottea95f5c2015-04-17 21:23:34 -0600757
758#ifdef _WIN32
David Pinedo18fb9232015-04-21 14:45:16 -0600759
760// Create a console window with a large scrollback size to which to send stdout.
761// Returns true if console window was successfully created, false otherwise.
762bool SetStdOutToNewConsole()
763{
764 // don't do anything if we already have a console
765 if (GetStdHandle(STD_OUTPUT_HANDLE))
766 return false;
767
768 // allocate a console for this app
769 AllocConsole();
770
771 // redirect unbuffered STDOUT to the console
772 HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
773 int fileDescriptor = _open_osfhandle((intptr_t)consoleHandle, _O_TEXT);
774 FILE *fp = _fdopen( fileDescriptor, "w" );
775 *stdout = *fp;
776 setvbuf( stdout, NULL, _IONBF, 0 );
777
778 // make the console window bigger
779 CONSOLE_SCREEN_BUFFER_INFO csbi;
780 SMALL_RECT r;
781 COORD bufferSize;
782 if (!GetConsoleScreenBufferInfo(consoleHandle, &csbi))
783 return false;
784 bufferSize.X = csbi.dwSize.X;
785 bufferSize.Y = 1000;
786 if (!SetConsoleScreenBufferSize(consoleHandle, bufferSize))
787 return false;
788 r.Left = r.Top = 0;
789 r.Right = csbi.dwSize.X-1;
790 r.Bottom = 60;
791 if (!SetConsoleWindowInfo(consoleHandle, true, &r))
792 return false;
793
794 // change the console window title
Ian Elliott4e19ed02015-04-28 10:52:52 -0600795 if (!SetConsoleTitle(TEXT(APP_SHORT_NAME)))
David Pinedo18fb9232015-04-21 14:45:16 -0600796 return false;
797
798 return true;
799}
800
Ian Elliottea95f5c2015-04-17 21:23:34 -0600801int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
802{
803 char *argv = pCmdLine;
David Pinedo18fb9232015-04-21 14:45:16 -0600804 consoleCreated = SetStdOutToNewConsole();
Ian Elliottea95f5c2015-04-17 21:23:34 -0600805 main(1, &argv);
806 fflush(stdout);
David Pinedo18fb9232015-04-21 14:45:16 -0600807 if (consoleCreated)
808 Sleep(INFINITE);
Ian Elliottea95f5c2015-04-17 21:23:34 -0600809}
810#endif