blob: 80f238a6e68e8bce588e019ee836ae085599a528 [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
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060030#include <vulkan.h>
Chia-I Wu46c29dd2014-12-02 21:09:20 +080031
32#define ERR(err) printf("%s:%d: failed with %s\n", \
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060033 __FILE__, __LINE__, vk_result_string(err));
Chia-I Wu46c29dd2014-12-02 21:09:20 +080034
35#define ERR_EXIT(err) do { ERR(err); exit(-1); } while (0)
36
37#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
38
39#define MAX_GPUS 8
40
41#define MAX_QUEUE_TYPES 5
42
43struct app_gpu;
44
45struct app_dev {
46 struct app_gpu *gpu; /* point back to the GPU */
47
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048 VK_DEVICE obj;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080049
Chia-I Wu46c29dd2014-12-02 21:09:20 +080050
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060051 VK_FORMAT_PROPERTIES format_props[VK_NUM_FMT];
Chia-I Wu46c29dd2014-12-02 21:09:20 +080052};
53
54struct app_gpu {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060055 uint32_t id;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060056 VK_PHYSICAL_GPU obj;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080057
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060058 VK_PHYSICAL_GPU_PROPERTIES props;
59 VK_PHYSICAL_GPU_PERFORMANCE perf;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080060
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060061 uint32_t queue_count;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060062 VK_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props;
63 VK_DEVICE_QUEUE_CREATE_INFO *queue_reqs;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080064
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060065 VK_PHYSICAL_GPU_MEMORY_PROPERTIES memory_props;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080066
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060067 uint32_t extension_count;
Ian Elliottaae1a572015-02-04 16:48:37 -070068 char **extensions;
Chia-I Wu46c29dd2014-12-02 21:09:20 +080069
70 struct app_dev dev;
71};
72
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060073static const char *vk_result_string(VK_RESULT err)
Chia-I Wu46c29dd2014-12-02 21:09:20 +080074{
75 switch (err) {
76#define STR(r) case r: return #r
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060077 STR(VK_SUCCESS);
78 STR(VK_UNSUPPORTED);
79 STR(VK_NOT_READY);
80 STR(VK_TIMEOUT);
81 STR(VK_EVENT_SET);
82 STR(VK_EVENT_RESET);
83 STR(VK_ERROR_UNKNOWN);
84 STR(VK_ERROR_UNAVAILABLE);
85 STR(VK_ERROR_INITIALIZATION_FAILED);
86 STR(VK_ERROR_OUT_OF_MEMORY);
87 STR(VK_ERROR_OUT_OF_GPU_MEMORY);
88 STR(VK_ERROR_DEVICE_ALREADY_CREATED);
89 STR(VK_ERROR_DEVICE_LOST);
90 STR(VK_ERROR_INVALID_POINTER);
91 STR(VK_ERROR_INVALID_VALUE);
92 STR(VK_ERROR_INVALID_HANDLE);
93 STR(VK_ERROR_INVALID_ORDINAL);
94 STR(VK_ERROR_INVALID_MEMORY_SIZE);
95 STR(VK_ERROR_INVALID_EXTENSION);
96 STR(VK_ERROR_INVALID_FLAGS);
97 STR(VK_ERROR_INVALID_ALIGNMENT);
98 STR(VK_ERROR_INVALID_FORMAT);
99 STR(VK_ERROR_INVALID_IMAGE);
100 STR(VK_ERROR_INVALID_DESCRIPTOR_SET_DATA);
101 STR(VK_ERROR_INVALID_QUEUE_TYPE);
102 STR(VK_ERROR_INVALID_OBJECT_TYPE);
103 STR(VK_ERROR_UNSUPPORTED_SHADER_IL_VERSION);
104 STR(VK_ERROR_BAD_SHADER_CODE);
105 STR(VK_ERROR_BAD_PIPELINE_DATA);
106 STR(VK_ERROR_TOO_MANY_MEMORY_REFERENCES);
107 STR(VK_ERROR_NOT_MAPPABLE);
108 STR(VK_ERROR_MEMORY_MAP_FAILED);
109 STR(VK_ERROR_MEMORY_UNMAP_FAILED);
110 STR(VK_ERROR_INCOMPATIBLE_DEVICE);
111 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
112 STR(VK_ERROR_INCOMPLETE_COMMAND_BUFFER);
113 STR(VK_ERROR_BUILDING_COMMAND_BUFFER);
114 STR(VK_ERROR_MEMORY_NOT_BOUND);
115 STR(VK_ERROR_INCOMPATIBLE_QUEUE);
116 STR(VK_ERROR_NOT_SHAREABLE);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800117#undef STR
118 default: return "UNKNOWN_RESULT";
119 }
120}
Chia-I Wud4bae362014-07-29 11:15:00 +0800121
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600122static const char *vk_gpu_type_string(VK_PHYSICAL_GPU_TYPE type)
Chia-I Wud4bae362014-07-29 11:15:00 +0800123{
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800124 switch (type) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600125#define STR(r) case VK_GPU_TYPE_ ##r: return #r
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800126 STR(OTHER);
127 STR(INTEGRATED);
128 STR(DISCRETE);
129 STR(VIRTUAL);
Chia-I Wud4bae362014-07-29 11:15:00 +0800130#undef STR
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800131 default: return "UNKNOWN_GPU";
132 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800133}
134
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600135static const char *vk_format_string(VK_FORMAT fmt)
Chia-I Wud4bae362014-07-29 11:15:00 +0800136{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700137 switch (fmt) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600138#define STR(r) case VK_FMT_ ##r: return #r
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800139 STR(UNDEFINED);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700140 STR(R4G4_UNORM);
141 STR(R4G4_USCALED);
142 STR(R4G4B4A4_UNORM);
143 STR(R4G4B4A4_USCALED);
144 STR(R5G6B5_UNORM);
145 STR(R5G6B5_USCALED);
146 STR(R5G5B5A1_UNORM);
147 STR(R5G5B5A1_USCALED);
148 STR(R8_UNORM);
149 STR(R8_SNORM);
150 STR(R8_USCALED);
151 STR(R8_SSCALED);
152 STR(R8_UINT);
153 STR(R8_SINT);
154 STR(R8_SRGB);
155 STR(R8G8_UNORM);
156 STR(R8G8_SNORM);
157 STR(R8G8_USCALED);
158 STR(R8G8_SSCALED);
159 STR(R8G8_UINT);
160 STR(R8G8_SINT);
161 STR(R8G8_SRGB);
162 STR(R8G8B8_UNORM);
163 STR(R8G8B8_SNORM);
164 STR(R8G8B8_USCALED);
165 STR(R8G8B8_SSCALED);
166 STR(R8G8B8_UINT);
167 STR(R8G8B8_SINT);
168 STR(R8G8B8_SRGB);
169 STR(R8G8B8A8_UNORM);
170 STR(R8G8B8A8_SNORM);
171 STR(R8G8B8A8_USCALED);
172 STR(R8G8B8A8_SSCALED);
173 STR(R8G8B8A8_UINT);
174 STR(R8G8B8A8_SINT);
175 STR(R8G8B8A8_SRGB);
176 STR(R10G10B10A2_UNORM);
177 STR(R10G10B10A2_SNORM);
178 STR(R10G10B10A2_USCALED);
179 STR(R10G10B10A2_SSCALED);
180 STR(R10G10B10A2_UINT);
181 STR(R10G10B10A2_SINT);
182 STR(R16_UNORM);
183 STR(R16_SNORM);
184 STR(R16_USCALED);
185 STR(R16_SSCALED);
186 STR(R16_UINT);
187 STR(R16_SINT);
188 STR(R16_SFLOAT);
189 STR(R16G16_UNORM);
190 STR(R16G16_SNORM);
191 STR(R16G16_USCALED);
192 STR(R16G16_SSCALED);
193 STR(R16G16_UINT);
194 STR(R16G16_SINT);
195 STR(R16G16_SFLOAT);
196 STR(R16G16B16_UNORM);
197 STR(R16G16B16_SNORM);
198 STR(R16G16B16_USCALED);
199 STR(R16G16B16_SSCALED);
200 STR(R16G16B16_UINT);
201 STR(R16G16B16_SINT);
202 STR(R16G16B16_SFLOAT);
203 STR(R16G16B16A16_UNORM);
204 STR(R16G16B16A16_SNORM);
205 STR(R16G16B16A16_USCALED);
206 STR(R16G16B16A16_SSCALED);
207 STR(R16G16B16A16_UINT);
208 STR(R16G16B16A16_SINT);
209 STR(R16G16B16A16_SFLOAT);
210 STR(R32_UINT);
211 STR(R32_SINT);
212 STR(R32_SFLOAT);
213 STR(R32G32_UINT);
214 STR(R32G32_SINT);
215 STR(R32G32_SFLOAT);
216 STR(R32G32B32_UINT);
217 STR(R32G32B32_SINT);
218 STR(R32G32B32_SFLOAT);
219 STR(R32G32B32A32_UINT);
220 STR(R32G32B32A32_SINT);
221 STR(R32G32B32A32_SFLOAT);
222 STR(R64_SFLOAT);
223 STR(R64G64_SFLOAT);
224 STR(R64G64B64_SFLOAT);
225 STR(R64G64B64A64_SFLOAT);
226 STR(R11G11B10_UFLOAT);
227 STR(R9G9B9E5_UFLOAT);
228 STR(D16_UNORM);
229 STR(D24_UNORM);
230 STR(D32_SFLOAT);
231 STR(S8_UINT);
232 STR(D16_UNORM_S8_UINT);
233 STR(D24_UNORM_S8_UINT);
234 STR(D32_SFLOAT_S8_UINT);
Courtney Goeltzenleuchterfe8a2e12015-03-03 11:30:36 -0700235 STR(BC1_RGB_UNORM);
236 STR(BC1_RGB_SRGB);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700237 STR(BC2_UNORM);
238 STR(BC2_SRGB);
239 STR(BC3_UNORM);
240 STR(BC3_SRGB);
241 STR(BC4_UNORM);
242 STR(BC4_SNORM);
243 STR(BC5_UNORM);
244 STR(BC5_SNORM);
245 STR(BC6H_UFLOAT);
246 STR(BC6H_SFLOAT);
247 STR(BC7_UNORM);
248 STR(BC7_SRGB);
249 STR(ETC2_R8G8B8_UNORM);
250 STR(ETC2_R8G8B8A1_UNORM);
251 STR(ETC2_R8G8B8A8_UNORM);
252 STR(EAC_R11_UNORM);
253 STR(EAC_R11_SNORM);
254 STR(EAC_R11G11_UNORM);
255 STR(EAC_R11G11_SNORM);
256 STR(ASTC_4x4_UNORM);
257 STR(ASTC_4x4_SRGB);
Courtney Goeltzenleuchterfe8a2e12015-03-03 11:30:36 -0700258 STR(ASTC_5x4_UNORM);
259 STR(ASTC_5x4_SRGB);
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700260 STR(ASTC_5x5_UNORM);
261 STR(ASTC_5x5_SRGB);
262 STR(ASTC_6x5_UNORM);
263 STR(ASTC_6x5_SRGB);
264 STR(ASTC_6x6_UNORM);
265 STR(ASTC_6x6_SRGB);
266 STR(ASTC_8x5_UNORM);
267 STR(ASTC_8x5_SRGB);
268 STR(ASTC_8x6_UNORM);
269 STR(ASTC_8x6_SRGB);
270 STR(ASTC_8x8_UNORM);
271 STR(ASTC_8x8_SRGB);
272 STR(ASTC_10x5_UNORM);
273 STR(ASTC_10x5_SRGB);
274 STR(ASTC_10x6_UNORM);
275 STR(ASTC_10x6_SRGB);
276 STR(ASTC_10x8_UNORM);
277 STR(ASTC_10x8_SRGB);
278 STR(ASTC_10x10_UNORM);
279 STR(ASTC_10x10_SRGB);
280 STR(ASTC_12x10_UNORM);
281 STR(ASTC_12x10_SRGB);
282 STR(ASTC_12x12_UNORM);
283 STR(ASTC_12x12_SRGB);
284 STR(B5G6R5_UNORM);
285 STR(B5G6R5_USCALED);
286 STR(B8G8R8_UNORM);
287 STR(B8G8R8_SNORM);
288 STR(B8G8R8_USCALED);
289 STR(B8G8R8_SSCALED);
290 STR(B8G8R8_UINT);
291 STR(B8G8R8_SINT);
292 STR(B8G8R8_SRGB);
293 STR(B8G8R8A8_UNORM);
294 STR(B8G8R8A8_SNORM);
295 STR(B8G8R8A8_USCALED);
296 STR(B8G8R8A8_SSCALED);
297 STR(B8G8R8A8_UINT);
298 STR(B8G8R8A8_SINT);
299 STR(B8G8R8A8_SRGB);
300 STR(B10G10R10A2_UNORM);
301 STR(B10G10R10A2_SNORM);
302 STR(B10G10R10A2_USCALED);
303 STR(B10G10R10A2_SSCALED);
304 STR(B10G10R10A2_UINT);
305 STR(B10G10R10A2_SINT);
Chia-I Wud4bae362014-07-29 11:15:00 +0800306#undef STR
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700307 default: return "UNKNOWN_FORMAT";
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800308 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800309}
310
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800311static void app_dev_init_formats(struct app_dev *dev)
312{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600313 VK_FORMAT f;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800314
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600315 for (f = 0; f < VK_NUM_FMT; f++) {
316 const VK_FORMAT fmt = f;
317 VK_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600318 size_t size = sizeof(dev->format_props[f]);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800319
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600320 err = vkGetFormatInfo(dev->obj, fmt,
321 VK_INFO_TYPE_FORMAT_PROPERTIES,
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700322 &size, &dev->format_props[f]);
323 if (err) {
324 memset(&dev->format_props[f], 0,
325 sizeof(dev->format_props[f]));
326 }
327 else if (size != sizeof(dev->format_props[f])) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600328 ERR_EXIT(VK_ERROR_UNKNOWN);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800329 }
330 }
331}
332
333static void app_dev_init(struct app_dev *dev, struct app_gpu *gpu)
334{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 VK_DEVICE_CREATE_INFO info = {
336 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800337 .pNext = NULL,
338 .queueRecordCount = 0,
339 .pRequestedQueues = NULL,
340 .extensionCount = 0,
341 .ppEnabledExtensionNames = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600342 .maxValidationLevel = VK_VALIDATION_LEVEL_END_RANGE,
343 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800344 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600345 VK_RESULT err;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800346
347 /* request all queues */
348 info.queueRecordCount = gpu->queue_count;
349 info.pRequestedQueues = gpu->queue_reqs;
350
351 /* enable all extensions */
352 info.extensionCount = gpu->extension_count;
Ian Elliott092ee852015-02-11 17:36:23 -0700353 info.ppEnabledExtensionNames = (const char*const*) gpu->extensions;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800354 dev->gpu = gpu;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600355 err = vkCreateDevice(gpu->obj, &info, &dev->obj);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800356 if (err)
357 ERR_EXIT(err);
358
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800359}
360
361static void app_dev_destroy(struct app_dev *dev)
362{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600363 vkDestroyDevice(dev->obj);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800364}
365
366static void app_gpu_init_extensions(struct app_gpu *gpu)
367{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600368 VK_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600369 uint32_t i;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800370
Ian Elliottaae1a572015-02-04 16:48:37 -0700371 static char *known_extensions[] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600372 "VK_WSI_X11",
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800373 };
374
375 for (i = 0; i < ARRAY_SIZE(known_extensions); i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600376 err = vkGetExtensionSupport(gpu->obj, known_extensions[i]);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800377 if (!err)
378 gpu->extension_count++;
379 }
380
381 gpu->extensions =
382 malloc(sizeof(gpu->extensions[0]) * gpu->extension_count);
383 if (!gpu->extensions)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600384 ERR_EXIT(VK_ERROR_OUT_OF_MEMORY);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800385
386 gpu->extension_count = 0;
387 for (i = 0; i < ARRAY_SIZE(known_extensions); i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600388 err = vkGetExtensionSupport(gpu->obj, known_extensions[i]);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800389 if (!err)
390 gpu->extensions[gpu->extension_count++] = known_extensions[i];
391 }
392}
393
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600394static void app_gpu_init(struct app_gpu *gpu, uint32_t id, VK_PHYSICAL_GPU obj)
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800395{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600396 size_t size;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600397 VK_RESULT err;
Ian Elliottaae1a572015-02-04 16:48:37 -0700398 uint32_t i;
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800399
400 memset(gpu, 0, sizeof(*gpu));
401
402 gpu->id = id;
403 gpu->obj = obj;
404 size = sizeof(gpu->props);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600405 err = vkGetGpuInfo(gpu->obj,
406 VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800407 &size, &gpu->props);
408 if (err || size != sizeof(gpu->props))
409 ERR_EXIT(err);
410
411 size = sizeof(gpu->perf);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600412 err = vkGetGpuInfo(gpu->obj,
413 VK_INFO_TYPE_PHYSICAL_GPU_PERFORMANCE,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800414 &size, &gpu->perf);
415 if (err || size != sizeof(gpu->perf))
416 ERR_EXIT(err);
417
418 /* get queue count */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600419 err = vkGetGpuInfo(gpu->obj,
420 VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800421 &size, NULL);
422 if (err || size % sizeof(gpu->queue_props[0]))
423 ERR_EXIT(err);
Ian Elliottaae1a572015-02-04 16:48:37 -0700424 gpu->queue_count = (uint32_t) (size / sizeof(gpu->queue_props[0]));
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800425
426 gpu->queue_props =
427 malloc(sizeof(gpu->queue_props[0]) * gpu->queue_count);
428 size = sizeof(gpu->queue_props[0]) * gpu->queue_count;
429 if (!gpu->queue_props)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600430 ERR_EXIT(VK_ERROR_OUT_OF_MEMORY);
431 err = vkGetGpuInfo(gpu->obj,
432 VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800433 &size, gpu->queue_props);
434 if (err || size != sizeof(gpu->queue_props[0]) * gpu->queue_count)
435 ERR_EXIT(err);
436
437 /* set up queue requests */
438 size = sizeof(*gpu->queue_reqs) * gpu->queue_count;
439 gpu->queue_reqs = malloc(sizeof(*gpu->queue_reqs) * gpu->queue_count);
440 if (!gpu->queue_reqs)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600441 ERR_EXIT(VK_ERROR_OUT_OF_MEMORY);
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800442 for (i = 0; i < gpu->queue_count; i++) {
443 gpu->queue_reqs[i].queueNodeIndex = i;
444 gpu->queue_reqs[i].queueCount = gpu->queue_props[i].queueCount;
445 }
446
447 size = sizeof(gpu->memory_props);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600448 err = vkGetGpuInfo(gpu->obj,
449 VK_INFO_TYPE_PHYSICAL_GPU_MEMORY_PROPERTIES,
Chia-I Wu46c29dd2014-12-02 21:09:20 +0800450 &size, &gpu->memory_props);
451 if (err || size != sizeof(gpu->memory_props))
452 ERR_EXIT(err);
453
454 app_gpu_init_extensions(gpu);
455 app_dev_init(&gpu->dev, gpu);
456 app_dev_init_formats(&gpu->dev);
457}
458
459static void app_gpu_destroy(struct app_gpu *gpu)
460{
461 app_dev_destroy(&gpu->dev);
462 free(gpu->extensions);
463 free(gpu->queue_reqs);
464 free(gpu->queue_props);
465}
466
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467static void app_dev_dump_format_props(const struct app_dev *dev, VK_FORMAT fmt)
Chia-I Wud4bae362014-07-29 11:15:00 +0800468{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600469 const VK_FORMAT_PROPERTIES *props = &dev->format_props[fmt];
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800470 struct {
471 const char *name;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 VK_FLAGS flags;
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800473 } tilings[2];
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600474 uint32_t i;
Chia-I Wud4bae362014-07-29 11:15:00 +0800475
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800476 if (!props->linearTilingFeatures && !props->optimalTilingFeatures)
477 return;
Chia-I Wud4bae362014-07-29 11:15:00 +0800478
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800479 tilings[0].name = "linear";
480 tilings[0].flags = props->linearTilingFeatures;
481 tilings[1].name = "optimal";
482 tilings[1].flags = props->optimalTilingFeatures;
Chia-I Wud4bae362014-07-29 11:15:00 +0800483
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600484 printf("FORMAT_%s\n", vk_format_string(fmt));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800485 for (i = 0; i < ARRAY_SIZE(tilings); i++) {
486 if (!tilings[i].flags)
487 continue;
Chia-I Wud4bae362014-07-29 11:15:00 +0800488
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800489 printf("\t%s tiling image =%s%s%s\n", tilings[i].name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600490 (tilings[i].flags & VK_FORMAT_IMAGE_SHADER_READ_BIT) ? " read" : "",
491 (tilings[i].flags & VK_FORMAT_IMAGE_SHADER_WRITE_BIT) ? " write" : "",
492 (tilings[i].flags & VK_FORMAT_IMAGE_COPY_BIT) ? " copy" : "");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800493 printf("\t%s tiling memory =%s\n", tilings[i].name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600494 (tilings[i].flags & VK_FORMAT_MEMORY_SHADER_ACCESS_BIT) ? " access" : "");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800495 printf("\t%s tiling attachment =%s%s%s%s%s\n", tilings[i].name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600496 (tilings[i].flags & VK_FORMAT_COLOR_ATTACHMENT_WRITE_BIT) ? " color" : "",
497 (tilings[i].flags & VK_FORMAT_COLOR_ATTACHMENT_BLEND_BIT) ? " blend" : "",
498 (tilings[i].flags & VK_FORMAT_DEPTH_ATTACHMENT_BIT) ? " depth" : "",
499 (tilings[i].flags & VK_FORMAT_STENCIL_ATTACHMENT_BIT) ? " stencil" : "",
500 (tilings[i].flags & VK_FORMAT_MSAA_ATTACHMENT_BIT) ? " msaa" : "");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800501 printf("\t%s tiling conversion = %u\n", tilings[i].name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600502 (bool) (tilings[i].flags & VK_FORMAT_CONVERSION_BIT));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800503 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800504}
505
Chia-I Wud4bae362014-07-29 11:15:00 +0800506
507static void
508app_dev_dump(const struct app_dev *dev)
509{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600510 VK_FORMAT fmt;
Chia-I Wud4bae362014-07-29 11:15:00 +0800511
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600512 for (fmt = 0; fmt < VK_NUM_FMT; fmt++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700513 app_dev_dump_format_props(dev, fmt);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800514 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800515}
516
517static void app_gpu_dump_multi_compat(const struct app_gpu *gpu, const struct app_gpu *other,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600518 const VK_GPU_COMPATIBILITY_INFO *info)
Chia-I Wud4bae362014-07-29 11:15:00 +0800519{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600520 printf("VK_GPU_COMPATIBILITY_INFO[GPU%d]\n", other->id);
Chia-I Wud4bae362014-07-29 11:15:00 +0800521
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600522#define TEST(info, b) printf(#b " = %u\n", (bool) (info->compatibilityFlags & VK_GPU_COMPAT_ ##b## _BIT))
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800523 TEST(info, ASIC_FEATURES);
524 TEST(info, IQ_MATCH);
525 TEST(info, PEER_TRANSFER);
526 TEST(info, SHARED_MEMORY);
527 TEST(info, SHARED_SYNC);
528 TEST(info, SHARED_GPU0_DISPLAY);
529 TEST(info, SHARED_GPU1_DISPLAY);
Chia-I Wud4bae362014-07-29 11:15:00 +0800530#undef TEST
531}
532
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600533static void app_gpu_multi_compat(struct app_gpu *gpus, uint32_t gpu_count)
Courtney Goeltzenleuchterd183e712014-08-06 16:12:02 -0600534{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600535 VK_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600536 uint32_t i, j;
Courtney Goeltzenleuchterd183e712014-08-06 16:12:02 -0600537
538 for (i = 0; i < gpu_count; i++) {
539 for (j = 0; j < gpu_count; j++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600540 VK_GPU_COMPATIBILITY_INFO info;
Courtney Goeltzenleuchterd183e712014-08-06 16:12:02 -0600541
542 if (i == j)
543 continue;
544
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600545 err = vkGetMultiGpuCompatibility(gpus[i].obj,
Courtney Goeltzenleuchterd183e712014-08-06 16:12:02 -0600546 gpus[j].obj, &info);
547 if (err)
548 ERR_EXIT(err);
549
550 app_gpu_dump_multi_compat(&gpus[i], &gpus[j], &info);
551 }
552 }
553}
554
Chia-I Wud4bae362014-07-29 11:15:00 +0800555static void app_gpu_dump_props(const struct app_gpu *gpu)
556{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600557 const VK_PHYSICAL_GPU_PROPERTIES *props = &gpu->props;
Chia-I Wud4bae362014-07-29 11:15:00 +0800558
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600559 printf("VK_PHYSICAL_GPU_PROPERTIES\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800560 printf("\tapiVersion = %u\n", props->apiVersion);
561 printf("\tdriverVersion = %u\n", props->driverVersion);
562 printf("\tvendorId = 0x%04x\n", props->vendorId);
563 printf("\tdeviceId = 0x%04x\n", props->deviceId);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600564 printf("\tgpuType = %s\n", vk_gpu_type_string(props->gpuType));
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800565 printf("\tgpuName = %s\n", props->gpuName);
Chia-I Wu54ed0792014-12-27 14:14:50 +0800566 printf("\tmaxInlineMemoryUpdateSize = %zu\n", props->maxInlineMemoryUpdateSize);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800567 printf("\tmaxBoundDescriptorSets = %u\n", props->maxBoundDescriptorSets);
568 printf("\tmaxThreadGroupSize = %u\n", props->maxThreadGroupSize);
569 printf("\ttimestampFrequency = %lu\n", props->timestampFrequency);
570 printf("\tmultiColorAttachmentClears = %u\n", props->multiColorAttachmentClears);
Chia-I Wud4bae362014-07-29 11:15:00 +0800571}
572
573static void app_gpu_dump_perf(const struct app_gpu *gpu)
574{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600575 const VK_PHYSICAL_GPU_PERFORMANCE *perf = &gpu->perf;
Chia-I Wud4bae362014-07-29 11:15:00 +0800576
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600577 printf("VK_PHYSICAL_GPU_PERFORMANCE\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800578 printf("\tmaxGpuClock = %f\n", perf->maxGpuClock);
579 printf("\taluPerClock = %f\n", perf->aluPerClock);
580 printf("\ttexPerClock = %f\n", perf->texPerClock);
581 printf("\tprimsPerClock = %f\n", perf->primsPerClock);
582 printf("\tpixelsPerClock = %f\n", perf->pixelsPerClock);
Chia-I Wud4bae362014-07-29 11:15:00 +0800583}
584
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600585static void app_gpu_dump_extensions(const struct app_gpu *gpu)
586{
Ian Elliottaae1a572015-02-04 16:48:37 -0700587 uint32_t i;
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600588 printf("Extensions");
589 printf("\tcount = %d\n", gpu->extension_count);
590 printf("\t");
591 for (i=0; i< gpu->extension_count; i++) {
592 if (i>0)
593 printf(", "); // separator between extension names
594 printf("%s", gpu->extensions[i]);
595 }
596 printf("\n");
597}
598
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600599static void app_gpu_dump_queue_props(const struct app_gpu *gpu, uint32_t id)
Chia-I Wud4bae362014-07-29 11:15:00 +0800600{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600601 const VK_PHYSICAL_GPU_QUEUE_PROPERTIES *props = &gpu->queue_props[id];
Chia-I Wud4bae362014-07-29 11:15:00 +0800602
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600603 printf("VK_PHYSICAL_GPU_QUEUE_PROPERTIES[%d]\n", id);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800604 printf("\tqueueFlags = %c%c%c%c\n",
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600605 (props->queueFlags & VK_QUEUE_GRAPHICS_BIT) ? 'G' : '.',
606 (props->queueFlags & VK_QUEUE_COMPUTE_BIT) ? 'C' : '.',
607 (props->queueFlags & VK_QUEUE_DMA_BIT) ? 'D' : '.',
608 (props->queueFlags & VK_QUEUE_EXTENDED_BIT) ? 'X' : '.');
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800609 printf("\tqueueCount = %u\n", props->queueCount);
610 printf("\tmaxAtomicCounters = %u\n", props->maxAtomicCounters);
611 printf("\tsupportsTimestamps = %u\n", props->supportsTimestamps);
Courtney Goeltzenleuchtere16aa8e2015-04-02 14:22:12 -0600612 printf("\tmaxMemReferences = %u\n", props->maxMemReferences);
Chia-I Wud4bae362014-07-29 11:15:00 +0800613}
614
615static void app_gpu_dump_memory_props(const struct app_gpu *gpu)
616{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600617 const VK_PHYSICAL_GPU_MEMORY_PROPERTIES *props = &gpu->memory_props;
Chia-I Wud4bae362014-07-29 11:15:00 +0800618
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600619 printf("VK_PHYSICAL_GPU_MEMORY_PROPERTIES\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800620 printf("\tsupportsMigration = %u\n", props->supportsMigration);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800621 printf("\tsupportsPinning = %u\n", props->supportsPinning);
Chia-I Wud4bae362014-07-29 11:15:00 +0800622}
623
624static void app_gpu_dump(const struct app_gpu *gpu)
625{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600626 uint32_t i;
Chia-I Wuf5c46f42014-08-05 15:33:40 +0800627
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800628 printf("GPU%u\n", gpu->id);
629 app_gpu_dump_props(gpu);
630 printf("\n");
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600631 app_gpu_dump_extensions(gpu);
632 printf("\n");
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800633 app_gpu_dump_perf(gpu);
634 printf("\n");
635 for (i = 0; i < gpu->queue_count; i++) {
636 app_gpu_dump_queue_props(gpu, i);
637 printf("\n");
638 }
639 app_gpu_dump_memory_props(gpu);
640 printf("\n");
641 app_dev_dump(&gpu->dev);
Chia-I Wud4bae362014-07-29 11:15:00 +0800642}
643
Chia-I Wud4bae362014-07-29 11:15:00 +0800644int main(int argc, char **argv)
645{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600646 static const VK_APPLICATION_INFO app_info = {
647 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800648 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600649 .pAppName = "vkinfo",
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800650 .appVersion = 1,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600651 .pEngineName = "vkinfo",
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800652 .engineVersion = 1,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600653 .apiVersion = VK_API_VERSION,
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800654 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600655 static const VK_INSTANCE_CREATE_INFO inst_info = {
656 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -0600657 .pNext = NULL,
658 .pAppInfo = &app_info,
659 .pAllocCb = NULL,
660 .extensionCount = 0,
661 .ppEnabledExtensionNames = NULL,
662 };
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800663 struct app_gpu gpus[MAX_GPUS];
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600664 VK_PHYSICAL_GPU objs[MAX_GPUS];
665 VK_INSTANCE inst;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600666 uint32_t gpu_count, i;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600667 VK_RESULT err;
Chia-I Wud4bae362014-07-29 11:15:00 +0800668
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600669 err = vkCreateInstance(&inst_info, &inst);
670 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Ian Elliottdfe55f72015-04-03 15:24:55 -0600671 printf("Cannot find a compatible Vulkan installable client driver "
672 "(ICD).\nExiting ...\n");
673 fflush(stdout);
674 exit(1);
675 } else if (err) {
Jon Ashburn29669a42015-04-04 14:52:07 -0600676
Jon Ashburn92e80132015-01-29 15:47:01 -0700677 ERR_EXIT(err);
Ian Elliottdfe55f72015-04-03 15:24:55 -0600678 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600679 err = vkEnumerateGpus(inst, MAX_GPUS, &gpu_count, objs);
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800680 if (err)
681 ERR_EXIT(err);
Chia-I Wud4bae362014-07-29 11:15:00 +0800682
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800683 for (i = 0; i < gpu_count; i++) {
684 app_gpu_init(&gpus[i], i, objs[i]);
685 app_gpu_dump(&gpus[i]);
686 printf("\n\n");
687 }
Chia-I Wud4bae362014-07-29 11:15:00 +0800688
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800689 app_gpu_multi_compat(gpus, gpu_count);
Chia-I Wud4bae362014-07-29 11:15:00 +0800690
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800691 for (i = 0; i < gpu_count; i++)
692 app_gpu_destroy(&gpus[i]);
Chia-I Wud4bae362014-07-29 11:15:00 +0800693
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600694 vkDestroyInstance(inst);
Chia-I Wu0b9a7372014-08-06 12:09:19 +0800695
Chia-I Wu190ebdc2014-08-06 12:04:13 +0800696 return 0;
Chia-I Wud4bae362014-07-29 11:15:00 +0800697}