Skip to content

Commit 322d634

Browse files
aqnuepcharles-lunarg
authored andcommitted
loader: Lazily allocate ICD surface objects
1 parent 224f1c3 commit 322d634

File tree

7 files changed

+484
-347
lines changed

7 files changed

+484
-347
lines changed

loader/allocation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include "loader_common.h"
3232

33+
// The loader always aligns memory allocations to the largest unit size which is the size of a uint64_t
34+
#define loader_aligned_size(x) ((((x) + sizeof(uint64_t) - 1) / sizeof(uint64_t)) * sizeof(uint64_t))
35+
3336
void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope);
3437
void *loader_instance_heap_calloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope);
3538
void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory);

loader/extension_manual.c

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,14 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2E
105105
struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
106106
struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
107107

108-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(surface);
109-
110-
// Unwrap the surface if needed
111-
VkSurfaceKHR unwrapped_surface = surface;
112-
if (NULL != phys_dev_term->this_icd_term->surface_list.list &&
113-
phys_dev_term->this_icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
114-
phys_dev_term->this_icd_term->surface_list.list[icd_surface->surface_index]) {
115-
unwrapped_surface = phys_dev_term->this_icd_term->surface_list.list[icd_surface->surface_index];
108+
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface);
109+
if (res != VK_SUCCESS) {
110+
return res;
116111
}
117112

118113
if (NULL != icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2EXT) {
119114
// Pass the call to the driver
120-
return icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2EXT(phys_dev_term->phys_dev, unwrapped_surface,
121-
pSurfaceCapabilities);
115+
return icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilities2EXT(phys_dev_term->phys_dev, surface, pSurfaceCapabilities);
122116
} else {
123117
// Emulate the call
124118
loader_log(icd_term->this_instance, VULKAN_LOADER_INFO_BIT, 0,
@@ -127,8 +121,7 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2E
127121
icd_term->scanned_icd->lib_name);
128122

129123
VkSurfaceCapabilitiesKHR surface_caps;
130-
VkResult res =
131-
icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, unwrapped_surface, &surface_caps);
124+
res = icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, surface, &surface_caps);
132125
pSurfaceCapabilities->minImageCount = surface_caps.minImageCount;
133126
pSurfaceCapabilities->maxImageCount = surface_caps.maxImageCount;
134127
pSurfaceCapabilities->currentExtent = surface_caps.currentExtent;
@@ -274,21 +267,17 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfacePresentModes2E
274267
"ICD associated with VkPhysicalDevice does not support GetPhysicalDeviceSurfacePresentModes2EXT");
275268
abort();
276269
}
270+
271+
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
277272
if (VK_NULL_HANDLE != pSurfaceInfo->surface) {
278-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(pSurfaceInfo->surface);
279-
if (NULL != icd_surface && NULL != icd_term->surface_list.list &&
280-
icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
281-
icd_term->surface_list.list[icd_surface->surface_index]) {
282-
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy;
283-
surface_info_copy.sType = pSurfaceInfo->sType;
284-
surface_info_copy.pNext = pSurfaceInfo->pNext;
285-
surface_info_copy.surface = icd_term->surface_list.list[icd_surface->surface_index];
286-
return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, &surface_info_copy,
287-
pPresentModeCount, pPresentModes);
273+
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface_info_copy.surface);
274+
if (res != VK_SUCCESS) {
275+
return res;
288276
}
289277
}
290-
return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, pSurfaceInfo, pPresentModeCount,
291-
pPresentModes);
278+
279+
return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, &surface_info_copy,
280+
pPresentModeCount, pPresentModes);
292281
}
293282

