Skip to content

Avoid using shared object entrypoints as function pointers #82

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

Merged
merged 3 commits into from
Feb 17, 2025
Merged
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
63 changes: 22 additions & 41 deletions generator/generate_vulkan_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,14 @@
'vkEnumerateInstanceVersion',
}

# These functions are excluded from generated intercept and dispatch tables
NO_INTERCEPT_OR_DISPATCH_FUNCTIONS = {
# Functions resolved by the loaded not the implementation
'vkGetDeviceProcAddr',
'vkGetInstanceProcAddr',
}

# These functions are excluded from generated intercept tables
NO_INTERCEPT_FUNCTIONS = {
# Functions exported as shared object symbols and resolved by loader
'vkEnumerateDeviceExtensionProperties',
'vkEnumerateDeviceLayerProperties',
'vkEnumerateInstanceExtensionProperties',
'vkEnumerateInstanceLayerProperties',
}

# These functions are excluded from generated dispatch tables
NO_DISPATCH_FUNCTIONS = {
# Functions resolved by the loader not the implementation
'vkCreateDevice',
'vkCreateInstance',
'vkEnumerateInstanceExtensionProperties',
'vkGetDeviceProcAddr',
'vkGetInstanceProcAddr',
}

# These functions are excluded from generated declarations
Expand All @@ -69,13 +55,13 @@
'vkCreateInstance',
'vkDestroyDevice',
'vkDestroyInstance',
'vkGetDeviceProcAddr',
'vkGetInstanceProcAddr',
'vkEnumerateDeviceExtensionProperties',
'vkEnumerateDeviceLayerProperties',
'vkEnumerateInstanceExtensionProperties',
'vkEnumerateInstanceLayerProperties',
'vkGetDeviceImageMemoryRequirementsKHR',
'vkGetDeviceProcAddr',
'vkGetInstanceProcAddr',
}

