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 
29 VkResult
vk_physical_device_init(struct vk_physical_device * pdevice,struct vk_instance * instance,const struct vk_device_extension_table * supported_extensions,const struct vk_features * supported_features,const struct vk_properties * properties,const struct vk_physical_device_dispatch_table * dispatch_table)30 vk_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_features *supported_features,
34                         const struct vk_properties *properties,
35                         const struct vk_physical_device_dispatch_table *dispatch_table)
36 {
37    memset(pdevice, 0, sizeof(*pdevice));
38    vk_object_base_init(NULL, &pdevice->base, VK_OBJECT_TYPE_PHYSICAL_DEVICE);
39    pdevice->instance = instance;
40 
41    if (supported_extensions != NULL)
42       pdevice->supported_extensions = *supported_extensions;
43 
44    if (supported_features != NULL)
45       pdevice->supported_features = *supported_features;
46 
47    if (properties != NULL)
48       pdevice->properties = *properties;
49 
50    pdevice->dispatch_table = *dispatch_table;
51 
52    /* Add common entrypoints without overwriting driver-provided ones. */
53    vk_physical_device_dispatch_table_from_entrypoints(
54       &pdevice->dispatch_table, &vk_common_physical_device_entrypoints, false);
55 
56    /* TODO */
57    pdevice->disk_cache = NULL;
58 
59    return VK_SUCCESS;
60 }
61 
62 void
vk_physical_device_finish(struct vk_physical_device * physical_device)63 vk_physical_device_finish(struct vk_physical_device *physical_device)
64 {
65    vk_object_base_finish(&physical_device->base);
66 }
67 
68 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,uint32_t * pPropertyCount,VkLayerProperties * pProperties)69 vk_common_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
70                                          uint32_t *pPropertyCount,
71                                          VkLayerProperties *pProperties)
72 {
73    if (pProperties == NULL) {
74       *pPropertyCount = 0;
75       return VK_SUCCESS;
76    }
77 
78    /* None supported at this time */
79    return VK_ERROR_LAYER_NOT_PRESENT;
80 }
81 
82 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)83 vk_common_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
84                                              const char *pLayerName,
85                                              uint32_t *pPropertyCount,
86                                              VkExtensionProperties *pProperties)
87 {
88    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
89    VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
90 
91    for (int i = 0; i < VK_DEVICE_EXTENSION_COUNT; i++) {
92       if (!pdevice->supported_extensions.extensions[i])
93          continue;
94 
95 #ifdef ANDROID
96       if (!vk_android_allowed_device_extensions.extensions[i])
97          continue;
98 #endif
99 
100       vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
101          *prop = vk_device_extensions[i];
102       }
103    }
104 
105    return vk_outarray_status(&out);
106 }
107 
108 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,VkPhysicalDeviceFeatures * pFeatures)109 vk_common_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
110                                     VkPhysicalDeviceFeatures *pFeatures)
111 {
112    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
113 
114    /* Don't zero-init this struct since the driver fills it out entirely */
115    VkPhysicalDeviceFeatures2 features2;
116    features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
117    features2.pNext = NULL;
118 
119    pdevice->dispatch_table.GetPhysicalDeviceFeatures2(physicalDevice,
120                                                       &features2);
121    *pFeatures = features2.features;
122 }
123 
124 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,VkPhysicalDeviceProperties * pProperties)125 vk_common_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
126                                       VkPhysicalDeviceProperties *pProperties)
127 {
128    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
129 
130    /* Don't zero-init this struct since the driver fills it out entirely */
131    VkPhysicalDeviceProperties2 props2;
132    props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
133    props2.pNext = NULL;
134 
135    pdevice->dispatch_table.GetPhysicalDeviceProperties2(physicalDevice,
136                                                         &props2);
137    *pProperties = props2.properties;
138 }
139 
140 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,uint32_t * pQueueFamilyPropertyCount,VkQueueFamilyProperties * pQueueFamilyProperties)141 vk_common_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
142                                                  uint32_t *pQueueFamilyPropertyCount,
143                                                  VkQueueFamilyProperties *pQueueFamilyProperties)
144 {
145    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
146 
147    if (!pQueueFamilyProperties) {
148       pdevice->dispatch_table.GetPhysicalDeviceQueueFamilyProperties2(physicalDevice,
149                                                                       pQueueFamilyPropertyCount,
150                                                                       NULL);
151       return;
152    }
153 
154    STACK_ARRAY(VkQueueFamilyProperties2, props2, *pQueueFamilyPropertyCount);
155 
156    for (unsigned i = 0; i < *pQueueFamilyPropertyCount; ++i) {
157       props2[i].sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2;
158       props2[i].pNext = NULL;
159    }
160 
161    pdevice->dispatch_table.GetPhysicalDeviceQueueFamilyProperties2(physicalDevice,
162                                                                    pQueueFamilyPropertyCount,
163                                                                    props2);
164 
165    for (unsigned i = 0; i < *pQueueFamilyPropertyCount; ++i)
166       pQueueFamilyProperties[i] = props2[i].queueFamilyProperties;
167 
168    STACK_ARRAY_FINISH(props2);
169 }
170 
171 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,VkPhysicalDeviceMemoryProperties * pMemoryProperties)172 vk_common_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
173                                             VkPhysicalDeviceMemoryProperties *pMemoryProperties)
174 {
175    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
176 
177    /* Don't zero-init this struct since the driver fills it out entirely */
178    VkPhysicalDeviceMemoryProperties2 props2;
179    props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
180    props2.pNext = NULL;
181 
182    pdevice->dispatch_table.GetPhysicalDeviceMemoryProperties2(physicalDevice,
183                                                               &props2);
184    *pMemoryProperties = props2.memoryProperties;
185 }
186 
187 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties * pFormatProperties)188 vk_common_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
189                                             VkFormat format,
190                                             VkFormatProperties *pFormatProperties)
191 {
192    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
193 
194    /* Don't zero-init this struct since the driver fills it out entirely */
195    VkFormatProperties2 props2;
196    props2.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;
197    props2.pNext = NULL;
198 
199    pdevice->dispatch_table.GetPhysicalDeviceFormatProperties2(physicalDevice,
200                                                               format, &props2);
201    *pFormatProperties = props2.formatProperties;
202 }
203 
204 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags flags,VkImageFormatProperties * pImageFormatProperties)205 vk_common_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,
206                                                  VkFormat format,
207                                                  VkImageType type,
208                                                  VkImageTiling tiling,
209                                                  VkImageUsageFlags usage,
210                                                  VkImageCreateFlags flags,
211                                                  VkImageFormatProperties *pImageFormatProperties)
212 {
213    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
214 
215    VkPhysicalDeviceImageFormatInfo2 info = {
216       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
217       .format = format,
218       .type = type,
219       .tiling = tiling,
220       .usage = usage,
221       .flags = flags
222    };
223 
224    /* Don't zero-init this struct since the driver fills it out entirely */
225    VkImageFormatProperties2 props2;
226    props2.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
227    props2.pNext = NULL;
228 
229    VkResult result =
230       pdevice->dispatch_table.GetPhysicalDeviceImageFormatProperties2(physicalDevice,
231                                                                       &info, &props2);
232    *pImageFormatProperties = props2.imageFormatProperties;
233 
234    return result;
235 }
236 
237 VKAPI_ATTR void VKAPI_CALL
vk_common_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkSampleCountFlagBits samples,VkImageUsageFlags usage,VkImageTiling tiling,uint32_t * pNumProperties,VkSparseImageFormatProperties * pProperties)238 vk_common_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,
239                                                        VkFormat format,
240                                                        VkImageType type,
241                                                        VkSampleCountFlagBits samples,
242                                                        VkImageUsageFlags usage,
243                                                        VkImageTiling tiling,
244                                                        uint32_t *pNumProperties,
245                                                        VkSparseImageFormatProperties *pProperties)
246 {
247    VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
248 
249    VkPhysicalDeviceSparseImageFormatInfo2 info = {
250       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SPARSE_IMAGE_FORMAT_INFO_2,
251       .format = format,
252       .type = type,
253       .samples = samples,
254       .usage = usage,
255       .tiling = tiling
256    };
257 
258    if (!pProperties) {
259       pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
260                                                                             &info,
261                                                                             pNumProperties,
262                                                                             NULL);
263       return;
264    }
265 
266    STACK_ARRAY(VkSparseImageFormatProperties2, props2, *pNumProperties);
267 
268    for (unsigned i = 0; i < *pNumProperties; ++i) {
269       props2[i].sType = VK_STRUCTURE_TYPE_SPARSE_IMAGE_FORMAT_PROPERTIES_2;
270       props2[i].pNext = NULL;
271    }
272 
273    pdevice->dispatch_table.GetPhysicalDeviceSparseImageFormatProperties2(physicalDevice,
274                                                                          &info,
275                                                                          pNumProperties,
276                                                                          props2);
277 
278    for (unsigned i = 0; i < *pNumProperties; ++i)
279       pProperties[i] = props2[i].properties;
280 
281    STACK_ARRAY_FINISH(props2);
282 }
283 
284 /* VK_EXT_tooling_info */
285 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetPhysicalDeviceToolProperties(VkPhysicalDevice physicalDevice,uint32_t * pToolCount,VkPhysicalDeviceToolProperties * pToolProperties)286 vk_common_GetPhysicalDeviceToolProperties(VkPhysicalDevice physicalDevice,
287                                           uint32_t *pToolCount,
288                                           VkPhysicalDeviceToolProperties *pToolProperties)
289 {
290    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceToolProperties, out, pToolProperties, pToolCount);
291 
292    return vk_outarray_status(&out);
293 }
294