blob: b118f6df0d6023cda447ec2ea1afb540666269b5 [file] [log] [blame]
Mark Youngd66edd52017-03-10 17:31:18 -07001/*
2 * Copyright (c) 2015-2017 The Khronos Group Inc.
3 * Copyright (c) 2015-2017 Valve Corporation
4 * Copyright (c) 2015-2017 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Mark Young <marky@lunarg.com>
Lenny Komow185b35a2017-05-23 15:18:21 -060019 * Author: Lenny Komow <lenny@lunarg.com>
Mark Youngd66edd52017-03-10 17:31:18 -070020 */
21
22#define _GNU_SOURCE
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include "vk_loader_platform.h"
27#include "loader.h"
28#include "vk_loader_extensions.h"
29#include <vulkan/vk_icd.h>
30#include "wsi.h"
31#include "debug_report.h"
32
33// ---- Manually added trampoline/terminator functions
34
35// These functions, for whatever reason, require more complex changes than
36// can easily be automatically generated.
37VkResult setupLoaderTrampPhysDevGroups(VkInstance instance);
38VkResult setupLoaderTermPhysDevGroups(struct loader_instance *inst);
39
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -060040// ---- VK_KHR_device_group extension trampoline/terminators
Lenny Komow728ff062017-06-01 13:32:28 -060041
Lenny Komow4687c7f2017-05-30 13:04:46 -060042VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice physicalDevice,
43 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
44 VkSurfaceCapabilities2KHR *pSurfaceCapabilities) {
45 const VkLayerInstanceDispatchTable *disp;
46 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
47 disp = loader_get_instance_layer_dispatch(physicalDevice);
48 return disp->GetPhysicalDeviceSurfaceCapabilities2KHR(unwrapped_phys_dev, pSurfaceInfo, pSurfaceCapabilities);
49}
50
51VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2KHR(
52 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
53 VkSurfaceCapabilities2KHR *pSurfaceCapabilities) {
54 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
55 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
56
57 VkIcdSurface *icd_surface = (VkIcdSurface *)(pSurfaceInfo->surface);
58 uint8_t icd_index = phys_dev_term->icd_index;
59
60 if (icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2KHR != NULL) {
61 // Pass the call to the driver, possibly unwrapping the ICD surface
62 if (icd_surface->real_icd_surfaces != NULL && (void *)icd_surface->real_icd_surfaces[icd_index] != NULL) {
63 VkPhysicalDeviceSurfaceInfo2KHR info_copy = *pSurfaceInfo;
64 info_copy.surface = icd_surface->real_icd_surfaces[icd_index];
65 return icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev_term->phys_dev, &info_copy,
66 pSurfaceCapabilities);
67 } else {
68 return icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev_term->phys_dev, pSurfaceInfo,
69 pSurfaceCapabilities);
70 }
71 } else {
72 // Emulate the call
73 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
74 "vkGetPhysicalDeviceSurfaceCapabilities2KHR: Emulating call in ICD \"%s\" using "
75 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
76 icd_term->scanned_icd->lib_name);
77
78 if (pSurfaceInfo->pNext != NULL) {
79 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
80 "vkGetPhysicalDeviceSurfaceCapabilities2KHR: Emulation found unrecognized structure type in "
81 "pSurfaceInfo->pNext - this struct will be ignored");
82 }
83
84 // Write to the VkSurfaceCapabilities2KHR struct
85 VkSurfaceKHR surface = pSurfaceInfo->surface;
86 if (icd_surface->real_icd_surfaces != NULL && (void *)icd_surface->real_icd_surfaces[icd_index] != NULL) {
87 surface = icd_surface->real_icd_surfaces[icd_index];
88 }
89 VkResult res = icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, surface,
90 &pSurfaceCapabilities->surfaceCapabilities);
91
92 if (pSurfaceCapabilities->pNext != NULL) {
93 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
94 "vkGetPhysicalDeviceSurfaceCapabilities2KHR: Emulation found unrecognized structure type in "
95 "pSurfaceCapabilities->pNext - this struct will be ignored");
96 }
97 return res;
98 }
99}
100
Lenny Komow82e15e02017-10-02 15:08:53 -0600101// ---- VK_NV_external_memory_capabilities extension trampoline/terminators
102
103VKAPI_ATTR VkResult VKAPI_CALL
104GetPhysicalDeviceExternalImageFormatPropertiesNV(
105 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
106 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
107 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
108 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
109 const VkLayerInstanceDispatchTable *disp;
110 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
111 disp = loader_get_instance_layer_dispatch(physicalDevice);
112
113 return disp->GetPhysicalDeviceExternalImageFormatPropertiesNV(
114 unwrapped_phys_dev, format, type, tiling, usage, flags,
115 externalHandleType, pExternalImageFormatProperties);
116}
117
118VKAPI_ATTR VkResult VKAPI_CALL
119terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV(
120 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
121 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
122 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
123 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
124 struct loader_physical_device_term *phys_dev_term =
125 (struct loader_physical_device_term *)physicalDevice;
126 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
127
128 if (!icd_term->dispatch.GetPhysicalDeviceExternalImageFormatPropertiesNV) {
129 if (externalHandleType) {
130 return VK_ERROR_FORMAT_NOT_SUPPORTED;
131 }
132
133 if (!icd_term->dispatch.GetPhysicalDeviceImageFormatProperties) {
134 return VK_ERROR_INITIALIZATION_FAILED;
135 }
136
137 pExternalImageFormatProperties->externalMemoryFeatures = 0;
138 pExternalImageFormatProperties->exportFromImportedHandleTypes = 0;
139 pExternalImageFormatProperties->compatibleHandleTypes = 0;
140
141 return icd_term->dispatch.GetPhysicalDeviceImageFormatProperties(
142 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
143 &pExternalImageFormatProperties->imageFormatProperties);
144 }
145
146 return icd_term->dispatch.GetPhysicalDeviceExternalImageFormatPropertiesNV(
147 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
148 externalHandleType, pExternalImageFormatProperties);
149}
150
Lenny Komow4687c7f2017-05-30 13:04:46 -0600151VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
152 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
153 uint32_t *pSurfaceFormatCount,
154 VkSurfaceFormat2KHR *pSurfaceFormats) {
155 const VkLayerInstanceDispatchTable *disp;
156 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
157 disp = loader_get_instance_layer_dispatch(physicalDevice);
158 return disp->GetPhysicalDeviceSurfaceFormats2KHR(unwrapped_phys_dev, pSurfaceInfo, pSurfaceFormatCount, pSurfaceFormats);
159}
160
161VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
162 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
163 uint32_t *pSurfaceFormatCount,
164 VkSurfaceFormat2KHR *pSurfaceFormats) {
165 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
166 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
167
168 VkIcdSurface *icd_surface = (VkIcdSurface *)(pSurfaceInfo->surface);
169 uint8_t icd_index = phys_dev_term->icd_index;
170
171 if (icd_term->dispatch.GetPhysicalDeviceSurfaceFormats2KHR != NULL) {
172 // Pass the call to the driver, possibly unwrapping the ICD surface
173 if (icd_surface->real_icd_surfaces != NULL && (void *)icd_surface->real_icd_surfaces[icd_index] != NULL) {
174 VkPhysicalDeviceSurfaceInfo2KHR info_copy = *pSurfaceInfo;
175 info_copy.surface = icd_surface->real_icd_surfaces[icd_index];
176 return icd_term->dispatch.GetPhysicalDeviceSurfaceFormats2KHR(phys_dev_term->phys_dev, &info_copy, pSurfaceFormatCount,
177 pSurfaceFormats);
178 } else {
179 return icd_term->dispatch.GetPhysicalDeviceSurfaceFormats2KHR(phys_dev_term->phys_dev, pSurfaceInfo,
180 pSurfaceFormatCount, pSurfaceFormats);
181 }
182 } else {
183 // Emulate the call
184 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
185 "vkGetPhysicalDeviceSurfaceFormats2KHR: Emulating call in ICD \"%s\" using vkGetPhysicalDeviceSurfaceFormatsKHR",
186 icd_term->scanned_icd->lib_name);
187
188 if (pSurfaceInfo->pNext != NULL) {
189 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
190 "vkGetPhysicalDeviceSurfaceFormats2KHR: Emulation found unrecognized structure type in pSurfaceInfo->pNext "
191 "- this struct will be ignored");
192 }
193
194 VkSurfaceKHR surface = pSurfaceInfo->surface;
195 if (icd_surface->real_icd_surfaces != NULL && (void *)icd_surface->real_icd_surfaces[icd_index] != NULL) {
196 surface = icd_surface->real_icd_surfaces[icd_index];
197 }
198
199 if (*pSurfaceFormatCount == 0 || pSurfaceFormats == NULL) {
200 // Write to pSurfaceFormatCount
201 return icd_term->dispatch.GetPhysicalDeviceSurfaceFormatsKHR(phys_dev_term->phys_dev, surface, pSurfaceFormatCount,
202 NULL);
203 } else {
204 // Allocate a temporary array for the output of the old function
205 VkSurfaceFormatKHR *formats = loader_stack_alloc(*pSurfaceFormatCount * sizeof(VkSurfaceFormatKHR));
206 if (formats == NULL) {
207 return VK_ERROR_OUT_OF_HOST_MEMORY;
208 }
209
210 VkResult res = icd_term->dispatch.GetPhysicalDeviceSurfaceFormatsKHR(phys_dev_term->phys_dev, surface,
211 pSurfaceFormatCount, formats);
212 for (uint32_t i = 0; i < *pSurfaceFormatCount; ++i) {
213 pSurfaceFormats[i].surfaceFormat = formats[i];
214 if (pSurfaceFormats[i].pNext != NULL) {
215 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
216 "vkGetPhysicalDeviceSurfaceFormats2KHR: Emulation found unrecognized structure type in "
217 "pSurfaceFormats[%d].pNext - this struct will be ignored",
218 i);
219 }
220 }
221 return res;
222 }
223 }
224}
225
Lenny Komow728ff062017-06-01 13:32:28 -0600226// ---- VK_EXT_display_surface_counter extension trampoline/terminators
227
228VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
229 VkSurfaceCapabilities2EXT *pSurfaceCapabilities) {
230 const VkLayerInstanceDispatchTable *disp;
231 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
232 disp = loader_get_instance_layer_dispatch(physicalDevice);
233 return disp->GetPhysicalDeviceSurfaceCapabilities2EXT(unwrapped_phys_dev, surface, pSurfaceCapabilities);
234}
235
236VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2EXT(
237 VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, VkSurfaceCapabilities2EXT *pSurfaceCapabilities) {
238 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
239 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
240
241 VkIcdSurface *icd_surface = (VkIcdSurface *)(surface);
242 uint8_t icd_index = phys_dev_term->icd_index;
243
244 // Unwrap the surface if needed
245 VkSurfaceKHR unwrapped_surface = surface;
246 if (icd_surface->real_icd_surfaces != NULL && (void *)icd_surface->real_icd_surfaces[icd_index] != NULL) {
247 unwrapped_surface = icd_surface->real_icd_surfaces[icd_index];
248 }
249
250 if (icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2EXT != NULL) {
251 // Pass the call to the driver
252 return icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2EXT(phys_dev_term->phys_dev, unwrapped_surface,
253 pSurfaceCapabilities);
254 } else {
255 // Emulate the call
256 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
257 "vkGetPhysicalDeviceSurfaceCapabilities2EXT: Emulating call in ICD \"%s\" using "
258 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
259 icd_term->scanned_icd->lib_name);
260
261 VkSurfaceCapabilitiesKHR surface_caps;
262 VkResult res =
263 icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, unwrapped_surface, &surface_caps);
264 pSurfaceCapabilities->minImageCount = surface_caps.minImageCount;
265 pSurfaceCapabilities->maxImageCount = surface_caps.maxImageCount;
266 pSurfaceCapabilities->currentExtent = surface_caps.currentExtent;
267 pSurfaceCapabilities->minImageExtent = surface_caps.minImageExtent;
268 pSurfaceCapabilities->maxImageExtent = surface_caps.maxImageExtent;
269 pSurfaceCapabilities->maxImageArrayLayers = surface_caps.maxImageArrayLayers;
270 pSurfaceCapabilities->supportedTransforms = surface_caps.supportedTransforms;
271 pSurfaceCapabilities->currentTransform = surface_caps.currentTransform;
272 pSurfaceCapabilities->supportedCompositeAlpha = surface_caps.supportedCompositeAlpha;
273 pSurfaceCapabilities->supportedUsageFlags = surface_caps.supportedUsageFlags;
274 pSurfaceCapabilities->supportedSurfaceCounters = 0;
275
276 if (pSurfaceCapabilities->pNext != NULL) {
277 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
278 "vkGetPhysicalDeviceSurfaceCapabilities2EXT: Emulation found unrecognized structure type in "
279 "pSurfaceCapabilities->pNext - this struct will be ignored");
280 }
281
282 return res;
283 }
284}
285
286// ---- VK_EXT_direct_mode_display extension trampoline/terminators
287
288VKAPI_ATTR VkResult VKAPI_CALL ReleaseDisplayEXT(VkPhysicalDevice physicalDevice, VkDisplayKHR display) {
289 const VkLayerInstanceDispatchTable *disp;
290 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
291 disp = loader_get_instance_layer_dispatch(physicalDevice);
292 return disp->ReleaseDisplayEXT(unwrapped_phys_dev, display);
293}
294
295VKAPI_ATTR VkResult VKAPI_CALL terminator_ReleaseDisplayEXT(VkPhysicalDevice physicalDevice, VkDisplayKHR display) {
296 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
297 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
298
299 if (icd_term->dispatch.ReleaseDisplayEXT == NULL) {
300 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
301 "ICD \"%s\" associated with VkPhysicalDevice does not support vkReleaseDisplayEXT - Consequently, the call is "
302 "invalid because it should not be possible to acquire a display on this device",
303 icd_term->scanned_icd->lib_name);
304 }
305 return icd_term->dispatch.ReleaseDisplayEXT(phys_dev_term->phys_dev, display);
306}
307
308// ---- VK_EXT_acquire_xlib_display extension trampoline/terminators
309
310#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
311VKAPI_ATTR VkResult VKAPI_CALL AcquireXlibDisplayEXT(VkPhysicalDevice physicalDevice, Display *dpy, VkDisplayKHR display) {
312 const VkLayerInstanceDispatchTable *disp;
313 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
314 disp = loader_get_instance_layer_dispatch(physicalDevice);
315 return disp->AcquireXlibDisplayEXT(unwrapped_phys_dev, dpy, display);
316}
317
318VKAPI_ATTR VkResult VKAPI_CALL terminator_AcquireXlibDisplayEXT(VkPhysicalDevice physicalDevice, Display *dpy,
319 VkDisplayKHR display) {
320 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
321 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
322
323 if (icd_term->dispatch.AcquireXlibDisplayEXT != NULL) {
324 // Pass the call to the driver
325 return icd_term->dispatch.AcquireXlibDisplayEXT(phys_dev_term->phys_dev, dpy, display);
326 } else {
327 // Emulate the call
328 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
329 "vkAcquireXLibDisplayEXT: Emulating call in ICD \"%s\" by returning error", icd_term->scanned_icd->lib_name);
330
331 // Fail for the unsupported command
332 return VK_ERROR_INITIALIZATION_FAILED;
333 }
334}
335
336VKAPI_ATTR VkResult VKAPI_CALL GetRandROutputDisplayEXT(VkPhysicalDevice physicalDevice, Display *dpy, RROutput rrOutput,
337 VkDisplayKHR *pDisplay) {
338 const VkLayerInstanceDispatchTable *disp;
339 VkPhysicalDevice unwrapped_phys_dev = loader_unwrap_physical_device(physicalDevice);
340 disp = loader_get_instance_layer_dispatch(physicalDevice);
341 return disp->GetRandROutputDisplayEXT(unwrapped_phys_dev, dpy, rrOutput, pDisplay);
342}
343
344VKAPI_ATTR VkResult VKAPI_CALL terminator_GetRandROutputDisplayEXT(VkPhysicalDevice physicalDevice, Display *dpy, RROutput rrOutput,
345 VkDisplayKHR *pDisplay) {
346 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
347 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
348
349 if (icd_term->dispatch.GetRandROutputDisplayEXT != NULL) {
350 // Pass the call to the driver
351 return icd_term->dispatch.GetRandROutputDisplayEXT(phys_dev_term->phys_dev, dpy, rrOutput, pDisplay);
352 } else {
353 // Emulate the call
354 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
355 "vkGetRandROutputDisplayEXT: Emulating call in ICD \"%s\" by returning null display",
356 icd_term->scanned_icd->lib_name);
357
358 // Return a null handle to indicate this can't be done
359 *pDisplay = VK_NULL_HANDLE;
360 return VK_SUCCESS;
361 }
362}
363
364#endif // VK_USE_PLATFORM_XLIB_XRANDR_EXT
365
Lenny Komow4687c7f2017-05-30 13:04:46 -0600366// ---- Helper functions
367
Mark Youngd66edd52017-03-10 17:31:18 -0700368VkResult setupLoaderTrampPhysDevGroups(VkInstance instance) {
369 VkResult res = VK_SUCCESS;
370 struct loader_instance *inst;
371 uint32_t total_count = 0;
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600372 VkPhysicalDeviceGroupPropertiesKHR **new_phys_dev_groups = NULL;
373 VkPhysicalDeviceGroupPropertiesKHR *local_phys_dev_groups = NULL;
Mark Youngd66edd52017-03-10 17:31:18 -0700374
375 inst = loader_get_instance(instance);
376 if (NULL == inst) {
377 res = VK_ERROR_INITIALIZATION_FAILED;
378 goto out;
379 }
380
381 // Setup the trampoline loader physical devices. This will actually
382 // call down and setup the terminator loader physical devices during the
383 // process.
384 VkResult setup_res = setupLoaderTrampPhysDevs(instance);
385 if (setup_res != VK_SUCCESS && setup_res != VK_INCOMPLETE) {
386 res = setup_res;
387 goto out;
388 }
389
390 // Query how many physical device groups there
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600391 res = inst->disp->layer_inst_disp.EnumeratePhysicalDeviceGroupsKHR(instance, &total_count, NULL);
Mark Youngd66edd52017-03-10 17:31:18 -0700392 if (res != VK_SUCCESS) {
393 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
394 "setupLoaderTrampPhysDevGroups: Failed during dispatch call of "
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600395 "\'EnumeratePhysicalDeviceGroupsKHR\' to lower layers or "
Mark Youngd66edd52017-03-10 17:31:18 -0700396 "loader to get count.");
397 goto out;
398 }
399
400 // Create an array for the new physical device groups, which will be stored
401 // in the instance for the trampoline code.
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600402 new_phys_dev_groups = (VkPhysicalDeviceGroupPropertiesKHR **)loader_instance_heap_alloc(
403 inst, total_count * sizeof(VkPhysicalDeviceGroupPropertiesKHR *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngd66edd52017-03-10 17:31:18 -0700404 if (NULL == new_phys_dev_groups) {
405 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
406 "setupLoaderTrampPhysDevGroups: Failed to allocate new physical device"
407 " group array of size %d",
408 total_count);
409 res = VK_ERROR_OUT_OF_HOST_MEMORY;
410 goto out;
411 }
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600412 memset(new_phys_dev_groups, 0, total_count * sizeof(VkPhysicalDeviceGroupPropertiesKHR *));
Mark Youngd66edd52017-03-10 17:31:18 -0700413
414 // Create a temporary array (on the stack) to keep track of the
415 // returned VkPhysicalDevice values.
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600416 local_phys_dev_groups = loader_stack_alloc(sizeof(VkPhysicalDeviceGroupPropertiesKHR) * total_count);
Mark Youngd66edd52017-03-10 17:31:18 -0700417 if (NULL == local_phys_dev_groups) {
418 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
419 "setupLoaderTrampPhysDevGroups: Failed to allocate local "
420 "physical device group array of size %d",
421 total_count);
422 res = VK_ERROR_OUT_OF_HOST_MEMORY;
423 goto out;
424 }
425 // Initialize the memory to something valid
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600426 memset(local_phys_dev_groups, 0, sizeof(VkPhysicalDeviceGroupPropertiesKHR) * total_count);
Mark Youngd66edd52017-03-10 17:31:18 -0700427 for (uint32_t group = 0; group < total_count; group++) {
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600428 local_phys_dev_groups[group].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES_KHR;
Mark Youngd66edd52017-03-10 17:31:18 -0700429 local_phys_dev_groups[group].pNext = NULL;
430 local_phys_dev_groups[group].subsetAllocation = false;
431 }
432
433 // Call down and get the content
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600434 res = inst->disp->layer_inst_disp.EnumeratePhysicalDeviceGroupsKHR(instance, &total_count, local_phys_dev_groups);
Mark Youngd66edd52017-03-10 17:31:18 -0700435 if (VK_SUCCESS != res) {
436 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
437 "setupLoaderTrampPhysDevGroups: Failed during dispatch call of "
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600438 "\'EnumeratePhysicalDeviceGroupsKHR\' to lower layers or "
Mark Youngd66edd52017-03-10 17:31:18 -0700439 "loader to get content.");
440 goto out;
441 }
442
443 // Replace all the physical device IDs with the proper loader values
444 for (uint32_t group = 0; group < total_count; group++) {
445 for (uint32_t group_gpu = 0; group_gpu < local_phys_dev_groups[group].physicalDeviceCount; group_gpu++) {
446 bool found = false;
447 for (uint32_t tramp_gpu = 0; tramp_gpu < inst->phys_dev_count_tramp; tramp_gpu++) {
448 if (local_phys_dev_groups[group].physicalDevices[group_gpu] == inst->phys_devs_tramp[tramp_gpu]->phys_dev) {
449 local_phys_dev_groups[group].physicalDevices[group_gpu] = (VkPhysicalDevice)inst->phys_devs_tramp[tramp_gpu];
450 found = true;
451 break;
452 }
453 }
454 if (!found) {
455 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
456 "setupLoaderTrampPhysDevGroups: Failed to find GPU %d in group %d"
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600457 " returned by \'EnumeratePhysicalDeviceGroupsKHR\' in list returned"
Mark Youngd66edd52017-03-10 17:31:18 -0700458 " by \'EnumeratePhysicalDevices\'", group_gpu, group);
459 res = VK_ERROR_INITIALIZATION_FAILED;
460 goto out;
461 }
462 }
463 }
464
465 // Copy or create everything to fill the new array of physical device groups
466 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
467 // Check if this physical device group with the same contents is already in the old buffer
468 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_group_count_tramp; old_idx++) {
469 if (local_phys_dev_groups[new_idx].physicalDeviceCount == inst->phys_dev_groups_tramp[old_idx]->physicalDeviceCount) {
470 bool found_all_gpus = true;
471 for (uint32_t old_gpu = 0; old_gpu < inst->phys_dev_groups_tramp[old_idx]->physicalDeviceCount; old_gpu++) {
472 bool found_gpu = false;
473 for (uint32_t new_gpu = 0; new_gpu < local_phys_dev_groups[new_idx].physicalDeviceCount; new_gpu++) {
474 if (local_phys_dev_groups[new_idx].physicalDevices[new_gpu] == inst->phys_dev_groups_tramp[old_idx]->physicalDevices[old_gpu]) {
475 found_gpu = true;
476 break;
477 }
478 }
479
480 if (!found_gpu) {
481 found_all_gpus = false;
482 break;
483 }
484 }
485 if (!found_all_gpus) {
486 continue;
487 } else {
488 new_phys_dev_groups[new_idx] = inst->phys_dev_groups_tramp[old_idx];
489 break;
490 }
491 }
492 }
493
494 // If this physical device group isn't in the old buffer, create it
495 if (NULL == new_phys_dev_groups[new_idx]) {
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600496 new_phys_dev_groups[new_idx] = (VkPhysicalDeviceGroupPropertiesKHR *)loader_instance_heap_alloc(
497 inst, sizeof(VkPhysicalDeviceGroupPropertiesKHR), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngd66edd52017-03-10 17:31:18 -0700498 if (NULL == new_phys_dev_groups[new_idx]) {
499 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
500 "setupLoaderTrampPhysDevGroups: Failed to allocate "
501 "physical device group trampoline object %d",
502 new_idx);
503 total_count = new_idx;
504 res = VK_ERROR_OUT_OF_HOST_MEMORY;
505 goto out;
506 }
507 memcpy(new_phys_dev_groups[new_idx], &local_phys_dev_groups[new_idx],
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600508 sizeof(VkPhysicalDeviceGroupPropertiesKHR));
Mark Youngd66edd52017-03-10 17:31:18 -0700509 }
510 }
511
512out:
513
514 if (VK_SUCCESS != res) {
515 if (NULL != new_phys_dev_groups) {
516 for (uint32_t i = 0; i < total_count; i++) {
517 loader_instance_heap_free(inst, new_phys_dev_groups[i]);
518 }
519 loader_instance_heap_free(inst, new_phys_dev_groups);
520 }
521 total_count = 0;
522 } else {
523 // Free everything that didn't carry over to the new array of
524 // physical device groups
525 if (NULL != inst->phys_dev_groups_tramp) {
526 for (uint32_t i = 0; i < inst->phys_dev_group_count_tramp; i++) {
527 bool found = false;
528 for (uint32_t j = 0; j < total_count; j++) {
529 if (inst->phys_dev_groups_tramp[i] == new_phys_dev_groups[j]) {
530 found = true;
531 break;
532 }
533 }
534 if (!found) {
535 loader_instance_heap_free(inst, inst->phys_dev_groups_tramp[i]);
536 }
537 }
538 loader_instance_heap_free(inst, inst->phys_dev_groups_tramp);
539 }
540
541 // Swap in the new physical device group list
542 inst->phys_dev_group_count_tramp = total_count;
543 inst->phys_dev_groups_tramp = new_phys_dev_groups;
544 }
545
546 return res;
547}
548
549VkResult setupLoaderTermPhysDevGroups(struct loader_instance *inst) {
550 VkResult res = VK_SUCCESS;
551 struct loader_icd_term *icd_term;
552 uint32_t total_count = 0;
553 uint32_t cur_icd_group_count = 0;
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600554 VkPhysicalDeviceGroupPropertiesKHR **new_phys_dev_groups = NULL;
555 VkPhysicalDeviceGroupPropertiesKHR *local_phys_dev_groups = NULL;
Mark Youngd66edd52017-03-10 17:31:18 -0700556
557 if (0 == inst->phys_dev_count_term) {
558 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
559 "setupLoaderTermPhysDevGroups: Loader failed to setup physical "
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600560 "device terminator info before calling \'EnumeratePhysicalDeviceGroupsKHR\'.");
Mark Youngd66edd52017-03-10 17:31:18 -0700561 assert(false);
562 res = VK_ERROR_INITIALIZATION_FAILED;
563 goto out;
564 }
565
566 // For each ICD, query the number of physical device groups, and then get an
567 // internal value for those physical devices.
568 icd_term = inst->icd_terms;
569 for (uint32_t icd_idx = 0; NULL != icd_term; icd_term = icd_term->next, icd_idx++) {
570 cur_icd_group_count = 0;
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600571 if (NULL == icd_term->dispatch.EnumeratePhysicalDeviceGroupsKHR) {
Mark Youngd66edd52017-03-10 17:31:18 -0700572 // Treat each ICD's GPU as it's own group if the extension isn't supported
573 res = icd_term->dispatch.EnumeratePhysicalDevices(icd_term->instance, &cur_icd_group_count, NULL);
574 if (res != VK_SUCCESS) {
575 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
576 "setupLoaderTermPhysDevGroups: Failed during dispatch call of "
577 "\'EnumeratePhysicalDevices\' to ICD %d to get plain phys dev count.",
578 icd_idx);
579 goto out;
580 }
581 } else {
582 // Query the actual group info
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600583 res = icd_term->dispatch.EnumeratePhysicalDeviceGroupsKHR(icd_term->instance, &cur_icd_group_count, NULL);
Mark Youngd66edd52017-03-10 17:31:18 -0700584 if (res != VK_SUCCESS) {
585 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
586 "setupLoaderTermPhysDevGroups: Failed during dispatch call of "
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600587 "\'EnumeratePhysicalDeviceGroupsKHR\' to ICD %d to get count.",
Mark Youngd66edd52017-03-10 17:31:18 -0700588 icd_idx);
589 goto out;
590 }
591 }
592 total_count += cur_icd_group_count;
593 }
594
595 // Create an array for the new physical device groups, which will be stored
596 // in the instance for the Terminator code.
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600597 new_phys_dev_groups = (VkPhysicalDeviceGroupPropertiesKHR **)loader_instance_heap_alloc(
598 inst, total_count * sizeof(VkPhysicalDeviceGroupPropertiesKHR *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngd66edd52017-03-10 17:31:18 -0700599 if (NULL == new_phys_dev_groups) {
600 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
601 "setupLoaderTermPhysDevGroups: Failed to allocate new physical device"
602 " group array of size %d",
603 total_count);
604 res = VK_ERROR_OUT_OF_HOST_MEMORY;
605 goto out;
606 }
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600607 memset(new_phys_dev_groups, 0, total_count * sizeof(VkPhysicalDeviceGroupPropertiesKHR *));
Mark Youngd66edd52017-03-10 17:31:18 -0700608
609 // Create a temporary array (on the stack) to keep track of the
610 // returned VkPhysicalDevice values.
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600611 local_phys_dev_groups = loader_stack_alloc(sizeof(VkPhysicalDeviceGroupPropertiesKHR) * total_count);
Mark Youngd66edd52017-03-10 17:31:18 -0700612 if (NULL == local_phys_dev_groups) {
613 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
614 "setupLoaderTermPhysDevGroups: Failed to allocate local "
615 "physical device group array of size %d",
616 total_count);
617 res = VK_ERROR_OUT_OF_HOST_MEMORY;
618 goto out;
619 }
620 // Initialize the memory to something valid
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600621 memset(local_phys_dev_groups, 0, sizeof(VkPhysicalDeviceGroupPropertiesKHR) * total_count);
Mark Youngd66edd52017-03-10 17:31:18 -0700622 for (uint32_t group = 0; group < total_count; group++) {
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600623 local_phys_dev_groups[group].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES_KHR;
Mark Youngd66edd52017-03-10 17:31:18 -0700624 local_phys_dev_groups[group].pNext = NULL;
625 local_phys_dev_groups[group].subsetAllocation = false;
626 }
627
628 cur_icd_group_count = 0;
629 icd_term = inst->icd_terms;
630 for (uint32_t icd_idx = 0; NULL != icd_term; icd_term = icd_term->next, icd_idx++) {
631 uint32_t count_this_time = total_count - cur_icd_group_count;
632
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600633 if (NULL == icd_term->dispatch.EnumeratePhysicalDeviceGroupsKHR) {
Mark Youngd66edd52017-03-10 17:31:18 -0700634 VkPhysicalDevice* phys_dev_array = loader_stack_alloc(sizeof(VkPhysicalDevice) * count_this_time);
635 if (NULL == phys_dev_array) {
636 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
637 "setupLoaderTermPhysDevGroups: Failed to allocate local "
638 "physical device array of size %d",
639 count_this_time);
640 res = VK_ERROR_OUT_OF_HOST_MEMORY;
641 goto out;
642 }
643
644 res = icd_term->dispatch.EnumeratePhysicalDevices(icd_term->instance, &count_this_time, phys_dev_array);
645 if (res != VK_SUCCESS) {
646 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
647 "setupLoaderTermPhysDevGroups: Failed during dispatch call of "
648 "\'EnumeratePhysicalDevices\' to ICD %d to get plain phys dev count.",
649 icd_idx);
650 goto out;
651 }
652
653 // Add each GPU as it's own group
654 for (uint32_t indiv_gpu = 0; indiv_gpu < count_this_time; indiv_gpu++) {
655 local_phys_dev_groups[indiv_gpu + cur_icd_group_count].physicalDeviceCount = 1;
656 local_phys_dev_groups[indiv_gpu + cur_icd_group_count].physicalDevices[0] = phys_dev_array[indiv_gpu];
657 }
658
659 } else {
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600660 res = icd_term->dispatch.EnumeratePhysicalDeviceGroupsKHR(icd_term->instance, &count_this_time, &local_phys_dev_groups[cur_icd_group_count]);
Mark Youngd66edd52017-03-10 17:31:18 -0700661 if (VK_SUCCESS != res) {
662 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
663 "setupLoaderTermPhysDevGroups: Failed during dispatch call of "
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600664 "\'EnumeratePhysicalDeviceGroupsKHR\' to ICD %d to get content.",
Mark Youngd66edd52017-03-10 17:31:18 -0700665 icd_idx);
666 goto out;
667 }
668 }
669
670 cur_icd_group_count += count_this_time;
671 }
672
673 // Replace all the physical device IDs with the proper loader values
674 for (uint32_t group = 0; group < total_count; group++) {
675 for (uint32_t group_gpu = 0; group_gpu < local_phys_dev_groups[group].physicalDeviceCount; group_gpu++) {
676 bool found = false;
677 for (uint32_t term_gpu = 0; term_gpu < inst->phys_dev_count_term; term_gpu++) {
678 if (local_phys_dev_groups[group].physicalDevices[group_gpu] == inst->phys_devs_term[term_gpu]->phys_dev) {
679 local_phys_dev_groups[group].physicalDevices[group_gpu] = (VkPhysicalDevice)inst->phys_devs_term[term_gpu];
680 found = true;
681 break;
682 }
683 }
684 if (!found) {
685 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
686 "setupLoaderTermPhysDevGroups: Failed to find GPU %d in group %d"
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600687 " returned by \'EnumeratePhysicalDeviceGroupsKHR\' in list returned"
Mark Youngd66edd52017-03-10 17:31:18 -0700688 " by \'EnumeratePhysicalDevices\'", group_gpu, group);
689 res = VK_ERROR_INITIALIZATION_FAILED;
690 goto out;
691 }
692 }
693 }
694
695 // Copy or create everything to fill the new array of physical device groups
696 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
697 // Check if this physical device group with the same contents is already in the old buffer
698 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_group_count_term; old_idx++) {
699 if (local_phys_dev_groups[new_idx].physicalDeviceCount == inst->phys_dev_groups_term[old_idx]->physicalDeviceCount) {
700 bool found_all_gpus = true;
701 for (uint32_t old_gpu = 0; old_gpu < inst->phys_dev_groups_term[old_idx]->physicalDeviceCount; old_gpu++) {
702 bool found_gpu = false;
703 for (uint32_t new_gpu = 0; new_gpu < local_phys_dev_groups[new_idx].physicalDeviceCount; new_gpu++) {
704 if (local_phys_dev_groups[new_idx].physicalDevices[new_gpu] == inst->phys_dev_groups_term[old_idx]->physicalDevices[old_gpu]) {
705 found_gpu = true;
706 break;
707 }
708 }
709
710 if (!found_gpu) {
711 found_all_gpus = false;
712 break;
713 }
714 }
715 if (!found_all_gpus) {
716 continue;
717 } else {
718 new_phys_dev_groups[new_idx] = inst->phys_dev_groups_term[old_idx];
719 break;
720 }
721 }
722 }
723
724 // If this physical device group isn't in the old buffer, create it
725 if (NULL == new_phys_dev_groups[new_idx]) {
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600726 new_phys_dev_groups[new_idx] = (VkPhysicalDeviceGroupPropertiesKHR *)loader_instance_heap_alloc(
727 inst, sizeof(VkPhysicalDeviceGroupPropertiesKHR), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngd66edd52017-03-10 17:31:18 -0700728 if (NULL == new_phys_dev_groups[new_idx]) {
729 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
730 "setupLoaderTermPhysDevGroups: Failed to allocate "
731 "physical device group Terminator object %d",
732 new_idx);
733 total_count = new_idx;
734 res = VK_ERROR_OUT_OF_HOST_MEMORY;
735 goto out;
736 }
737 memcpy(new_phys_dev_groups[new_idx], &local_phys_dev_groups[new_idx],
Mark Lobodzinski7c7c8d22017-09-18 16:28:27 -0600738 sizeof(VkPhysicalDeviceGroupPropertiesKHR));
Mark Youngd66edd52017-03-10 17:31:18 -0700739 }
740 }
741
742out:
743
744 if (VK_SUCCESS != res) {
745 if (NULL != new_phys_dev_groups) {
746 for (uint32_t i = 0; i < total_count; i++) {
747 loader_instance_heap_free(inst, new_phys_dev_groups[i]);
748 }
749 loader_instance_heap_free(inst, new_phys_dev_groups);
750 }
751 total_count = 0;
752 } else {
753 // Free everything that didn't carry over to the new array of
754 // physical device groups
755 if (NULL != inst->phys_dev_groups_term) {
756 for (uint32_t i = 0; i < inst->phys_dev_group_count_term; i++) {
757 bool found = false;
758 for (uint32_t j = 0; j < total_count; j++) {
759 if (inst->phys_dev_groups_term[i] == new_phys_dev_groups[j]) {
760 found = true;
761 break;
762 }
763 }
764 if (!found) {
765 loader_instance_heap_free(inst, inst->phys_dev_groups_term[i]);
766 }
767 }
768 loader_instance_heap_free(inst, inst->phys_dev_groups_term);
769 }
770
771 // Swap in the new physical device group list
772 inst->phys_dev_group_count_term = total_count;
773 inst->phys_dev_groups_term = new_phys_dev_groups;
774 }
775
776 return res;
Mark Youngabc2d6e2017-07-07 07:59:56 -0600777}