# Filter out extensions from these vendors by default
Expand Down Expand Up @@ -434,21 +420,16 @@ def generate_instance_dispatch_table(
continue

assert command.name

if command.name in NO_INTERCEPT_OR_DISPATCH_FUNCTIONS:
continue

plat_define = mapping.get_platform_define(command.name)

ttype = f'PFN_{command.name}'

if command.name not in NO_INTERCEPT_FUNCTIONS:
if plat_define:
itable_members.append(f'#if defined({plat_define})')
if plat_define:
itable_members.append(f'#if defined({plat_define})')

itable_members.append(f' ENTRY({command.name}),')
if plat_define:
itable_members.append('#endif')
itable_members.append(f' ENTRY({command.name}),')

if plat_define:
itable_members.append('#endif')

if command.name not in NO_DISPATCH_FUNCTIONS:
if plat_define:
Expand Down Expand Up @@ -586,9 +567,6 @@ def generate_instance_defs(
continue

assert command.name
if command.name in NO_INTERCEPT_FUNCTIONS:
continue

if command.name in CUSTOM_FUNCTIONS:
continue

Expand Down Expand Up @@ -657,25 +635,28 @@ def generate_device_dispatch_table(
continue

assert command.name
if command.name in NO_INTERCEPT_OR_DISPATCH_FUNCTIONS:
continue

plat_define = mapping.get_platform_define(command.name)
ttype = f'PFN_{command.name}'

if plat_define:
itable_members.append(f'#if defined({plat_define})')
dispatch_table_members.append(f'#if defined({plat_define})')
dispatch_table_inits.append(f'#if defined({plat_define})')

itable_members.append(f' ENTRY({command.name}),')
dispatch_table_members.append(f' {ttype} {command.name};')
dispatch_table_inits.append(f' ENTRY({command.name});')

if plat_define:
itable_members.append('#endif')
dispatch_table_members.append('#endif')
dispatch_table_inits.append('#endif')

if command.name not in NO_DISPATCH_FUNCTIONS:
if plat_define:
dispatch_table_members.append(f'#if defined({plat_define})')
dispatch_table_inits.append(f'#if defined({plat_define})')

dispatch_table_members.append(f' {ttype} {command.name};')
dispatch_table_inits.append(f' ENTRY({command.name});')

if plat_define:
dispatch_table_members.append('#endif')
dispatch_table_inits.append('#endif')

data = data.replace('{ITABLE_MEMBERS}', '\n'.join(itable_members))
data = data.replace('{DTABLE_MEMBERS}', '\n'.join(dispatch_table_members))
Expand Down
1 change: 1 addition & 0 deletions source_common/framework/device_dispatch_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ static const struct DeviceInterceptTableEntry deviceIntercepts[] = {
ENTRY(vkGetDeviceMemoryOpaqueCaptureAddress),
ENTRY(vkGetDeviceMemoryOpaqueCaptureAddressKHR),
ENTRY(vkGetDeviceMicromapCompatibilityEXT),
ENTRY(vkGetDeviceProcAddr),
ENTRY(vkGetDeviceQueue),
ENTRY(vkGetDeviceQueue2),
ENTRY(vkGetEventStatus),
Expand Down
5 changes: 5 additions & 0 deletions source_common/framework/instance_dispatch_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ static const struct InstanceInterceptTableEntry instanceIntercepts[] = {
ENTRY(vkDestroyDebugUtilsMessengerEXT),
ENTRY(vkDestroyInstance),
ENTRY(vkDestroySurfaceKHR),
ENTRY(vkEnumerateDeviceExtensionProperties),
ENTRY(vkEnumerateDeviceLayerProperties),
ENTRY(vkEnumerateInstanceExtensionProperties),
ENTRY(vkEnumerateInstanceLayerProperties),
ENTRY(vkEnumeratePhysicalDeviceGroups),
ENTRY(vkEnumeratePhysicalDeviceGroupsKHR),
ENTRY(vkEnumeratePhysicalDeviceQueueFamilyPerformanceQueryCountersKHR),
Expand All @@ -84,6 +88,7 @@ static const struct InstanceInterceptTableEntry instanceIntercepts[] = {
ENTRY(vkGetDisplayPlaneCapabilities2KHR),
ENTRY(vkGetDisplayPlaneCapabilitiesKHR),
ENTRY(vkGetDisplayPlaneSupportedDisplaysKHR),
ENTRY(vkGetInstanceProcAddr),
ENTRY(vkGetPhysicalDeviceCalibrateableTimeDomainsEXT),
ENTRY(vkGetPhysicalDeviceCalibrateableTimeDomainsKHR),
ENTRY(vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR),
Expand Down
64 changes: 7 additions & 57 deletions source_common/framework/manual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,9 @@ struct DispatchTableEntry
/**
* @brief Utility macro to define a lookup for a core function.
*/
#define VK_TABLE_ENTRY(func) \
{ \
STR(func), reinterpret_cast<PFN_vkVoidFunction>(func) \
}

/**
* @brief Utility macro to define a lookup for a layer-dispatch-only function.
*/
#define VK_TABLE_ENTRYL(func) \
{ \
STR(func), reinterpret_cast<PFN_vkVoidFunction>(layer_##func) \
#define VK_TABLE_ENTRY(func) \
{ \
STR(func), reinterpret_cast<PFN_vkVoidFunction>(layer_##func<user_tag>) \
}

/* See header for documentation. */
Expand Down Expand Up @@ -118,28 +110,6 @@ VkLayerDeviceCreateInfo* getChainInfo(const VkDeviceCreateInfo* pCreateInfo)
return const_cast<VkLayerDeviceCreateInfo*>(info);
}

/* See header for documentation. */
PFN_vkVoidFunction getFixedInstanceLayerFunction(const char* name)
{
static const DispatchTableEntry layerFunctions[] = {
VK_TABLE_ENTRY(vkGetInstanceProcAddr),
VK_TABLE_ENTRY(vkEnumerateDeviceLayerProperties),
VK_TABLE_ENTRY(vkEnumerateDeviceExtensionProperties),
VK_TABLE_ENTRY(vkEnumerateInstanceLayerProperties),
VK_TABLE_ENTRY(vkEnumerateInstanceExtensionProperties),
};

for (auto& function : layerFunctions)
{
if (!strcmp(function.name, name))
{
return function.function;
}
}

return nullptr;
}

/* See header for documentation. */
PFN_vkVoidFunction getInstanceLayerFunction(const char* name)
{
Expand All @@ -157,20 +127,6 @@ PFN_vkVoidFunction getInstanceLayerFunction(const char* name)
/* See header for documentation. */
PFN_vkVoidFunction getDeviceLayerFunction(const char* name)
{
static const DispatchTableEntry layerFunctions[] = {
VK_TABLE_ENTRY(vkGetDeviceProcAddr),
VK_TABLE_ENTRY(vkEnumerateDeviceExtensionProperties),
VK_TABLE_ENTRY(vkEnumerateDeviceLayerProperties),
};

for (auto& function : layerFunctions)
{
if (!strcmp(function.name, name))
{
return function.function;
}
}

for (auto& function : deviceIntercepts)
{
if (!strcmp(function.name, name))
Expand Down Expand Up @@ -570,16 +526,9 @@ static void enableDeviceVkExtImageCompressionControl(VkDeviceCreateInfo& createI
/** See Vulkan API for documentation. */
PFN_vkVoidFunction layer_vkGetInstanceProcAddr_default(VkInstance instance, const char* pName)
{
// Always expose these functions ...
PFN_vkVoidFunction layerFunction = getFixedInstanceLayerFunction(pName);
if (layerFunction)
{
return layerFunction;
}

// Otherwise, only expose functions that the driver exposes to avoid
// changing queryable interface behavior seen by the application
layerFunction = getInstanceLayerFunction(pName);
// Only expose functions that the driver exposes to avoid changing
// queryable interface behavior seen by the application
auto layerFunction = getInstanceLayerFunction(pName);
if (instance)
{
std::unique_lock<std::mutex> lock {g_vulkanLock};
Expand Down Expand Up @@ -667,6 +616,7 @@ VkResult layer_vkEnumerateDeviceExtensionProperties_default(VkPhysicalDevice gpu

// For other cases forward to the driver to handle it
assert(!pLayerName);
assert(gpu);

// Hold the lock to access layer-wide global store
std::unique_lock<std::mutex> lock {g_vulkanLock};
Expand Down