Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader: Lazily allocate ICD surface objects #1659

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 21 additions & 40 deletions loader/extension_manual.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,14 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfaceCapabilities2E
VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, VkSurfaceCapabilities2EXT *pSurfaceCapabilities) {
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 +120,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 +266,15 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceSurfacePresentModes2E
"ICD associated with VkPhysicalDevice does not support GetPhysicalDeviceSurfacePresentModes2EXT");
abort();
}
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);
}

VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
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 +309,15 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetDeviceGroupSurfacePresentModes2EXT(
"[VUID-vkGetDeviceGroupSurfacePresentModes2EXT-pSurfaceInfo-parameter]");
abort(); /* Intentionally fail so user can correct issue. */
}
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);
}

VkPhysicalDeviceSurfaceInfo2KHR surface_info_copy = *pSurfaceInfo;
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
Loading