089aa36cee0e27b829fc2fbaf48d41246210bf16
[mesa.git] / src / libre-soc / vulkan / libresoc_device.c
1 /*
2 * Copyright © 2019 Raspberry Pi
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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/sysinfo.h>
29 #include <unistd.h>
30
31 #include "libresoc_private.h"
32 #include "vk_util.h"
33
34 VkResult
35 libresoc_EnumerateInstanceExtensionProperties(const char *pLayerName,
36 uint32_t *pPropertyCount,
37 VkExtensionProperties *pProperties)
38 {
39 if (getenv("LIBRESOC_TRACE")) {
40 fprintf(stderr, "EnumerateInstanceExtensionProperties called for: %s \n", pLayerName);
41 }
42 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
43
44 for (int i = 0; i < LIBRESOC_INSTANCE_EXTENSION_COUNT; i++) {
45 if (libresoc_instance_extensions_supported.extensions[i]) {
46 vk_outarray_append(&out, prop) {
47 *prop = libresoc_instance_extensions[i];
48 }
49 }
50 }
51
52 return vk_outarray_status(&out);
53 }
54
55 VkResult
56 libresoc_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
57 const VkAllocationCallbacks *pAllocator,
58 VkInstance *pInstance)
59 {
60 if (getenv("LIBRESOC_TRACE")) {
61 fprintf(stderr, "CreateInstance called. \n");
62 }
63 /* FIXME: stub */
64 return VK_SUCCESS;
65 }
66
67 void
68 libresoc_DestroyInstance(VkInstance _instance,
69 const VkAllocationCallbacks *pAllocator)
70 {
71 if (getenv("LIBRESOC_TRACE")) {
72 fprintf(stderr, "DestroyInstance called. \n");
73 }
74 /* FIXME: stub */
75 }
76
77 VkResult
78 libresoc_EnumeratePhysicalDevices(VkInstance _instance,
79 uint32_t *pPhysicalDeviceCount,
80 VkPhysicalDevice *pPhysicalDevices)
81 {
82 if (getenv("LIBRESOC_TRACE")) {
83 fprintf(stderr, "EnumeratePhysicalDevices called\n");
84 }
85 /* FIXME: stub */
86 return VK_SUCCESS;
87 }
88
89 void
90 libresoc_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
91 VkPhysicalDeviceFeatures *pFeatures)
92 {
93 if (getenv("LIBRESOC_TRACE")) {
94 fprintf(stderr, "GetPhysicalDeviceFeatures called. \n");
95 }
96 /* FIXME: stub */
97 }
98
99 void
100 libresoc_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
101 VkPhysicalDeviceProperties *pProperties)
102 {
103 if (getenv("LIBRESOC_TRACE")) {
104 fprintf(stderr, "GetPhysicalDeviceProperties called. \n");
105 }
106 /* FIXME: stub */
107 }
108
109 void
110 libresoc_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
111 uint32_t *pCount,
112 VkQueueFamilyProperties *pQueueFamilyProperties)
113 {
114 if (getenv("LIBRESOC_TRACE")) {
115 fprintf(stderr, "GetPhysicalDeviceQueueFamilyProperites called. \n");
116 }
117 /* FIXME: stub */
118 }
119
120 void
121 libresoc_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
122 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
123 {
124 if (getenv("LIBRESOC_TRACE")) {
125 fprintf(stderr, "GetPhysicalDEviceMemoryProperties called. \n");
126 }
127 /* FIXME: stub */
128 }
129
130 void
131 libresoc_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties *pFormatProperties) {
132
133 if (getenv("LIBRESOC_TRACE")) {
134 fprintf(stderr, "GetPhysicalDeviceFormatProperties called. \n");
135 }
136 /* FIXME: stub */
137 }
138
139 VkResult
140 libresoc_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* pImageFormatProperties)
141 {
142 if (getenv("LIBRESOC_TRACE")) {
143 fprintf(stderr, "GetPhysicalDEviceImageFormatProperties called. \n");
144 }
145
146 /* FIXME: stub */
147 return VK_SUCCESS;
148 }
149 void
150 libresoc_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling, uint32_t* pPropertyCount, VkSparseImageFormatProperties* pProperties)
151 {
152 if (getenv("LIBRESOC_TRACE")) {
153 fprintf(stderr, "GetPhysicalDeviceSparseImageFormatProperties called. \n");
154 }
155 /* FIXME: stub */
156 }
157 PFN_vkVoidFunction
158 libresoc_GetInstanceProcAddr(VkInstance _instance,
159 const char *pName)
160 {
161 if (getenv("LIBRESOC_TRACE")) {
162 fprintf(stderr, "GetInstanceProcAddr called for: %s \n", pName);
163 }
164 LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
165
166 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
167 * when we have to return valid function pointers, NULL, or it's left
168 * undefined. See the table for exact details.
169 */
170 if (pName == NULL)
171 return NULL;
172
173 #define LOOKUP_LIBRESOC_ENTRYPOINT(entrypoint) \
174 if (strcmp(pName, "vk" #entrypoint) == 0) \
175 return (PFN_vkVoidFunction)libresoc_##entrypoint
176
177 LOOKUP_LIBRESOC_ENTRYPOINT(EnumerateInstanceExtensionProperties);
178 LOOKUP_LIBRESOC_ENTRYPOINT(CreateInstance);
179 LOOKUP_LIBRESOC_ENTRYPOINT(DestroyInstance);
180 LOOKUP_LIBRESOC_ENTRYPOINT(EnumeratePhysicalDevices);
181 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceFeatures);
182 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceFormatProperties);
183 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceImageFormatProperties);
184 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceProperties);
185 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceQueueFamilyProperties);
186 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceMemoryProperties);
187 LOOKUP_LIBRESOC_ENTRYPOINT(GetDeviceProcAddr);
188 LOOKUP_LIBRESOC_ENTRYPOINT(CreateDevice);
189 LOOKUP_LIBRESOC_ENTRYPOINT(EnumerateDeviceExtensionProperties);
190 LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceSparseImageFormatProperties);
191
192
193 #undef LOOKUP_LIBRESOC_ENTRYPOINT
194
195 if (instance == NULL)
196 return NULL;
197
198 int idx = libresoc_get_instance_entrypoint_index(pName);
199 if (idx >= 0)
200 return instance->dispatch.entrypoints[idx];
201
202 idx = libresoc_get_physical_device_entrypoint_index(pName);
203 if (idx >= 0)
204 return instance->physicalDevice.dispatch.entrypoints[idx];
205
206 idx = libresoc_get_device_entrypoint_index(pName);
207 if (idx >= 0)
208 return instance->device_dispatch.entrypoints[idx];
209
210 return NULL;
211 }
212
213 /* With version 1+ of the loader interface the ICD should expose
214 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
215 */
216 PUBLIC
217 VKAPI_ATTR PFN_vkVoidFunction
218 VKAPI_CALL vk_icdGetInstanceProcAddr(VkInstance instance,
219 const char *pName);
220
221 PUBLIC
222 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
223 vk_icdGetInstanceProcAddr(VkInstance instance,
224 const char* pName)
225 {
226 if (getenv("LIBRESOC_TRACE")) {
227 fprintf(stderr, "vk_icdGetInstanceProcAddr called for: %s \n", pName);
228 }
229 return libresoc_GetInstanceProcAddr(instance, pName);
230 }
231
232 PFN_vkVoidFunction
233 libresoc_GetDeviceProcAddr(VkDevice _device,
234 const char *pName)
235 {
236 if (getenv("LIBRESOC_TRACE")) {
237 fprintf(stderr, "GetDeviceProcAddr called for: %s \n", pName);
238 }
239 LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
240
241 if (!device || !pName)
242 return NULL;
243
244 int idx = libresoc_get_device_entrypoint_index(pName);
245 if (idx < 0)
246 return NULL;
247
248 return device->dispatch.entrypoints[idx];
249 }
250
251 /* With version 4+ of the loader interface the ICD should expose
252 * vk_icdGetPhysicalDeviceProcAddr()
253 */
254 PUBLIC
255 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
256 vk_icdGetPhysicalDeviceProcAddr(VkInstance _instance,
257 const char* pName);
258
259 PFN_vkVoidFunction
260 vk_icdGetPhysicalDeviceProcAddr(VkInstance _instance,
261 const char* pName)
262 {
263 if (getenv("LIBRESOC_TRACE")) {
264 fprintf(stderr, "vk_icdGetPhysicalDeviceProcAddr called for: %s \n", pName);
265 }
266 LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
267
268 if (!pName || !instance)
269 return NULL;
270
271 int idx = libresoc_get_physical_device_entrypoint_index(pName);
272 if (idx < 0)
273 return NULL;
274
275 return instance->physicalDevice.dispatch.entrypoints[idx];
276 }
277
278 VkResult
279 libresoc_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
280 const char *pLayerName,
281 uint32_t *pPropertyCount,
282 VkExtensionProperties *pProperties)
283 {
284 if (getenv("LIBRESOC_TRACE")) {
285 fprintf(stderr, "EnumerateDeviceExtensionProperties called for layer: %s \n", pLayerName);
286 }
287 /* FIXME: stub */
288 return VK_SUCCESS;
289 }
290
291 VkResult
292 libresoc_CreateDevice(VkPhysicalDevice physicalDevice,
293 const VkDeviceCreateInfo *pCreateInfo,
294 const VkAllocationCallbacks *pAllocator,
295 VkDevice *pDevice)
296 {
297 if (getenv("LIBRESOC_TRACE")) {
298 fprintf(stderr, "CreateDevice called \n");
299 }
300 /* FIXME: stub */
301 return VK_SUCCESS;
302 }
303
304 void
305 libresoc_DestroyDevice(VkDevice _device,
306 const VkAllocationCallbacks *pAllocator)
307 {
308 if (getenv("LIBRESOC_TRACE")) {
309 fprintf(stderr, "DestroyDevice called. \n");
310 }
311 /* FIXME: stub */
312 }
313
314 void
315 libresoc_GetDeviceQueue(VkDevice _device,
316 uint32_t queueNodeIndex,
317 uint32_t queueIndex,
318 VkQueue *pQueue)
319 {
320 if (getenv("LIBRESOC_TRACE")) {
321 fprintf(stderr, "GetDeviceQueue called. \n");
322 }
323 /* FIXME: stub */
324 }