1 ///////////////////////////////////////////////////////////////////////////////
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
30 namespace {
31
EnumerateExtensions(const char * layer_name,std::vector<VkExtensionProperties> * extensions)32 bool 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
HasExtension(const char * extension_name,const std::vector<VkExtensionProperties> & extensions)47 bool HasExtension(const char* extension_name,
48 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
VkJsonGetDevice(VkPhysicalDevice physical_device)57 VkJsonDevice VkJsonGetDevice(VkPhysicalDevice physical_device) {
58 VkJsonDevice device;
59
60 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
77 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;
88 }
89 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
119 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;
132 // TODO(http://b/171403054): avoid hard-coding last value in the
133 // contiguous range
134 format <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK;
135 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;
147 // TODO(http://b/171403054): avoid hard-coding last value in the
148 // contiguous range
149 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
160 device.subgroup_properties.sType =
161 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
162 device.subgroup_properties.pNext = properties.pNext;
163 properties.pNext = &device.subgroup_properties;
164
165 device.point_clipping_properties.sType =
166 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES;
167 device.point_clipping_properties.pNext = properties.pNext;
168 properties.pNext = &device.point_clipping_properties;
169
170 device.multiview_properties.sType =
171 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
172 device.multiview_properties.pNext = properties.pNext;
173 properties.pNext = &device.multiview_properties;
174
175 device.id_properties.sType =
176 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
177 device.id_properties.pNext = properties.pNext;
178 properties.pNext = &device.id_properties;
179
180 device.maintenance3_properties.sType =
181 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES;
182 device.maintenance3_properties.pNext = properties.pNext;
183 properties.pNext = &device.maintenance3_properties;
184
185 vkGetPhysicalDeviceProperties2(physical_device, &properties);
186
187 device.bit16_storage_features.sType =
188 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES;
189 device.bit16_storage_features.pNext = features.pNext;
190 features.pNext = &device.bit16_storage_features;
191
192 device.multiview_features.sType =
193 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES;
194 device.multiview_features.pNext = features.pNext;
195 features.pNext = &device.multiview_features;
196
197 device.variable_pointer_features.sType =
198 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES;
199 device.variable_pointer_features.pNext = features.pNext;
200 features.pNext = &device.variable_pointer_features;
201
202 device.protected_memory_features.sType =
203 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES;
204 device.protected_memory_features.pNext = features.pNext;
205 features.pNext = &device.protected_memory_features;
206
207 device.sampler_ycbcr_conversion_features.sType =
208 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
209 device.sampler_ycbcr_conversion_features.pNext = features.pNext;
210 features.pNext = &device.sampler_ycbcr_conversion_features;
211
212 device.shader_draw_parameter_features.sType =
213 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES;
214 device.shader_draw_parameter_features.pNext = features.pNext;
215 features.pNext = &device.shader_draw_parameter_features;
216
217 vkGetPhysicalDeviceFeatures2(physical_device, &features);
218
219 VkPhysicalDeviceExternalFenceInfo external_fence_info = {
220 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO, nullptr,
221 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT};
222 VkExternalFenceProperties external_fence_properties = {};
223
224 for (VkExternalFenceHandleTypeFlagBits handle_type =
225 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT;
226 handle_type <= VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
227 handle_type =
228 static_cast<VkExternalFenceHandleTypeFlagBits>(handle_type << 1)) {
229 external_fence_info.handleType = handle_type;
230 vkGetPhysicalDeviceExternalFenceProperties(
231 physical_device, &external_fence_info, &external_fence_properties);
232 if (external_fence_properties.exportFromImportedHandleTypes ||
233 external_fence_properties.compatibleHandleTypes ||
234 external_fence_properties.externalFenceFeatures) {
235 device.external_fence_properties.insert(
236 std::make_pair(handle_type, external_fence_properties));
237 }
238 }
239
240 VkPhysicalDeviceExternalSemaphoreInfo external_semaphore_info = {
241 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, nullptr,
242 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
243 VkExternalSemaphoreProperties external_semaphore_properties = {};
244
245 for (VkExternalSemaphoreHandleTypeFlagBits handle_type =
246 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
247 handle_type <= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
248 handle_type = static_cast<VkExternalSemaphoreHandleTypeFlagBits>(
249 handle_type << 1)) {
250 external_semaphore_info.handleType = handle_type;
251 vkGetPhysicalDeviceExternalSemaphoreProperties(
252 physical_device, &external_semaphore_info,
253 &external_semaphore_properties);
254 if (external_semaphore_properties.exportFromImportedHandleTypes ||
255 external_semaphore_properties.compatibleHandleTypes ||
256 external_semaphore_properties.externalSemaphoreFeatures) {
257 device.external_semaphore_properties.insert(
258 std::make_pair(handle_type, external_semaphore_properties));
259 }
260 }
261 }
262
263 if (device.properties.apiVersion >= VK_API_VERSION_1_2) {
264 device.core12.properties.sType =
265 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES;
266 device.core12.properties.pNext = properties.pNext;
267 properties.pNext = &device.core12.properties;
268
269 vkGetPhysicalDeviceProperties2(physical_device, &properties);
270
271 device.core12.features.sType =
272 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
273 device.core12.features.pNext = features.pNext;
274 features.pNext = &device.core12.features;
275
276 vkGetPhysicalDeviceFeatures2(physical_device, &features);
277 }
278
279 if (device.properties.apiVersion >= VK_API_VERSION_1_3) {
280 device.core13.properties.sType =
281 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_PROPERTIES;
282 device.core13.properties.pNext = properties.pNext;
283 properties.pNext = &device.core13.properties;
284
285 vkGetPhysicalDeviceProperties2(physical_device, &properties);
286
287 device.core13.features.sType =
288 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
289 device.core13.features.pNext = features.pNext;
290 features.pNext = &device.core13.features;
291
292 vkGetPhysicalDeviceFeatures2(physical_device, &features);
293 }
294
295 return device;
296 }
297
VkJsonGetInstance()298 VkJsonInstance VkJsonGetInstance() {
299 VkJsonInstance instance;
300 VkResult result;
301 uint32_t count;
302
303 count = 0;
304 result = vkEnumerateInstanceLayerProperties(&count, nullptr);
305 if (result != VK_SUCCESS)
306 return VkJsonInstance();
307 if (count > 0) {
308 std::vector<VkLayerProperties> layers(count);
309 result = vkEnumerateInstanceLayerProperties(&count, layers.data());
310 if (result != VK_SUCCESS)
311 return VkJsonInstance();
312 instance.layers.reserve(count);
313 for (auto& layer : layers) {
314 instance.layers.push_back(VkJsonLayer{layer, std::vector<VkExtensionProperties>()});
315 if (!EnumerateExtensions(layer.layerName,
316 &instance.layers.back().extensions))
317 return VkJsonInstance();
318 }
319 }
320
321 if (!EnumerateExtensions(nullptr, &instance.extensions))
322 return VkJsonInstance();
323
324 const VkApplicationInfo app_info = {
325 VK_STRUCTURE_TYPE_APPLICATION_INFO,
326 nullptr,
327 "vkjson_info",
328 1,
329 "",
330 0,
331 VK_API_VERSION_1_1,
332 };
333 VkInstanceCreateInfo instance_info = {
334 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
335 nullptr,
336 0,
337 &app_info,
338 0,
339 nullptr,
340 0,
341 nullptr,
342 };
343 VkInstance vkinstance;
344 result = vkCreateInstance(&instance_info, nullptr, &vkinstance);
345 if (result != VK_SUCCESS)
346 return VkJsonInstance();
347
348 count = 0;
349 result = vkEnumeratePhysicalDevices(vkinstance, &count, nullptr);
350 if (result != VK_SUCCESS) {
351 vkDestroyInstance(vkinstance, nullptr);
352 return VkJsonInstance();
353 }
354
355 std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE);
356 result = vkEnumeratePhysicalDevices(vkinstance, &count, devices.data());
357 if (result != VK_SUCCESS) {
358 vkDestroyInstance(vkinstance, nullptr);
359 return VkJsonInstance();
360 }
361
362 std::map<VkPhysicalDevice, uint32_t> device_map;
363 const uint32_t sz = devices.size();
364 instance.devices.reserve(sz);
365 for (uint32_t i = 0; i < sz; ++i) {
366 device_map.insert(std::make_pair(devices[i], i));
367 instance.devices.emplace_back(VkJsonGetDevice(devices[i]));
368 }
369
370 result = vkEnumerateInstanceVersion(&instance.api_version);
371 if (result != VK_SUCCESS) {
372 vkDestroyInstance(vkinstance, nullptr);
373 return VkJsonInstance();
374 }
375
376 count = 0;
377 result = vkEnumeratePhysicalDeviceGroups(vkinstance, &count, nullptr);
378 if (result != VK_SUCCESS) {
379 vkDestroyInstance(vkinstance, nullptr);
380 return VkJsonInstance();
381 }
382
383 VkJsonDeviceGroup device_group;
384 std::vector<VkPhysicalDeviceGroupProperties> group_properties;
385 group_properties.resize(count);
386 for (auto& properties : group_properties) {
387 properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
388 properties.pNext = nullptr;
389 }
390 result = vkEnumeratePhysicalDeviceGroups(vkinstance, &count,
391 group_properties.data());
392 if (result != VK_SUCCESS) {
393 vkDestroyInstance(vkinstance, nullptr);
394 return VkJsonInstance();
395 }
396 for (auto properties : group_properties) {
397 device_group.properties = properties;
398 for (uint32_t i = 0; i < properties.physicalDeviceCount; ++i) {
399 device_group.device_inds.push_back(
400 device_map[properties.physicalDevices[i]]);
401 }
402 instance.device_groups.push_back(device_group);
403 }
404
405 vkDestroyInstance(vkinstance, nullptr);
406 return instance;
407 }
408