294283
VKAPI_ATTR VkResult VKAPI_CALL GetDeviceGroupSurfacePresentModes2EXT(VkDevice device,
@@ -323,20 +312,17 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetDeviceGroupSurfacePresentModes2EXT(
323312
"[VUID-vkGetDeviceGroupSurfacePresentModes2EXT-pSurfaceInfo-parameter]");
324313
abort(); /* Intentionally fail so user can correct issue. */
325314
}
315+
316+
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
326317
if (VK_NULL_HANDLE != pSurfaceInfo->surface) {
327-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(pSurfaceInfo->surface);
328-
if (NULL != icd_surface && NULL != icd_term->surface_list.list &&
329-
icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
330-
icd_term->surface_list.list[icd_surface->surface_index]) {
331-
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy;
332-
surface_info_copy.sType = pSurfaceInfo->sType;
333-
surface_info_copy.pNext = pSurfaceInfo->pNext;
334-
surface_info_copy.surface = icd_term->surface_list.list[icd_surface->surface_index];
335-
return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(
336-
device, &surface_info_copy, pModes);
318+
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface_info_copy.surface);
319+
if (res != VK_SUCCESS) {
320+
return res;
337321
}
338322
}
339-
return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(device, pSurfaceInfo, pModes);
323+
324+
return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(device, &surface_info_copy,
325+
pModes);
340326
}
341327

342328
#endif // VK_USE_PLATFORM_WIN32_KHR

loader/generated/vk_loader_extensions.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,10 +5338,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectTagEXT(
53385338
// If this is a KHR_surface, and the ICD has created its own, we have to replace it with the proper one for the next call.
53395339
} else if (pTagInfo->objectType == VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
53405340
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
5341-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pTagInfo->object;
5342-
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
5343-
&& icd_term->surface_list.list[icd_surface->surface_index]) {
5344-
local_tag_info.object = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
5341+
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pTagInfo->object;
5342+
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
5343+
local_tag_info.object = (uint64_t)surface;
53455344
}
53465345
}
53475346
// If this is an instance we have to replace it with the proper one for the next call.
@@ -5397,10 +5396,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectNameEXT(
53975396
// If this is a KHR_surface, and the ICD has created its own, we have to replace it with the proper one for the next call.
53985397
} else if (pNameInfo->objectType == VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
53995398
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
5400-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pNameInfo->object;
5401-
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
5402-
&& icd_term->surface_list.list[icd_surface->surface_index]) {
5403-
local_name_info.object = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
5399+
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pNameInfo->object;
5400+
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
5401+
local_name_info.object = (uint64_t)surface;
54045402
}
54055403
}
54065404
// If this is an instance we have to replace it with the proper one for the next call.
@@ -6001,10 +5999,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_SetDebugUtilsObjectNameEXT(
60015999
// If this is a KHR_surface, and the ICD has created its own, we have to replace it with the proper one for the next call.
60026000
} else if (pNameInfo->objectType == VK_OBJECT_TYPE_SURFACE_KHR) {
60036001
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
6004-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pNameInfo->objectHandle;
6005-
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
6006-
&& icd_term->surface_list.list[icd_surface->surface_index]) {
6007-
local_name_info.objectHandle = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
6002+
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pNameInfo->objectHandle;
6003+
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
6004+
local_name_info.objectHandle = (uint64_t)surface;
60086005
}
60096006
}
60106007
// If this is an instance we have to replace it with the proper one for the next call.
@@ -6064,10 +6061,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_SetDebugUtilsObjectTagEXT(
60646061
// If this is a KHR_surface, and the ICD has created its own, we have to replace it with the proper one for the next call.
60656062
} else if (pTagInfo->objectType == VK_OBJECT_TYPE_SURFACE_KHR) {
60666063
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
6067-
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pTagInfo->objectHandle;
6068-
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
6069-
&& icd_term->surface_list.list[icd_surface->surface_index]) {
6070-
local_tag_info.objectHandle = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
6064+
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pTagInfo->objectHandle;
6065+
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
6066+
local_tag_info.objectHandle = (uint64_t)surface;
60716067
}
60726068
}
60736069
// If this is an instance we have to replace it with the proper one for the next call.

0 commit comments

Comments
 (0)