vkjson_instance.cc revision 92e5460b8791123cc3329e74d47c2ac03729a9d4
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// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and/or associated documentation files (the "Materials"), to
10// deal in the Materials without restriction, including without limitation the
11// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12// sell copies of the Materials, and to permit persons to whom the Materials are
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice(s) and this permission notice shall be included in
16// all copies or substantial portions of the Materials.
17//
18// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21//
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25// USE OR OTHER DEALINGS IN THE MATERIALS.
26///////////////////////////////////////////////////////////////////////////////
27
28#define VK_PROTOTYPES
29#include "vkjson.h"
30
31#include <utility>
32
33VkJsonDevice VkJsonGetDevice(VkPhysicalDevice physical_device) {
34  VkJsonDevice device;
35  vkGetPhysicalDeviceProperties(physical_device, &device.properties);
36  vkGetPhysicalDeviceFeatures(physical_device, &device.features);
37  vkGetPhysicalDeviceMemoryProperties(physical_device, &device.memory);
38
39  uint32_t queue_family_count = 0;
40  vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count,
41                                           nullptr);
42  if (queue_family_count > 0) {
43    device.queues.resize(queue_family_count);
44    vkGetPhysicalDeviceQueueFamilyProperties(
45        physical_device, &queue_family_count, device.queues.data());
46  }
47
48  // Only device extensions.
49  // TODO(piman): do we want to show layer extensions?
50  uint32_t extension_count = 0;
51  vkEnumerateDeviceExtensionProperties(physical_device, nullptr,
52                                       &extension_count, nullptr);
53  if (extension_count > 0) {
54    device.extensions.resize(extension_count);
55    vkEnumerateDeviceExtensionProperties(
56        physical_device, nullptr, &extension_count, device.extensions.data());
57  }
58
59  uint32_t layer_count = 0;
60  vkEnumerateDeviceLayerProperties(physical_device, &layer_count, nullptr);
61  if (layer_count > 0) {
62    device.layers.resize(layer_count);
63    vkEnumerateDeviceLayerProperties(physical_device, &layer_count,
64                                     device.layers.data());
65  }
66
67  VkFormatProperties format_properties = {0};
68  for (VkFormat format = VK_FORMAT_R4G4_UNORM_PACK8;
69       format <= VK_FORMAT_END_RANGE;
70       format = static_cast<VkFormat>(format + 1)) {
71    vkGetPhysicalDeviceFormatProperties(physical_device, format,
72                                        &format_properties);
73    if (format_properties.linearTilingFeatures ||
74        format_properties.optimalTilingFeatures ||
75        format_properties.bufferFeatures) {
76      device.formats.insert(std::make_pair(format, format_properties));
77    }
78  }
79  return device;
80}
81
82VkJsonInstance VkJsonGetInstance() {
83  VkJsonInstance instance;
84  uint32_t count;
85
86  const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
87                                      nullptr,
88                                      "vkjson_info",
89                                      1,
90                                      "",
91                                      0,
92                                      VK_API_VERSION_1_0};
93  VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
94                                        nullptr,
95                                        0,
96                                        &app_info,
97                                        0,
98                                        nullptr,
99                                        0,
100                                        nullptr};
101  VkInstance vkinstance;
102  VkResult result = vkCreateInstance(&instance_info, nullptr, &vkinstance);
103  if (result != VK_SUCCESS)
104    return VkJsonInstance();
105
106  count = 0;
107  result = vkEnumeratePhysicalDevices(vkinstance, &count, nullptr);
108  if (result != VK_SUCCESS) {
109    vkDestroyInstance(vkinstance, nullptr);
110    return VkJsonInstance();
111  }
112  std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE);
113  result = vkEnumeratePhysicalDevices(vkinstance, &count, devices.data());
114  if (result != VK_SUCCESS) {
115    vkDestroyInstance(vkinstance, nullptr);
116    return VkJsonInstance();
117  }
118
119  instance.devices.reserve(devices.size());
120  for (auto device : devices)
121    instance.devices.emplace_back(VkJsonGetDevice(device));
122
123  vkDestroyInstance(vkinstance, nullptr);
124  return instance;
125}
126