blob: 587249539fbf12d2cab884e14d2f88375f89fd0a [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 {
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070031
32bool EnumerateExtensions(const char* layer_name,
33 std::vector<VkExtensionProperties>* extensions) {
34 VkResult result;
35 uint32_t count = 0;
36 result = vkEnumerateInstanceExtensionProperties(layer_name, &count, nullptr);
37 if (result != VK_SUCCESS)
38 return false;
39 extensions->resize(count);
40 result = vkEnumerateInstanceExtensionProperties(layer_name, &count,
41 extensions->data());
42 if (result != VK_SUCCESS)
43 return false;
44 return true;
45}
46
47bool HasExtension(const char* extension_name,
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070048 const std::vector<VkExtensionProperties>& extensions) {
49 return std::find_if(extensions.cbegin(), extensions.cend(),
50 [extension_name](const VkExtensionProperties& extension) {
51 return strcmp(extension.extensionName,
52 extension_name) == 0;
53 }) != extensions.cend();
54}
55} // anonymous namespace
56
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -070057VkJsonDevice VkJsonGetDevice(VkPhysicalDevice physical_device) {
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070058 VkJsonDevice device;
59
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070060 uint32_t extension_count = 0;
61 vkEnumerateDeviceExtensionProperties(physical_device, nullptr,
62 &extension_count, nullptr);
63 if (extension_count > 0) {
64 device.extensions.resize(extension_count);
65 vkEnumerateDeviceExtensionProperties(
66 physical_device, nullptr, &extension_count, device.extensions.data());
67 }
68
69 uint32_t layer_count = 0;
70 vkEnumerateDeviceLayerProperties(physical_device, &layer_count, nullptr);
71 if (layer_count > 0) {
72 device.layers.resize(layer_count);
73 vkEnumerateDeviceLayerProperties(physical_device, &layer_count,
74 device.layers.data());
75 }
76
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -070077 VkPhysicalDeviceProperties2 properties = {
78 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
79 nullptr,
80 {},
81 };
82 if (HasExtension("VK_KHR_driver_properties", device.extensions)) {
83 device.ext_driver_properties.reported = true;
84 device.ext_driver_properties.driver_properties_khr.sType =
85 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
86 device.ext_driver_properties.driver_properties_khr.pNext = properties.pNext;
87 properties.pNext = &device.ext_driver_properties.driver_properties_khr;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -070088 }
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -070089 vkGetPhysicalDeviceProperties2(physical_device, &properties);
90 device.properties = properties.properties;
91
92 VkPhysicalDeviceFeatures2 features = {
93 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
94 nullptr,
95 {},
96 };
97 if (HasExtension("VK_KHR_variable_pointers", device.extensions)) {
98 device.ext_variable_pointer_features.reported = true;
99 device.ext_variable_pointer_features.variable_pointer_features_khr.sType =
100 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR;
101 device.ext_variable_pointer_features.variable_pointer_features_khr.pNext =
102 features.pNext;
103 features.pNext =
104 &device.ext_variable_pointer_features.variable_pointer_features_khr;
105 }
106 if (HasExtension("VK_KHR_shader_float16_int8", device.extensions)) {
107 device.ext_shader_float16_int8_features.reported = true;
108 device.ext_shader_float16_int8_features.shader_float16_int8_features_khr
109 .sType =
110 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR;
111 device.ext_shader_float16_int8_features.shader_float16_int8_features_khr
112 .pNext = features.pNext;
113 features.pNext = &device.ext_shader_float16_int8_features
114 .shader_float16_int8_features_khr;
115 }
116 vkGetPhysicalDeviceFeatures2(physical_device, &features);
117 device.features = features.features;
118
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700119 vkGetPhysicalDeviceMemoryProperties(physical_device, &device.memory);
120
121 uint32_t queue_family_count = 0;
122 vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count,
123 nullptr);
124 if (queue_family_count > 0) {
125 device.queues.resize(queue_family_count);
126 vkGetPhysicalDeviceQueueFamilyProperties(
127 physical_device, &queue_family_count, device.queues.data());
128 }
129
130 VkFormatProperties format_properties = {};
131 for (VkFormat format = VK_FORMAT_R4G4_UNORM_PACK8;
Yiwei Zhang17bf1c02020-10-19 20:14:01 -0700132 // TODO(http://b/171403054): avoid hard-coding last value in the
133 // contiguous range
134 format <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700135 format = static_cast<VkFormat>(format + 1)) {
136 vkGetPhysicalDeviceFormatProperties(physical_device, format,
137 &format_properties);
138 if (format_properties.linearTilingFeatures ||
139 format_properties.optimalTilingFeatures ||
140 format_properties.bufferFeatures) {
141 device.formats.insert(std::make_pair(format, format_properties));
142 }
143 }
144
145 if (device.properties.apiVersion >= VK_API_VERSION_1_1) {
146 for (VkFormat format = VK_FORMAT_G8B8G8R8_422_UNORM;
Yiwei Zhang17bf1c02020-10-19 20:14:01 -0700147 // TODO(http://b/171403054): avoid hard-coding last value in the
148 // contiguous range
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700149 format <= VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM;
150 format = static_cast<VkFormat>(format + 1)) {
151 vkGetPhysicalDeviceFormatProperties(physical_device, format,
152 &format_properties);
153 if (format_properties.linearTilingFeatures ||
154 format_properties.optimalTilingFeatures ||
155 format_properties.bufferFeatures) {
156 device.formats.insert(std::make_pair(format, format_properties));
157 }
158 }
159
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700160 VkPhysicalDeviceProperties2 properties2 = {
161 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
162 nullptr,
163 {},
164 };
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700165
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700166 device.subgroup_properties.sType =
167 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
168 device.subgroup_properties.pNext = properties2.pNext;
169 properties2.pNext = &device.subgroup_properties;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700170
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700171 device.point_clipping_properties.sType =
172 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES;
173 device.point_clipping_properties.pNext = properties2.pNext;
174 properties2.pNext = &device.point_clipping_properties;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700175
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700176 device.multiview_properties.sType =
177 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
178 device.multiview_properties.pNext = properties2.pNext;
179 properties2.pNext = &device.multiview_properties;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700180
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700181 device.id_properties.sType =
182 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
183 device.id_properties.pNext = properties2.pNext;
184 properties2.pNext = &device.id_properties;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700185
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700186 device.maintenance3_properties.sType =
187 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES;
188 device.maintenance3_properties.pNext = properties2.pNext;
189 properties2.pNext = &device.maintenance3_properties;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700190
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700191 vkGetPhysicalDeviceProperties2(physical_device, &properties2);
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700192
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700193 VkPhysicalDeviceFeatures2 features2 = {
194 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
195 nullptr,
196 {},
197 };
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700198
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700199 device.bit16_storage_features.sType =
200 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES;
201 device.bit16_storage_features.pNext = features2.pNext;
202 features2.pNext = &device.bit16_storage_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700203
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700204 device.multiview_features.sType =
205 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES;
206 device.multiview_features.pNext = features2.pNext;
207 features2.pNext = &device.multiview_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700208
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700209 device.variable_pointer_features.sType =
210 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES;
211 device.variable_pointer_features.pNext = features2.pNext;
212 features2.pNext = &device.variable_pointer_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700213
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700214 device.protected_memory_features.sType =
215 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES;
216 device.protected_memory_features.pNext = features2.pNext;
217 features2.pNext = &device.protected_memory_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700218
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700219 device.sampler_ycbcr_conversion_features.sType =
220 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
221 device.sampler_ycbcr_conversion_features.pNext = features2.pNext;
222 features2.pNext = &device.sampler_ycbcr_conversion_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700223
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700224 device.shader_draw_parameter_features.sType =
225 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES;
226 device.shader_draw_parameter_features.pNext = features2.pNext;
227 features2.pNext = &device.shader_draw_parameter_features;
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700228
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700229 vkGetPhysicalDeviceFeatures2(physical_device, &features2);
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700230
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700231 VkPhysicalDeviceExternalFenceInfo external_fence_info = {
232 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO, nullptr,
233 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT};
234 VkExternalFenceProperties external_fence_properties = {};
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700235
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700236 for (VkExternalFenceHandleTypeFlagBits handle_type =
237 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT;
238 handle_type <= VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
239 handle_type =
240 static_cast<VkExternalFenceHandleTypeFlagBits>(handle_type << 1)) {
241 external_fence_info.handleType = handle_type;
242 vkGetPhysicalDeviceExternalFenceProperties(
243 physical_device, &external_fence_info, &external_fence_properties);
244 if (external_fence_properties.exportFromImportedHandleTypes ||
245 external_fence_properties.compatibleHandleTypes ||
246 external_fence_properties.externalFenceFeatures) {
247 device.external_fence_properties.insert(
248 std::make_pair(handle_type, external_fence_properties));
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700249 }
250 }
251
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700252 VkPhysicalDeviceExternalSemaphoreInfo external_semaphore_info = {
253 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, nullptr,
254 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
255 VkExternalSemaphoreProperties external_semaphore_properties = {};
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700256
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700257 for (VkExternalSemaphoreHandleTypeFlagBits handle_type =
258 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
259 handle_type <= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
260 handle_type = static_cast<VkExternalSemaphoreHandleTypeFlagBits>(
261 handle_type << 1)) {
262 external_semaphore_info.handleType = handle_type;
263 vkGetPhysicalDeviceExternalSemaphoreProperties(
264 physical_device, &external_semaphore_info,
265 &external_semaphore_properties);
266 if (external_semaphore_properties.exportFromImportedHandleTypes ||
267 external_semaphore_properties.compatibleHandleTypes ||
268 external_semaphore_properties.externalSemaphoreFeatures) {
269 device.external_semaphore_properties.insert(
270 std::make_pair(handle_type, external_semaphore_properties));
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700271 }
272 }
273 }
274
275 return device;
276}
277
278VkJsonInstance VkJsonGetInstance() {
279 VkJsonInstance instance;
280 VkResult result;
281 uint32_t count;
282
283 count = 0;
284 result = vkEnumerateInstanceLayerProperties(&count, nullptr);
285 if (result != VK_SUCCESS)
286 return VkJsonInstance();
287 if (count > 0) {
288 std::vector<VkLayerProperties> layers(count);
289 result = vkEnumerateInstanceLayerProperties(&count, layers.data());
290 if (result != VK_SUCCESS)
291 return VkJsonInstance();
292 instance.layers.reserve(count);
293 for (auto& layer : layers) {
294 instance.layers.push_back(VkJsonLayer{layer, std::vector<VkExtensionProperties>()});
295 if (!EnumerateExtensions(layer.layerName,
296 &instance.layers.back().extensions))
297 return VkJsonInstance();
298 }
299 }
300
301 if (!EnumerateExtensions(nullptr, &instance.extensions))
302 return VkJsonInstance();
303
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700304 const VkApplicationInfo app_info = {
305 VK_STRUCTURE_TYPE_APPLICATION_INFO,
306 nullptr,
307 "vkjson_info",
308 1,
309 "",
310 0,
311 VK_API_VERSION_1_1,
312 };
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700313 VkInstanceCreateInfo instance_info = {
314 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
315 nullptr,
316 0,
317 &app_info,
318 0,
319 nullptr,
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700320 0,
321 nullptr,
322 };
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700323 VkInstance vkinstance;
324 result = vkCreateInstance(&instance_info, nullptr, &vkinstance);
325 if (result != VK_SUCCESS)
326 return VkJsonInstance();
327
328 count = 0;
329 result = vkEnumeratePhysicalDevices(vkinstance, &count, nullptr);
330 if (result != VK_SUCCESS) {
331 vkDestroyInstance(vkinstance, nullptr);
332 return VkJsonInstance();
333 }
334
335 std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE);
336 result = vkEnumeratePhysicalDevices(vkinstance, &count, devices.data());
337 if (result != VK_SUCCESS) {
338 vkDestroyInstance(vkinstance, nullptr);
339 return VkJsonInstance();
340 }
341
342 std::map<VkPhysicalDevice, uint32_t> device_map;
343 const uint32_t sz = devices.size();
344 instance.devices.reserve(sz);
345 for (uint32_t i = 0; i < sz; ++i) {
346 device_map.insert(std::make_pair(devices[i], i));
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700347 instance.devices.emplace_back(VkJsonGetDevice(devices[i]));
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700348 }
349
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700350 result = vkEnumerateInstanceVersion(&instance.api_version);
351 if (result != VK_SUCCESS) {
352 vkDestroyInstance(vkinstance, nullptr);
353 return VkJsonInstance();
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700354 }
355
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700356 count = 0;
357 result = vkEnumeratePhysicalDeviceGroups(vkinstance, &count, nullptr);
358 if (result != VK_SUCCESS) {
359 vkDestroyInstance(vkinstance, nullptr);
360 return VkJsonInstance();
361 }
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700362
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700363 VkJsonDeviceGroup device_group;
364 std::vector<VkPhysicalDeviceGroupProperties> group_properties;
365 group_properties.resize(count);
Yiwei Zhang92bf3302021-04-03 19:33:39 +0000366 for (auto& properties : group_properties) {
367 properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
368 properties.pNext = nullptr;
369 }
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700370 result = vkEnumeratePhysicalDeviceGroups(vkinstance, &count,
371 group_properties.data());
372 if (result != VK_SUCCESS) {
373 vkDestroyInstance(vkinstance, nullptr);
374 return VkJsonInstance();
375 }
376 for (auto properties : group_properties) {
377 device_group.properties = properties;
378 for (uint32_t i = 0; i < properties.physicalDeviceCount; ++i) {
379 device_group.device_inds.push_back(
380 device_map[properties.physicalDevices[i]]);
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700381 }
Yiwei Zhang91fc3dc2020-07-05 23:33:22 -0700382 instance.device_groups.push_back(device_group);
Yiwei Zhangf9a57e62018-04-05 00:17:22 -0700383 }
384
385 vkDestroyInstance(vkinstance, nullptr);
386 return instance;
387}