blob: 73586d42f89f39b9c4fea1d2c3fa5d594ab5384a [file] [log] [blame]
Yiwei Zhangf9a57e62018-04-05 00:17:22 -07001///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015-2016 The Khronos Group Inc.
4// Copyright (c) 2015-2016 Valve Corporation
5// Copyright (c) 2015-2016 LunarG, Inc.
6// Copyright (c) 2015-2016 Google, Inc.
7//
8// Licensed under the Apache License, Version 2.0 (the "License");
9// you may not use this file except in compliance with the License.
10// You may obtain a copy of the License at
11//
12// http://www.apache.org/licenses/LICENSE-2.0
13//
14// Unless required by applicable law or agreed to in writing, software
15// distributed under the License is distributed on an "AS IS" BASIS,
16// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17// See the License for the specific language governing permissions and
18// limitations under the License.
19///////////////////////////////////////////////////////////////////////////////
20
21#ifndef VK_PROTOTYPES
22#define VK_PROTOTYPES
23#endif
24
25#include "vkjson.h"
26
27#include <algorithm>
28#include <utility>
29
30namespace {
31const char* kSupportedInstanceExtensions[] = {
32 "VK_KHR_get_physical_device_properties2"};
33
34bool EnumerateExtensions(const char* layer_name,
35 std::vector<VkExtensionProperties>* extensions) {
36 VkResult result;
37 uint32_t count = 0;
38 result = vkEnumerateInstanceExtensionProperties(layer_name, &count, nullptr);
39 if (result != VK_SUCCESS)
40 return false;
41 extensions->resize(count);
42 result = vkEnumerateInstanceExtensionProperties(layer_name, &count,
43 extensions->data());
44 if (result != VK_SUCCESS)
45 return false;
46 return true;
47}
48
49bool HasExtension(const char* extension_name,
50 uint32_t count,
51 const char* const* extensions) {
52 return std::find_if(extensions, extensions + count,
53 [extension_name](const char* extension) {
54 return strcmp(extension, extension_name) == 0;
55 }) != extensions + count;
56}
57
58bool HasExtension(const char* extension_name,
59 const std::vector<VkExtensionProperties>& extensions) {
60 return std::find_if(extensions.cbegin(), extensions.cend(),
61 [extension_name](const VkExtensionProperties& extension) {
62 return strcmp(extension.extensionName,
63 extension_name) == 0;
64 }) != extensions.cend();
65}
66} // anonymous namespace
67
68VkJsonDevice VkJsonGetDevice(VkInstance instance,
69 VkPhysicalDevice physical_device,
70 uint32_t instance_extension_count,
71 const char* const* instance_extensions) {
72 VkJsonDevice device;
73
Yiwei Zhang7b4169d2018-10-02 18:58:31 -070074 PFN_vkGetPhysicalDeviceProperties2KHR vkpGetPhysicalDeviceProperties2KHR =
75 nullptr;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070076 PFN_vkGetPhysicalDeviceFeatures2KHR vkpGetPhysicalDeviceFeatures2KHR =
77 nullptr;
78 if (instance != VK_NULL_HANDLE &&
79 HasExtension("VK_KHR_get_physical_device_properties2",
80 instance_extension_count, instance_extensions)) {
Yiwei Zhang7b4169d2018-10-02 18:58:31 -070081 vkpGetPhysicalDeviceProperties2KHR =
82 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2KHR>(
83 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2KHR"));
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070084 vkpGetPhysicalDeviceFeatures2KHR =
85 reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures2KHR>(
86 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2KHR"));
87 }
88
89 uint32_t extension_count = 0;
90 vkEnumerateDeviceExtensionProperties(physical_device, nullptr,
91 &extension_count, nullptr);
92 if (extension_count > 0) {
93 device.extensions.resize(extension_count);
94 vkEnumerateDeviceExtensionProperties(
95 physical_device, nullptr, &extension_count, device.extensions.data());
96 }
97
98 uint32_t layer_count = 0;
99 vkEnumerateDeviceLayerProperties(physical_device, &layer_count, nullptr);
100 if (layer_count > 0) {
101 device.layers.resize(layer_count);
102 vkEnumerateDeviceLayerProperties(physical_device, &layer_count,
103 device.layers.data());
104 }
105
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700106 if (HasExtension("VK_KHR_get_physical_device_properties2",
107 instance_extension_count, instance_extensions)) {
Yiwei Zhang7b4169d2018-10-02 18:58:31 -0700108 VkPhysicalDeviceProperties2KHR properties = {
109 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
110 nullptr,
111 {} // properties
112 };
113 if (HasExtension("VK_KHR_driver_properties", device.extensions)) {
114 device.ext_driver_properties.reported = true;
115 device.ext_driver_properties.driver_properties_khr.sType =
116 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
117 device.ext_driver_properties.driver_properties_khr.pNext =
118 properties.pNext;
119 properties.pNext =
120 &device.ext_driver_properties.driver_properties_khr;
121 }
122 vkpGetPhysicalDeviceProperties2KHR(physical_device, &properties);
123 device.properties = properties.properties;
124
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700125 VkPhysicalDeviceFeatures2KHR features = {
126 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR,
127 nullptr,
128 {} // features
129 };
130 if (HasExtension("VK_KHR_variable_pointers", device.extensions)) {
Yiwei Zhang7b4169d2018-10-02 18:58:31 -0700131 device.ext_variable_pointer_features.reported = true;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700132 device.ext_variable_pointer_features.variable_pointer_features_khr.sType =
133 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR;
134 device.ext_variable_pointer_features.variable_pointer_features_khr.pNext =
135 features.pNext;
136 features.pNext =
137 &device.ext_variable_pointer_features.variable_pointer_features_khr;
138 }
Peiyong Linc1b5ffb2020-02-27 19:31:51 -0800139 if (HasExtension("VK_KHR_shader_float16_int8", device.extensions)) {
140 device.ext_shader_float16_int8_features.reported = true;
141 device.ext_shader_float16_int8_features.shader_float16_int8_features_khr
142 .sType =
143 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR;
144 device.ext_shader_float16_int8_features.shader_float16_int8_features_khr
145 .pNext = features.pNext;
146 features.pNext = &device.ext_shader_float16_int8_features
147 .shader_float16_int8_features_khr;
148 }
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700149 vkpGetPhysicalDeviceFeatures2KHR(physical_device, &features);
150 device.features = features.features;
151 } else {
Yiwei Zhang7b4169d2018-10-02 18:58:31 -0700152 vkGetPhysicalDeviceProperties(physical_device, &device.properties);
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700153 vkGetPhysicalDeviceFeatures(physical_device, &device.features);
154 }
155 vkGetPhysicalDeviceMemoryProperties(physical_device, &device.memory);
156
157 uint32_t queue_family_count = 0;
158 vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count,
159 nullptr);
160 if (queue_family_count > 0) {
161 device.queues.resize(queue_family_count);
162 vkGetPhysicalDeviceQueueFamilyProperties(
163 physical_device, &queue_family_count, device.queues.data());
164 }
165
166 VkFormatProperties format_properties = {};
167 for (VkFormat format = VK_FORMAT_R4G4_UNORM_PACK8;
Yiwei Zhang17bf1c02020-10-19 20:14:01 -0700168 // TODO(http://b/171403054): avoid hard-coding last value in the
169 // contiguous range
170 format <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700171 format = static_cast<VkFormat>(format + 1)) {
172 vkGetPhysicalDeviceFormatProperties(physical_device, format,
173 &format_properties);
174 if (format_properties.linearTilingFeatures ||
175 format_properties.optimalTilingFeatures ||
176 format_properties.bufferFeatures) {
177 device.formats.insert(std::make_pair(format, format_properties));
178 }
179 }
180
181 if (device.properties.apiVersion >= VK_API_VERSION_1_1) {
182 for (VkFormat format = VK_FORMAT_G8B8G8R8_422_UNORM;
Yiwei Zhang17bf1c02020-10-19 20:14:01 -0700183 // TODO(http://b/171403054): avoid hard-coding last value in the
184 // contiguous range
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700185 format <= VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM;
186 format = static_cast<VkFormat>(format + 1)) {
187 vkGetPhysicalDeviceFormatProperties(physical_device, format,
188 &format_properties);
189 if (format_properties.linearTilingFeatures ||
190 format_properties.optimalTilingFeatures ||
191 format_properties.bufferFeatures) {
192 device.formats.insert(std::make_pair(format, format_properties));
193 }
194 }
195
196 PFN_vkGetPhysicalDeviceProperties2 vkpGetPhysicalDeviceProperties2 =
197 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>(
198 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2"));
199 if (vkpGetPhysicalDeviceProperties2) {
200 VkPhysicalDeviceProperties2 properties2 = {
201 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, nullptr, {}};
202
203 device.subgroup_properties.sType =
204 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
205 device.subgroup_properties.pNext = properties2.pNext;
206 properties2.pNext = &device.subgroup_properties;
207
208 device.point_clipping_properties.sType =
209 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES;
210 device.point_clipping_properties.pNext = properties2.pNext;
211 properties2.pNext = &device.point_clipping_properties;
212
213 device.multiview_properties.sType =
214 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
215 device.multiview_properties.pNext = properties2.pNext;
216 properties2.pNext = &device.multiview_properties;
217
218 device.id_properties.sType =
219 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
220 device.id_properties.pNext = properties2.pNext;
221 properties2.pNext = &device.id_properties;
222
223 device.maintenance3_properties.sType =
224 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES;
225 device.maintenance3_properties.pNext = properties2.pNext;
226 properties2.pNext = &device.maintenance3_properties;
227
228 (*vkpGetPhysicalDeviceProperties2)(physical_device, &properties2);
229 }
230
231 PFN_vkGetPhysicalDeviceFeatures2 vkpGetPhysicalDeviceFeatures2 =
232 reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures2>(
233 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2"));
234 if (vkpGetPhysicalDeviceFeatures2) {
235 VkPhysicalDeviceFeatures2 features2 = {
236 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, nullptr, {}};
237
238 device.bit16_storage_features.sType =
239 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES;
240 device.bit16_storage_features.pNext = features2.pNext;
241 features2.pNext = &device.bit16_storage_features;
242
243 device.multiview_features.sType =
244 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES;
245 device.multiview_features.pNext = features2.pNext;
246 features2.pNext = &device.multiview_features;
247
248 device.variable_pointer_features.sType =
249 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES;
250 device.variable_pointer_features.pNext = features2.pNext;
251 features2.pNext = &device.variable_pointer_features;
252
253 device.protected_memory_features.sType =
254 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES;
255 device.protected_memory_features.pNext = features2.pNext;
256 features2.pNext = &device.protected_memory_features;
257
258 device.sampler_ycbcr_conversion_features.sType =
259 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
260 device.sampler_ycbcr_conversion_features.pNext = features2.pNext;
261 features2.pNext = &device.sampler_ycbcr_conversion_features;
262
263 device.shader_draw_parameter_features.sType =
264 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES;
265 device.shader_draw_parameter_features.pNext = features2.pNext;
266 features2.pNext = &device.shader_draw_parameter_features;
267
268 (*vkpGetPhysicalDeviceFeatures2)(physical_device, &features2);
269 }
270
271 PFN_vkGetPhysicalDeviceExternalFenceProperties
272 vkpGetPhysicalDeviceExternalFenceProperties =
273 reinterpret_cast<PFN_vkGetPhysicalDeviceExternalFenceProperties>(
274 vkGetInstanceProcAddr(
275 instance, "vkGetPhysicalDeviceExternalFenceProperties"));
276 if (vkpGetPhysicalDeviceExternalFenceProperties) {
277 VkPhysicalDeviceExternalFenceInfo external_fence_info = {
278 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO, nullptr,
279 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT};
280 VkExternalFenceProperties external_fence_properties = {};
281
282 for (VkExternalFenceHandleTypeFlagBits handle_type =
283 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT;
284 handle_type <= VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
285 handle_type = static_cast<VkExternalFenceHandleTypeFlagBits>(
286 handle_type << 1)) {
287 external_fence_info.handleType = handle_type;
288 (*vkpGetPhysicalDeviceExternalFenceProperties)(
289 physical_device, &external_fence_info, &external_fence_properties);
290 if (external_fence_properties.exportFromImportedHandleTypes ||
291 external_fence_properties.compatibleHandleTypes ||
292 external_fence_properties.externalFenceFeatures) {
293 device.external_fence_properties.insert(
294 std::make_pair(handle_type, external_fence_properties));
295 }
296 }
297 }
298
299 PFN_vkGetPhysicalDeviceExternalSemaphoreProperties
300 vkpGetPhysicalDeviceExternalSemaphoreProperties = reinterpret_cast<
301 PFN_vkGetPhysicalDeviceExternalSemaphoreProperties>(
302 vkGetInstanceProcAddr(
303 instance, "vkGetPhysicalDeviceExternalSemaphoreProperties"));
304 if (vkpGetPhysicalDeviceExternalSemaphoreProperties) {
305 VkPhysicalDeviceExternalSemaphoreInfo external_semaphore_info = {
306 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, nullptr,
307 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
308 VkExternalSemaphoreProperties external_semaphore_properties = {};
309
310 for (VkExternalSemaphoreHandleTypeFlagBits handle_type =
311 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
312 handle_type <= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
313 handle_type = static_cast<VkExternalSemaphoreHandleTypeFlagBits>(
314 handle_type << 1)) {
315 external_semaphore_info.handleType = handle_type;
316 (*vkpGetPhysicalDeviceExternalSemaphoreProperties)(
317 physical_device, &external_semaphore_info,
318 &external_semaphore_properties);
319 if (external_semaphore_properties.exportFromImportedHandleTypes ||
320 external_semaphore_properties.compatibleHandleTypes ||
321 external_semaphore_properties.externalSemaphoreFeatures) {
322 device.external_semaphore_properties.insert(
323 std::make_pair(handle_type, external_semaphore_properties));
324 }
325 }
326 }
327 }
328
329 return device;
330}
331
332VkJsonInstance VkJsonGetInstance() {
333 VkJsonInstance instance;
334 VkResult result;
335 uint32_t count;
336
337 count = 0;
338 result = vkEnumerateInstanceLayerProperties(&count, nullptr);
339 if (result != VK_SUCCESS)
340 return VkJsonInstance();
341 if (count > 0) {
342 std::vector<VkLayerProperties> layers(count);
343 result = vkEnumerateInstanceLayerProperties(&count, layers.data());
344 if (result != VK_SUCCESS)
345 return VkJsonInstance();
346 instance.layers.reserve(count);
347 for (auto& layer : layers) {
348 instance.layers.push_back(VkJsonLayer{layer, std::vector<VkExtensionProperties>()});
349 if (!EnumerateExtensions(layer.layerName,
350 &instance.layers.back().extensions))
351 return VkJsonInstance();
352 }
353 }
354
355 if (!EnumerateExtensions(nullptr, &instance.extensions))
356 return VkJsonInstance();
357
358 std::vector<const char*> instance_extensions;
359 for (const auto extension : kSupportedInstanceExtensions) {
360 if (HasExtension(extension, instance.extensions))
361 instance_extensions.push_back(extension);
362 }
363
364 const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
365 nullptr,
366 "vkjson_info",
367 1,
368 "",
369 0,
Tapani Pällia8714232018-10-16 09:43:46 +0300370 VK_API_VERSION_1_1};
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700371 VkInstanceCreateInfo instance_info = {
372 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
373 nullptr,
374 0,
375 &app_info,
376 0,
377 nullptr,
378 static_cast<uint32_t>(instance_extensions.size()),
379 instance_extensions.data()};
380 VkInstance vkinstance;
381 result = vkCreateInstance(&instance_info, nullptr, &vkinstance);
382 if (result != VK_SUCCESS)
383 return VkJsonInstance();
384
385 count = 0;
386 result = vkEnumeratePhysicalDevices(vkinstance, &count, nullptr);
387 if (result != VK_SUCCESS) {
388 vkDestroyInstance(vkinstance, nullptr);
389 return VkJsonInstance();
390 }
391
392 std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE);
393 result = vkEnumeratePhysicalDevices(vkinstance, &count, devices.data());
394 if (result != VK_SUCCESS) {
395 vkDestroyInstance(vkinstance, nullptr);
396 return VkJsonInstance();
397 }
398
399 std::map<VkPhysicalDevice, uint32_t> device_map;
400 const uint32_t sz = devices.size();
401 instance.devices.reserve(sz);
402 for (uint32_t i = 0; i < sz; ++i) {
403 device_map.insert(std::make_pair(devices[i], i));
404 instance.devices.emplace_back(VkJsonGetDevice(vkinstance, devices[i],
405 instance_extensions.size(),
406 instance_extensions.data()));
407 }
408
409 PFN_vkEnumerateInstanceVersion vkpEnumerateInstanceVersion =
410 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
411 vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
412 if (!vkpEnumerateInstanceVersion) {
413 instance.api_version = VK_API_VERSION_1_0;
414 } else {
415 result = (*vkpEnumerateInstanceVersion)(&instance.api_version);
416 if (result != VK_SUCCESS) {
417 vkDestroyInstance(vkinstance, nullptr);
418 return VkJsonInstance();
419 }
420 }
421
422 PFN_vkEnumeratePhysicalDeviceGroups vkpEnumeratePhysicalDeviceGroups =
423 reinterpret_cast<PFN_vkEnumeratePhysicalDeviceGroups>(
424 vkGetInstanceProcAddr(vkinstance, "vkEnumeratePhysicalDeviceGroups"));
425 if (vkpEnumeratePhysicalDeviceGroups) {
426 count = 0;
427 result = (*vkpEnumeratePhysicalDeviceGroups)(vkinstance, &count, nullptr);
428 if (result != VK_SUCCESS) {
429 vkDestroyInstance(vkinstance, nullptr);
430 return VkJsonInstance();
431 }
432
433 VkJsonDeviceGroup device_group;
434 std::vector<VkPhysicalDeviceGroupProperties> group_properties;
435 group_properties.resize(count);
436 result = (*vkpEnumeratePhysicalDeviceGroups)(vkinstance, &count,
437 group_properties.data());
438 if (result != VK_SUCCESS) {
439 vkDestroyInstance(vkinstance, nullptr);
440 return VkJsonInstance();
441 }
442 for (auto properties : group_properties) {
443 device_group.properties = properties;
444 for (uint32_t i = 0; i < properties.physicalDeviceCount; ++i) {
445 device_group.device_inds.push_back(
446 device_map[properties.physicalDevices[i]]);
447 }
448 instance.device_groups.push_back(device_group);
449 }
450 }
451
452 vkDestroyInstance(vkinstance, nullptr);
453 return instance;
454}