Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 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 | const char* kSupportedInstanceExtensions[] = { |
| 32 | "VK_KHR_get_physical_device_properties2"}; |
| 33 | |
| 34 | bool 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 | |
| 49 | bool 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 | |
| 58 | bool 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 | |
| 68 | VkJsonDevice VkJsonGetDevice(VkInstance instance, |
| 69 | VkPhysicalDevice physical_device, |
| 70 | uint32_t instance_extension_count, |
| 71 | const char* const* instance_extensions) { |
| 72 | VkJsonDevice device; |
| 73 | |
Yiwei Zhang | 7b4169d | 2018-10-02 18:58:31 -0700 | [diff] [blame] | 74 | PFN_vkGetPhysicalDeviceProperties2KHR vkpGetPhysicalDeviceProperties2KHR = |
| 75 | nullptr; |
Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 76 | 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 Zhang | 7b4169d | 2018-10-02 18:58:31 -0700 | [diff] [blame] | 81 | vkpGetPhysicalDeviceProperties2KHR = |
| 82 | reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2KHR>( |
| 83 | vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2KHR")); |
Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 84 | 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 Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 106 | if (HasExtension("VK_KHR_get_physical_device_properties2", |
| 107 | instance_extension_count, instance_extensions)) { |
Yiwei Zhang | 7b4169d | 2018-10-02 18:58:31 -0700 | [diff] [blame] | 108 | 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 Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 125 | 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 Zhang | 7b4169d | 2018-10-02 18:58:31 -0700 | [diff] [blame] | 131 | device.ext_variable_pointer_features.reported = true; |
Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 132 | 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 Lin | c1b5ffb | 2020-02-27 19:31:51 -0800 | [diff] [blame] | 139 | 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 Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 149 | vkpGetPhysicalDeviceFeatures2KHR(physical_device, &features); |
| 150 | device.features = features.features; |
| 151 | } else { |
Yiwei Zhang | 7b4169d | 2018-10-02 18:58:31 -0700 | [diff] [blame] | 152 | vkGetPhysicalDeviceProperties(physical_device, &device.properties); |
Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 153 | 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; |
| 168 | format <= VK_FORMAT_END_RANGE; |
| 169 | format = static_cast<VkFormat>(format + 1)) { |
| 170 | vkGetPhysicalDeviceFormatProperties(physical_device, format, |
| 171 | &format_properties); |
| 172 | if (format_properties.linearTilingFeatures || |
| 173 | format_properties.optimalTilingFeatures || |
| 174 | format_properties.bufferFeatures) { |
| 175 | device.formats.insert(std::make_pair(format, format_properties)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (device.properties.apiVersion >= VK_API_VERSION_1_1) { |
| 180 | for (VkFormat format = VK_FORMAT_G8B8G8R8_422_UNORM; |
| 181 | format <= VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM; |
| 182 | format = static_cast<VkFormat>(format + 1)) { |
| 183 | vkGetPhysicalDeviceFormatProperties(physical_device, format, |
| 184 | &format_properties); |
| 185 | if (format_properties.linearTilingFeatures || |
| 186 | format_properties.optimalTilingFeatures || |
| 187 | format_properties.bufferFeatures) { |
| 188 | device.formats.insert(std::make_pair(format, format_properties)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | PFN_vkGetPhysicalDeviceProperties2 vkpGetPhysicalDeviceProperties2 = |
| 193 | reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>( |
| 194 | vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2")); |
| 195 | if (vkpGetPhysicalDeviceProperties2) { |
| 196 | VkPhysicalDeviceProperties2 properties2 = { |
| 197 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, nullptr, {}}; |
| 198 | |
| 199 | device.subgroup_properties.sType = |
| 200 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES; |
| 201 | device.subgroup_properties.pNext = properties2.pNext; |
| 202 | properties2.pNext = &device.subgroup_properties; |
| 203 | |
| 204 | device.point_clipping_properties.sType = |
| 205 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES; |
| 206 | device.point_clipping_properties.pNext = properties2.pNext; |
| 207 | properties2.pNext = &device.point_clipping_properties; |
| 208 | |
| 209 | device.multiview_properties.sType = |
| 210 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES; |
| 211 | device.multiview_properties.pNext = properties2.pNext; |
| 212 | properties2.pNext = &device.multiview_properties; |
| 213 | |
| 214 | device.id_properties.sType = |
| 215 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES; |
| 216 | device.id_properties.pNext = properties2.pNext; |
| 217 | properties2.pNext = &device.id_properties; |
| 218 | |
| 219 | device.maintenance3_properties.sType = |
| 220 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES; |
| 221 | device.maintenance3_properties.pNext = properties2.pNext; |
| 222 | properties2.pNext = &device.maintenance3_properties; |
| 223 | |
| 224 | (*vkpGetPhysicalDeviceProperties2)(physical_device, &properties2); |
| 225 | } |
| 226 | |
| 227 | PFN_vkGetPhysicalDeviceFeatures2 vkpGetPhysicalDeviceFeatures2 = |
| 228 | reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures2>( |
| 229 | vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2")); |
| 230 | if (vkpGetPhysicalDeviceFeatures2) { |
| 231 | VkPhysicalDeviceFeatures2 features2 = { |
| 232 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, nullptr, {}}; |
| 233 | |
| 234 | device.bit16_storage_features.sType = |
| 235 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES; |
| 236 | device.bit16_storage_features.pNext = features2.pNext; |
| 237 | features2.pNext = &device.bit16_storage_features; |
| 238 | |
| 239 | device.multiview_features.sType = |
| 240 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES; |
| 241 | device.multiview_features.pNext = features2.pNext; |
| 242 | features2.pNext = &device.multiview_features; |
| 243 | |
| 244 | device.variable_pointer_features.sType = |
| 245 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES; |
| 246 | device.variable_pointer_features.pNext = features2.pNext; |
| 247 | features2.pNext = &device.variable_pointer_features; |
| 248 | |
| 249 | device.protected_memory_features.sType = |
| 250 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES; |
| 251 | device.protected_memory_features.pNext = features2.pNext; |
| 252 | features2.pNext = &device.protected_memory_features; |
| 253 | |
| 254 | device.sampler_ycbcr_conversion_features.sType = |
| 255 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES; |
| 256 | device.sampler_ycbcr_conversion_features.pNext = features2.pNext; |
| 257 | features2.pNext = &device.sampler_ycbcr_conversion_features; |
| 258 | |
| 259 | device.shader_draw_parameter_features.sType = |
| 260 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES; |
| 261 | device.shader_draw_parameter_features.pNext = features2.pNext; |
| 262 | features2.pNext = &device.shader_draw_parameter_features; |
| 263 | |
| 264 | (*vkpGetPhysicalDeviceFeatures2)(physical_device, &features2); |
| 265 | } |
| 266 | |
| 267 | PFN_vkGetPhysicalDeviceExternalFenceProperties |
| 268 | vkpGetPhysicalDeviceExternalFenceProperties = |
| 269 | reinterpret_cast<PFN_vkGetPhysicalDeviceExternalFenceProperties>( |
| 270 | vkGetInstanceProcAddr( |
| 271 | instance, "vkGetPhysicalDeviceExternalFenceProperties")); |
| 272 | if (vkpGetPhysicalDeviceExternalFenceProperties) { |
| 273 | VkPhysicalDeviceExternalFenceInfo external_fence_info = { |
| 274 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO, nullptr, |
| 275 | VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT}; |
| 276 | VkExternalFenceProperties external_fence_properties = {}; |
| 277 | |
| 278 | for (VkExternalFenceHandleTypeFlagBits handle_type = |
| 279 | VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT; |
| 280 | handle_type <= VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT; |
| 281 | handle_type = static_cast<VkExternalFenceHandleTypeFlagBits>( |
| 282 | handle_type << 1)) { |
| 283 | external_fence_info.handleType = handle_type; |
| 284 | (*vkpGetPhysicalDeviceExternalFenceProperties)( |
| 285 | physical_device, &external_fence_info, &external_fence_properties); |
| 286 | if (external_fence_properties.exportFromImportedHandleTypes || |
| 287 | external_fence_properties.compatibleHandleTypes || |
| 288 | external_fence_properties.externalFenceFeatures) { |
| 289 | device.external_fence_properties.insert( |
| 290 | std::make_pair(handle_type, external_fence_properties)); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | PFN_vkGetPhysicalDeviceExternalSemaphoreProperties |
| 296 | vkpGetPhysicalDeviceExternalSemaphoreProperties = reinterpret_cast< |
| 297 | PFN_vkGetPhysicalDeviceExternalSemaphoreProperties>( |
| 298 | vkGetInstanceProcAddr( |
| 299 | instance, "vkGetPhysicalDeviceExternalSemaphoreProperties")); |
| 300 | if (vkpGetPhysicalDeviceExternalSemaphoreProperties) { |
| 301 | VkPhysicalDeviceExternalSemaphoreInfo external_semaphore_info = { |
| 302 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, nullptr, |
| 303 | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT}; |
| 304 | VkExternalSemaphoreProperties external_semaphore_properties = {}; |
| 305 | |
| 306 | for (VkExternalSemaphoreHandleTypeFlagBits handle_type = |
| 307 | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT; |
| 308 | handle_type <= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 309 | handle_type = static_cast<VkExternalSemaphoreHandleTypeFlagBits>( |
| 310 | handle_type << 1)) { |
| 311 | external_semaphore_info.handleType = handle_type; |
| 312 | (*vkpGetPhysicalDeviceExternalSemaphoreProperties)( |
| 313 | physical_device, &external_semaphore_info, |
| 314 | &external_semaphore_properties); |
| 315 | if (external_semaphore_properties.exportFromImportedHandleTypes || |
| 316 | external_semaphore_properties.compatibleHandleTypes || |
| 317 | external_semaphore_properties.externalSemaphoreFeatures) { |
| 318 | device.external_semaphore_properties.insert( |
| 319 | std::make_pair(handle_type, external_semaphore_properties)); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return device; |
| 326 | } |
| 327 | |
| 328 | VkJsonInstance VkJsonGetInstance() { |
| 329 | VkJsonInstance instance; |
| 330 | VkResult result; |
| 331 | uint32_t count; |
| 332 | |
| 333 | count = 0; |
| 334 | result = vkEnumerateInstanceLayerProperties(&count, nullptr); |
| 335 | if (result != VK_SUCCESS) |
| 336 | return VkJsonInstance(); |
| 337 | if (count > 0) { |
| 338 | std::vector<VkLayerProperties> layers(count); |
| 339 | result = vkEnumerateInstanceLayerProperties(&count, layers.data()); |
| 340 | if (result != VK_SUCCESS) |
| 341 | return VkJsonInstance(); |
| 342 | instance.layers.reserve(count); |
| 343 | for (auto& layer : layers) { |
| 344 | instance.layers.push_back(VkJsonLayer{layer, std::vector<VkExtensionProperties>()}); |
| 345 | if (!EnumerateExtensions(layer.layerName, |
| 346 | &instance.layers.back().extensions)) |
| 347 | return VkJsonInstance(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (!EnumerateExtensions(nullptr, &instance.extensions)) |
| 352 | return VkJsonInstance(); |
| 353 | |
| 354 | std::vector<const char*> instance_extensions; |
| 355 | for (const auto extension : kSupportedInstanceExtensions) { |
| 356 | if (HasExtension(extension, instance.extensions)) |
| 357 | instance_extensions.push_back(extension); |
| 358 | } |
| 359 | |
| 360 | const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO, |
| 361 | nullptr, |
| 362 | "vkjson_info", |
| 363 | 1, |
| 364 | "", |
| 365 | 0, |
Tapani Pälli | a871423 | 2018-10-16 09:43:46 +0300 | [diff] [blame] | 366 | VK_API_VERSION_1_1}; |
Yiwei Zhang | f9a57e6 | 2018-04-05 00:17:22 -0700 | [diff] [blame] | 367 | VkInstanceCreateInfo instance_info = { |
| 368 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
| 369 | nullptr, |
| 370 | 0, |
| 371 | &app_info, |
| 372 | 0, |
| 373 | nullptr, |
| 374 | static_cast<uint32_t>(instance_extensions.size()), |
| 375 | instance_extensions.data()}; |
| 376 | VkInstance vkinstance; |
| 377 | result = vkCreateInstance(&instance_info, nullptr, &vkinstance); |
| 378 | if (result != VK_SUCCESS) |
| 379 | return VkJsonInstance(); |
| 380 | |
| 381 | count = 0; |
| 382 | result = vkEnumeratePhysicalDevices(vkinstance, &count, nullptr); |
| 383 | if (result != VK_SUCCESS) { |
| 384 | vkDestroyInstance(vkinstance, nullptr); |
| 385 | return VkJsonInstance(); |
| 386 | } |
| 387 | |
| 388 | std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE); |
| 389 | result = vkEnumeratePhysicalDevices(vkinstance, &count, devices.data()); |
| 390 | if (result != VK_SUCCESS) { |
| 391 | vkDestroyInstance(vkinstance, nullptr); |
| 392 | return VkJsonInstance(); |
| 393 | } |
| 394 | |
| 395 | std::map<VkPhysicalDevice, uint32_t> device_map; |
| 396 | const uint32_t sz = devices.size(); |
| 397 | instance.devices.reserve(sz); |
| 398 | for (uint32_t i = 0; i < sz; ++i) { |
| 399 | device_map.insert(std::make_pair(devices[i], i)); |
| 400 | instance.devices.emplace_back(VkJsonGetDevice(vkinstance, devices[i], |
| 401 | instance_extensions.size(), |
| 402 | instance_extensions.data())); |
| 403 | } |
| 404 | |
| 405 | PFN_vkEnumerateInstanceVersion vkpEnumerateInstanceVersion = |
| 406 | reinterpret_cast<PFN_vkEnumerateInstanceVersion>( |
| 407 | vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion")); |
| 408 | if (!vkpEnumerateInstanceVersion) { |
| 409 | instance.api_version = VK_API_VERSION_1_0; |
| 410 | } else { |
| 411 | result = (*vkpEnumerateInstanceVersion)(&instance.api_version); |
| 412 | if (result != VK_SUCCESS) { |
| 413 | vkDestroyInstance(vkinstance, nullptr); |
| 414 | return VkJsonInstance(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | PFN_vkEnumeratePhysicalDeviceGroups vkpEnumeratePhysicalDeviceGroups = |
| 419 | reinterpret_cast<PFN_vkEnumeratePhysicalDeviceGroups>( |
| 420 | vkGetInstanceProcAddr(vkinstance, "vkEnumeratePhysicalDeviceGroups")); |
| 421 | if (vkpEnumeratePhysicalDeviceGroups) { |
| 422 | count = 0; |
| 423 | result = (*vkpEnumeratePhysicalDeviceGroups)(vkinstance, &count, nullptr); |
| 424 | if (result != VK_SUCCESS) { |
| 425 | vkDestroyInstance(vkinstance, nullptr); |
| 426 | return VkJsonInstance(); |
| 427 | } |
| 428 | |
| 429 | VkJsonDeviceGroup device_group; |
| 430 | std::vector<VkPhysicalDeviceGroupProperties> group_properties; |
| 431 | group_properties.resize(count); |
| 432 | result = (*vkpEnumeratePhysicalDeviceGroups)(vkinstance, &count, |
| 433 | group_properties.data()); |
| 434 | if (result != VK_SUCCESS) { |
| 435 | vkDestroyInstance(vkinstance, nullptr); |
| 436 | return VkJsonInstance(); |
| 437 | } |
| 438 | for (auto properties : group_properties) { |
| 439 | device_group.properties = properties; |
| 440 | for (uint32_t i = 0; i < properties.physicalDeviceCount; ++i) { |
| 441 | device_group.device_inds.push_back( |
| 442 | device_map[properties.physicalDevices[i]]); |
| 443 | } |
| 444 | instance.device_groups.push_back(device_group); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | vkDestroyInstance(vkinstance, nullptr); |
| 449 | return instance; |
| 450 | } |