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   return VK_SUCCESS;
49}
50
51void
52vk_physical_device_finish(struct vk_physical_device *physical_device)
53{
54   vk_object_base_finish(&physical_device->base);
55}
56
57VKAPI_ATTR VkResult VKAPI_CALL
58vk_common_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
59                                         uint32_t *pPropertyCount,
60                                         VkLayerProperties *pProperties)
61{
62   if (pProperties == NULL) {
63      *pPropertyCount = 0;
64      return VK_SUCCESS;
65   }
66
67   /* None supported at this time */
68   return VK_ERROR_LAYER_NOT_PRESENT;
69}
70
71VKAPI_ATTR VkResult VKAPI_CALL
72vk_common_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
73                                             const char *pLayerName,
74                                             uint32_t *pPropertyCount,
75                                             VkExtensionProperties *pProperties)
76{
77   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
78   VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
79
80   for (int i = 0; i < VK_DEVICE_EXTENSION_COUNT; i++) {
81      if (!pdevice->supported_extensions.extensions[i])
82         continue;
83
84#ifdef ANDROID
85      if (!vk_android_allowed_device_extensions.extensions[i])
86         continue;
87#endif
88
89      vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
90         *prop = vk_device_extensions[i];
91      }
92   }
93
94   return vk_outarray_status(&out);
95}
96
97VKAPI_ATTR void VKAPI_CALL
98vk_common_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
99                                    VkPhysicalDeviceFeatures *pFeatures)
100{
101   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
102
103   /* Don't zero-init this struct since the driver fills it out entirely */
104   VkPhysicalDeviceFeatures2 features2;
105   features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
106   features2.pNext = NULL;
107
108   pdevice->dispatch_table.GetPhysicalDeviceFeatures2(physicalDevice,
109                                                      &features2);
110   *pFeatures = features2.features;
111}
112
113VKAPI_ATTR void VKAPI_CALL
114vk_common_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
115                                      VkPhysicalDeviceProperties *pProperties)
116{
117   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
118
119   /* Don't zero-init this struct since the driver fills it out entirely */
120   VkPhysicalDeviceProperties2 props2;
121   props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
122   props2.pNext = NULL;
123
124   pdevice->dispatch_table.GetPhysicalDeviceProperties2(physicalDevice,
125                                                        &props2);
126   *pProperties = props2.properties;
127}
128
129VKAPI_ATTR void VKAPI_CALL
130vk_common_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
131                                            VkPhysicalDeviceMemoryProperties *pMemoryProperties)
132{
133   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
134
135   /* Don't zero-init this struct since the driver fills it out entirely */
136   VkPhysicalDeviceMemoryProperties2 props2;
137   props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
138   props2.pNext = NULL;
139
140   pdevice->dispatch_table.GetPhysicalDeviceMemoryProperties2(physicalDevice,
141                                                              &props2);
142   /* dEQP-VK.api.info.get_physical_device_properties2.memory_properties memsets
143    * the struct to 0xcd and expects that the unused array elements are
144    * untouched.
145    */
146   pMemoryProperties->memoryHeapCount = props2.memoryProperties.memoryHeapCount;
147   for (int i = 0; i < pMemoryProperties->memoryHeapCount; i++) {
148      pMemoryProperties->memoryHeaps[i].flags = props2.memoryProperties.memoryHeaps[i].flags;
149      pMemoryProperties->memoryHeaps[i].size = props2.memoryProperties.memoryHeaps[i].size;
150   }
151
152   pMemoryProperties->memoryTypeCount = props2.memoryProperties.memoryTypeCount;
153   for (int i = 0; i < pMemoryProperties->memoryTypeCount; i++) {
154      pMemoryProperties->memoryTypes[i].heapIndex = props2.memoryProperties.memoryTypes[i].heapIndex;
155      pMemoryProperties->memoryTypes[i].propertyFlags = props2.memoryProperties.memoryTypes[i].propertyFlags;
156   }
157}
158
159VKAPI_ATTR void VKAPI_CALL
160vk_common_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
161                                            VkFormat format,
162                                            VkFormatProperties *pFormatProperties)
163{
164   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
165
166   /* Don't zero-init this struct since the driver fills it out entirely */
167   VkFormatProperties2 props2;
168   props2.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;
169   props2.pNext = NULL;
170
171   pdevice->dispatch_table.GetPhysicalDeviceFormatProperties2(physicalDevice,
172                                                              format, &props2);
173   *pFormatProperties = props2.formatProperties;
174}
175
176VKAPI_ATTR VkResult VKAPI_CALL
177vk_common_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,
178                                                 VkFormat format,
179                                                 VkImageType type,
180                                                 VkImageTiling tiling,
181                                                 VkImageUsageFlags usage,
182                                                 VkImageCreateFlags flags,
183                                                 VkImageFormatProperties *pImageFormatProperties)
184{
185   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
186
187   VkPhysicalDeviceImageFormatInfo2 info = {
188      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
189      .format = format,
190      .type = type,
191      .tiling = tiling,
192      .usage = usage,
193      .flags = flags
194   };
195
196   /* Don't zero-init this struct since the driver fills it out entirely */
197   VkImageFormatProperties2 props2;
198   props2.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
199   props2.pNext = NULL;
200
201   VkResult result =
202      pdevice->dispatch_table.GetPhysicalDeviceImageFormatProperties2(physicalDevice,
203                                                                      &info, &props2);
204   *pImageFormatProperties = props2.imageFormatProperties;
205
206   return result;
207}
208
209VKAPI_ATTR void VKAPI_CALL
210vk_common_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,
211                                                       VkFormat format,
212                                                       VkImageType type,
213                                                       uint32_t samples,
214                                                       VkImageUsageFlags usage,
215                                                       VkImageTiling tiling,
216                                                       uint32_t *pNumProperties,
217                                                       VkSparseImageFormatProperties *pProperties)
218{
219   VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
220
221   VkPhysicalDeviceSparseImageFormatInfo2 info = {
222      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SPARSE_IMAGE_FORMAT_INFO_2,
223      .format = format,
224      .type = type,
225      .samples = samples,
226      .usage = usage,
227      .tiling = tiling
228   };
229
230   if (!pProperties) {
231      pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
232                                                                            &info,
233                                                                            pNumProperties,
234                                                                            NULL);
235      return;
236   }
237
238   STACK_ARRAY(VkSparseImageFormatProperties2, props2, *pNumProperties);
239
240   for (unsigned i = 0; i < *pNumProperties; ++i) {
241      props2[i].sType = VK_STRUCTURE_TYPE_SPARSE_IMAGE_FORMAT_PROPERTIES_2;
242      props2[i].pNext = NULL;
243   }
244
245   pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
246                                                                         &info,
247                                                                         pNumProperties,
248                                                                         props2);
249
250   for (unsigned i = 0; i < *pNumProperties; ++i)
251      pProperties[i] = props2[i].properties;
252
253   STACK_ARRAY_FINISH(props2);
254}
255