1/*
2 * Copyright © 2021 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "vk_physical_device.h"
25
26#include "vk_common_entrypoints.h"
27#include "vk_util.h"
28
29VkResult
30vk_physical_device_init(struct vk_physical_device *pdevice,
31                        struct vk_instance *instance,
32                        const struct vk_device_extension_table *supported_extensions,
33                        const struct vk_physical_device_dispatch_table *dispatch_table)
34{
35   memset(pdevice, 0, sizeof(*pdevice));
36   vk_object_base_init(NULL, &pdevice->base, VK_OBJECT_TYPE_PHYSICAL_DEVICE);
37   pdevice->instance = instance;
38
39   if (supported_extensions != NULL)
40      pdevice->supported_extensions = *supported_extensions;
41
42   pdevice->dispatch_table = *dispatch_table;
43
44   /* Add common entrypoints without overwriting driver-provided ones. */
45   vk_physical_device_dispatch_table_from_entrypoints(
46      &pdevice->dispatch_table, &vk_common_physical_device_entrypoints, false);
47
48   /* TODO */
49   pdevice->disk_cache = NULL;
50
51   return VK_SUCCESS;
52}
53
54void
55vk_physical_device_finish(struct vk_physical_device *physical_device)
56{
57   vk_object_base_finish(&physical_device->base);
58}
59
60VKAPI_ATTR VkResult VKAPI_CALL
61vk_common_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
62                                         uint32_t *pPropertyCount,
63                                         VkLayerProperties *pProperties)
64{
65   if (pProperties == NULL) {
66      *pPropertyCount = 0;
67      return VK_SUCCESS;
68   }
69
70   /* None supported at this time */
71   return VK_ERROR_LAYER_NOT_PRESENT;
72}
73
74VKAPI_ATTR VkResult VKAPI_CALL
75vk_common_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
76                                             const char *pLayerName,
77                                             uint32_t *pPropertyCount,
78                                             VkExtensionProperties *pProperties)
79{
80   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
81   VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
82
83   for (int i = 0; i < VK_DEVICE_EXTENSION_COUNT; i++) {
84      if (!pdevice->supported_extensions.extensions[i])
85         continue;
86
87#ifdef ANDROID
88      if (!vk_android_allowed_device_extensions.extensions[i])
89         continue;
90#endif
91
92      vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
93         *prop = vk_device_extensions[i];
94      }
95   }
96
97   return vk_outarray_status(&out);
98}
99
100VKAPI_ATTR void VKAPI_CALL
101vk_common_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
102                                    VkPhysicalDeviceFeatures *pFeatures)
103{
104   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
105
106   /* Don't zero-init this struct since the driver fills it out entirely */
107   VkPhysicalDeviceFeatures2 features2;
108   features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
109   features2.pNext = NULL;
110
111   pdevice->dispatch_table.GetPhysicalDeviceFeatures2(physicalDevice,
112                                                      &features2);
113   *pFeatures = features2.features;
114}
115
116VKAPI_ATTR void VKAPI_CALL
117vk_common_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
118                                      VkPhysicalDeviceProperties *pProperties)
119{
120   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
121
122   /* Don't zero-init this struct since the driver fills it out entirely */
123   VkPhysicalDeviceProperties2 props2;
124   props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
125   props2.pNext = NULL;
126
127   pdevice->dispatch_table.GetPhysicalDeviceProperties2(physicalDevice,
128                                                        &props2);
129   *pProperties = props2.properties;
130}
131
132VKAPI_ATTR void VKAPI_CALL
133vk_common_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
134                                                 uint32_t *pQueueFamilyPropertyCount,
135                                                 VkQueueFamilyProperties *pQueueFamilyProperties)
136{
137   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
138
139   if (!pQueueFamilyProperties) {
140      pdevice->dispatch_table.GetPhysicalDeviceQueueFamilyProperties2(physicalDevice,
141                                                                      pQueueFamilyPropertyCount,
142                                                                      NULL);
143      return;
144   }
145
146   STACK_ARRAY(VkQueueFamilyProperties2, props2, *pQueueFamilyPropertyCount);
147
148   for (unsigned i = 0; i < *pQueueFamilyPropertyCount; ++i) {
149      props2[i].sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2;
150      props2[i].pNext = NULL;
151   }
152
153   pdevice->dispatch_table.GetPhysicalDeviceQueueFamilyProperties2(physicalDevice,
154                                                                   pQueueFamilyPropertyCount,
155                                                                   props2);
156
157   for (unsigned i = 0; i < *pQueueFamilyPropertyCount; ++i)
158      pQueueFamilyProperties[i] = props2[i].queueFamilyProperties;
159
160   STACK_ARRAY_FINISH(props2);
161}
162
163VKAPI_ATTR void VKAPI_CALL
164vk_common_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
165                                            VkPhysicalDeviceMemoryProperties *pMemoryProperties)
166{
167   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
168
169   /* Don't zero-init this struct since the driver fills it out entirely */
170   VkPhysicalDeviceMemoryProperties2 props2;
171   props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
172   props2.pNext = NULL;
173
174   pdevice->dispatch_table.GetPhysicalDeviceMemoryProperties2(physicalDevice,
175                                                              &props2);
176   *pMemoryProperties = props2.memoryProperties;
177}
178
179VKAPI_ATTR void VKAPI_CALL
180vk_common_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
181                                            VkFormat format,
182                                            VkFormatProperties *pFormatProperties)
183{
184   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
185
186   /* Don't zero-init this struct since the driver fills it out entirely */
187   VkFormatProperties2 props2;
188   props2.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;
189   props2.pNext = NULL;
190
191   pdevice->dispatch_table.GetPhysicalDeviceFormatProperties2(physicalDevice,
192                                                              format, &props2);
193   *pFormatProperties = props2.formatProperties;
194}
195
196VKAPI_ATTR VkResult VKAPI_CALL
197vk_common_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,
198                                                 VkFormat format,
199                                                 VkImageType type,
200                                                 VkImageTiling tiling,
201                                                 VkImageUsageFlags usage,
202                                                 VkImageCreateFlags flags,
203                                                 VkImageFormatProperties *pImageFormatProperties)
204{
205   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
206
207   VkPhysicalDeviceImageFormatInfo2 info = {
208      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
209      .format = format,
210      .type = type,
211      .tiling = tiling,
212      .usage = usage,
213      .flags = flags
214   };
215
216   /* Don't zero-init this struct since the driver fills it out entirely */
217   VkImageFormatProperties2 props2;
218   props2.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
219   props2.pNext = NULL;
220
221   VkResult result =
222      pdevice->dispatch_table.GetPhysicalDeviceImageFormatProperties2(physicalDevice,
223                                                                      &info, &props2);
224   *pImageFormatProperties = props2.imageFormatProperties;
225
226   return result;
227}
228
229VKAPI_ATTR void VKAPI_CALL
230vk_common_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,
231                                                       VkFormat format,
232                                                       VkImageType type,
233                                                       uint32_t samples,
234                                                       VkImageUsageFlags usage,
235                                                       VkImageTiling tiling,
236                                                       uint32_t *pNumProperties,
237                                                       VkSparseImageFormatProperties *pProperties)
238{
239   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
240
241   VkPhysicalDeviceSparseImageFormatInfo2 info = {
242      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SPARSE_IMAGE_FORMAT_INFO_2,
243      .format = format,
244      .type = type,
245      .samples = samples,
246      .usage = usage,
247      .tiling = tiling
248   };
249
250   if (!pProperties) {
251      pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
252                                                                            &info,
253                                                                            pNumProperties,
254                                                                            NULL);
255      return;
256   }
257
258   STACK_ARRAY(VkSparseImageFormatProperties2, props2, *pNumProperties);
259
260   for (unsigned i = 0; i < *pNumProperties; ++i) {
261      props2[i].sType = VK_STRUCTURE_TYPE_SPARSE_IMAGE_FORMAT_PROPERTIES_2;
262      props2[i].pNext = NULL;
263   }
264
265   pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
266                                                                         &info,
267                                                                         pNumProperties,
268                                                                         props2);
269
270   for (unsigned i = 0; i < *pNumProperties; ++i)
271      pProperties[i] = props2[i].properties;
272
273   STACK_ARRAY_FINISH(props2);
274}
275
276/* VK_EXT_tooling_info */
277VKAPI_ATTR VkResult VKAPI_CALL
278vk_common_GetPhysicalDeviceToolProperties(VkPhysicalDevice physicalDevice,
279                                          uint32_t *pToolCount,
280                                          VkPhysicalDeviceToolProperties *pToolProperties)
281{
282   VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceToolProperties, out, pToolProperties, pToolCount);
283
284   return vk_outarray_status(&out);
285}
286