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 VkPhysicalDeviceProperties2 properties2 = {
161 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
162 nullptr,
163 {},
164 };
165
166 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;
170
171 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;
175
176 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;
180
181 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;
185
186 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;
190
191 vkGetPhysicalDeviceProperties2(physical_device, &properties2);
192
193 VkPhysicalDeviceFeatures2 features2 = {
194 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
195 nullptr,
196 {},
197 };
198
199 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;
203
204 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;
208
209 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;
213
214 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;
218
219 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;
223
224 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;
228
229 vkGetPhysicalDeviceFeatures2(physical_device, &features2);
230
231 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 = {};
235
236 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));
249 }
250 }
251
252 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 = {};
256
257 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));
271 }
272 }
273 }
274
275 return device;
276 }
277
VkJsonGetInstance()278 VkJsonInstance 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
304 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 };
313 VkInstanceCreateInfo instance_info = {
314 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
315 nullptr,
316 0,
317 &app_info,
318 0,
319 nullptr,
320 0,
321 nullptr,
322 };
323 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));
347 instance.devices.emplace_back(VkJsonGetDevice(devices[i]));
348 }
349
350 result = vkEnumerateInstanceVersion(&instance.api_version);
351 if (result != VK_SUCCESS) {
352 vkDestroyInstance(vkinstance, nullptr);
353 return VkJsonInstance();
354 }
355
356 count = 0;
357 result = vkEnumeratePhysicalDeviceGroups(vkinstance, &count, nullptr);
358 if (result != VK_SUCCESS) {
359 vkDestroyInstance(vkinstance, nullptr);
360 return VkJsonInstance();
361 }
362
363 VkJsonDeviceGroup device_group;
364 std::vector<VkPhysicalDeviceGroupProperties> group_properties;
365 group_properties.resize(count);
366 for (auto& properties : group_properties) {
367 properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
368 properties.pNext = nullptr;
369 }
370 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]]);
381 }
382 instance.device_groups.push_back(device_group);
383 }
384
385 vkDestroyInstance(vkinstance, nullptr);
386 return instance;
387 }
388