Skip to content

Commit

Permalink
loader: Lazily allocate ICD surface objects
Browse files Browse the repository at this point in the history
  • Loading branch information
aqnuep authored and charles-lunarg committed Mar 4, 2025
1 parent 224f1c3 commit 322d634
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 347 deletions.
3 changes: 3 additions & 0 deletions loader/allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

#include "loader_common.h"

// The loader always aligns memory allocations to the largest unit size which is the size of a uint64_t
#define loader_aligned_size(x) ((((x) + sizeof(uint64_t) - 1) / sizeof(uint64_t)) * sizeof(uint64_t))

void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope);
void *loader_instance_heap_calloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope);
void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory);
Expand Down
56 changes: 21 additions & 35 deletions loader/extension_manual.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,14 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2E
struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;

VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(surface);

// Unwrap the surface if needed
VkSurfaceKHR unwrapped_surface = surface;
if (NULL != phys_dev_term->this_icd_term->surface_list.list &&
phys_dev_term->this_icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
phys_dev_term->this_icd_term->surface_list.list[icd_surface->surface_index]) {
unwrapped_surface = phys_dev_term->this_icd_term->surface_list.list[icd_surface->surface_index];
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface);
if (res != VK_SUCCESS) {
return res;
}

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

VkSurfaceCapabilitiesKHR surface_caps;
VkResult res =
icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, unwrapped_surface, &surface_caps);
res = icd_term->dispatch.GetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev_term->phys_dev, surface, &surface_caps);
pSurfaceCapabilities->minImageCount = surface_caps.minImageCount;
pSurfaceCapabilities->maxImageCount = surface_caps.maxImageCount;
pSurfaceCapabilities->currentExtent = surface_caps.currentExtent;
Expand Down Expand Up @@ -274,21 +267,17 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfacePresentModes2E
"ICD associated with VkPhysicalDevice does not support GetPhysicalDeviceSurfacePresentModes2EXT");
abort();
}

VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
if (VK_NULL_HANDLE != pSurfaceInfo->surface) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(pSurfaceInfo->surface);
if (NULL != icd_surface && NULL != icd_term->surface_list.list &&
icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
icd_term->surface_list.list[icd_surface->surface_index]) {
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy;
surface_info_copy.sType = pSurfaceInfo->sType;
surface_info_copy.pNext = pSurfaceInfo->pNext;
surface_info_copy.surface = icd_term->surface_list.list[icd_surface->surface_index];
return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, &surface_info_copy,
pPresentModeCount, pPresentModes);
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface_info_copy.surface);
if (res != VK_SUCCESS) {
return res;
}
}
return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, pSurfaceInfo, pPresentModeCount,
pPresentModes);

return icd_term->dispatch.GetPhysicalDeviceSurfacePresentModes2EXT(phys_dev_term->phys_dev, &surface_info_copy,
pPresentModeCount, pPresentModes);
}

VKAPI_ATTR VkResult VKAPI_CALL GetDeviceGroupSurfacePresentModes2EXT(VkDevice device,
Expand Down Expand Up @@ -323,20 +312,17 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetDeviceGroupSurfacePresentModes2EXT(
"[VUID-vkGetDeviceGroupSurfacePresentModes2EXT-pSurfaceInfo-parameter]");
abort(); /* Intentionally fail so user can correct issue. */
}

VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
if (VK_NULL_HANDLE != pSurfaceInfo->surface) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)(pSurfaceInfo->surface);
if (NULL != icd_surface && NULL != icd_term->surface_list.list &&
icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR) &&
icd_term->surface_list.list[icd_surface->surface_index]) {
VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy;
surface_info_copy.sType = pSurfaceInfo->sType;
surface_info_copy.pNext = pSurfaceInfo->pNext;
surface_info_copy.surface = icd_term->surface_list.list[icd_surface->surface_index];
return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(
device, &surface_info_copy, pModes);
VkResult res = wsi_unwrap_icd_surface(icd_term, &surface_info_copy.surface);
if (res != VK_SUCCESS) {
return res;
}
}
return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(device, pSurfaceInfo, pModes);

return dev->loader_dispatch.extension_terminator_dispatch.GetDeviceGroupSurfacePresentModes2EXT(device, &surface_info_copy,
pModes);
}

#endif // VK_USE_PLATFORM_WIN32_KHR
Expand Down
28 changes: 12 additions & 16 deletions loader/generated/vk_loader_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -5338,10 +5338,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectTagEXT(
// 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.
} else if (pTagInfo->objectType == VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pTagInfo->object;
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
&& icd_term->surface_list.list[icd_surface->surface_index]) {
local_tag_info.object = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pTagInfo->object;
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
local_tag_info.object = (uint64_t)surface;
}
}
// If this is an instance we have to replace it with the proper one for the next call.
Expand Down Expand Up @@ -5397,10 +5396,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectNameEXT(
// 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.
} else if (pNameInfo->objectType == VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pNameInfo->object;
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
&& icd_term->surface_list.list[icd_surface->surface_index]) {
local_name_info.object = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pNameInfo->object;
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
local_name_info.object = (uint64_t)surface;
}
}
// If this is an instance we have to replace it with the proper one for the next call.
Expand Down Expand Up @@ -6001,10 +5999,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_SetDebugUtilsObjectNameEXT(
// 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.
} else if (pNameInfo->objectType == VK_OBJECT_TYPE_SURFACE_KHR) {
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pNameInfo->objectHandle;
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
&& icd_term->surface_list.list[icd_surface->surface_index]) {
local_name_info.objectHandle = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pNameInfo->objectHandle;
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
local_name_info.objectHandle = (uint64_t)surface;
}
}
// If this is an instance we have to replace it with the proper one for the next call.
Expand Down Expand Up @@ -6064,10 +6061,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_SetDebugUtilsObjectTagEXT(
// 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.
} else if (pTagInfo->objectType == VK_OBJECT_TYPE_SURFACE_KHR) {
if (NULL != dev && NULL != dev->loader_dispatch.core_dispatch.CreateSwapchainKHR) {
VkIcdSurface *icd_surface = (VkIcdSurface *)(uintptr_t)pTagInfo->objectHandle;
if (NULL != icd_term->surface_list.list && icd_term->surface_list.capacity > icd_surface->surface_index * sizeof(VkSurfaceKHR)
&& icd_term->surface_list.list[icd_surface->surface_index]) {
local_tag_info.objectHandle = (uint64_t)icd_term->surface_list.list[icd_surface->surface_index];
VkSurfaceKHR surface = (VkSurfaceKHR)(uintptr_t)pTagInfo->objectHandle;
if (wsi_unwrap_icd_surface(icd_term, &surface) == VK_SUCCESS) {
local_tag_info.objectHandle = (uint64_t)surface;
}
}
// If this is an instance we have to replace it with the proper one for the next call.
Expand Down
Loading

0 comments on commit 322d634

Please sign in to comment.