Skip to content

Commit

Permalink
util: Fixup and add Layer Dispatch Table header
Browse files Browse the repository at this point in the history
Fixes the vk_layer_dispatch_table.h header file so that they can be used
in other projects. The contents of vk_dispatch_table_helper.h have been
moved into vk_layer_dispatch_table.h.

The structs VkLayerDispatchTable and VkLayerInstanceDispatchTable
struct contain function pointers for the device and instance, respectively.

The functions layer_init_device_dispatch_table and
layer_init_instance_dispatch_table fill out the aforementioned structs,
making the task of setting up the disptach table in a layer much simpler.
  • Loading branch information
charles-lunarg committed Aug 23, 2023
1 parent 8ea7803 commit 7ccda3a
Show file tree
Hide file tree
Showing 15 changed files with 1,803 additions and 156 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ if(${CMAKE_CXX_COMPILER_ID} MATCHES "(GNU|Clang)")
)
endif()

add_library(VulkanUtilityHeaders INTERFACE)
add_library(Vulkan::UtilityHeaders ALIAS VulkanUtilityHeaders)

add_subdirectory(src)
add_subdirectory(include)

Expand All @@ -49,9 +52,11 @@ if (VUL_IS_TOP_LEVEL)
endif()

include(GNUInstallDirs)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

set_target_properties(VulkanLayerSettings PROPERTIES EXPORT_NAME "LayerSettings")
install(TARGETS VulkanLayerSettings EXPORT VulkanUtilityLibrariesConfig INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
set_target_properties(VulkanUtilityHeaders PROPERTIES EXPORT_NAME "UtilityHeaders")
install(TARGETS VulkanLayerSettings VulkanUtilityHeaders EXPORT VulkanUtilityLibrariesConfig INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT VulkanUtilityLibrariesConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/VulkanUtilityLibraries NAMESPACE Vulkan::)
endif()
11 changes: 11 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ target_sources(VulkanLayerSettings PRIVATE
vulkan/layer/vk_layer_settings.hpp
vulkan/layer/vk_layer_settings_ext.h
)

target_sources(VulkanUtilityHeaders PRIVATE
vulkan/utility/vk_layer_dispatch_table.h
vulkan/vk_enum_string_helper.h
)

# NOTE: Because Vulkan::Headers header files are exposed in the public facing interface
# we must expose this library as public to users.
target_link_Libraries(VulkanUtilityHeaders INTERFACE Vulkan::Headers)

target_include_directories(VulkanUtilityHeaders INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
1,577 changes: 1,577 additions & 0 deletions include/vulkan/utility/vk_layer_dispatch_table.h

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions scripts/generate_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
from reg import Registry

from generators.base_generator import BaseGeneratorOptions
from generators.dispatch_table_helper_generator import DispatchTableHelperOutputGenerator
from generators.layer_dispatch_table_generator import LayerDispatchTableOutputGenerator
from generators.enum_string_helper_generator import EnumStringHelperOutputGenerator

# Build up a list of all generators and custom options
generators = {
# TODO: vk_dispatch_table_helper.h doesn't compile.
# 'vk_dispatch_table_helper.h' : {
# 'generator' : DispatchTableHelperOutputGenerator,
# 'directory' : 'include/vulkan',
#},
# TODO: vk_layer_dispatch_table.h doesn't compile.
#
#'vk_layer_dispatch_table.h' : {
# 'generator' : LayerDispatchTableOutputGenerator,
# 'directory' : 'include/vulkan',
#},
'vk_layer_dispatch_table.h' : {
'generator' : LayerDispatchTableOutputGenerator,
'directory' : 'include/vulkan/utility',
},
'vk_enum_string_helper.h' : {
'generator' : EnumStringHelperOutputGenerator,
'directory' : 'include/vulkan',
Expand Down
134 changes: 0 additions & 134 deletions scripts/generators/dispatch_table_helper_generator.py

This file was deleted.

43 changes: 40 additions & 3 deletions scripts/generators/layer_dispatch_table_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def generate(self):
//
// SPDX-License-Identifier: Apache-2.0
\n''')
out.append('// NOLINTBEGIN') # Wrap for clang-tidy to ignore

out.append('''
#pragma once
#include <vulkan/vulkan.h>
#include <string.h>
typedef PFN_vkVoidFunction (VKAPI_PTR *PFN_GetPhysicalDeviceProcAddr)(VkInstance instance, const char* pName);
''')
out.append('''
Expand All @@ -52,5 +55,39 @@ def generate(self):
out.extend([f'#endif //{command.protect}\n'] if command.protect else [])
out.append('} VkLayerDispatchTable;\n')

out.append('// NOLINTEND') # Wrap for clang-tidy to ignore
self.write("".join(out))
out.append('''
static inline void layer_init_device_dispatch_table(VkDevice device, VkLayerDispatchTable *table, PFN_vkGetDeviceProcAddr gdpa) {
memset(table, 0, sizeof(*table));
// Device function pointers
table->GetDeviceProcAddr = gdpa;
''')

for command in [x for x in self.vk.commands.values() if x.device and x.name != 'vkGetDeviceProcAddr']:
out.extend([f'#ifdef {command.protect}\n'] if command.protect else [])
out.append(f' table->{command.name[2:]} = (PFN_{command.name}) gdpa(device, "{command.name}");\n')
out.extend([f'#endif // {command.protect}\n'] if command.protect else [])
out.append('}\n')

out.append('''
static inline void layer_init_instance_dispatch_table(VkInstance instance, VkLayerInstanceDispatchTable *table, PFN_vkGetInstanceProcAddr gipa) {
memset(table, 0, sizeof(*table));
// Instance function pointers
table->GetInstanceProcAddr = gipa;
table->GetPhysicalDeviceProcAddr = (PFN_GetPhysicalDeviceProcAddr) gipa(instance, "vk_layerGetPhysicalDeviceProcAddr");
''')

for command in [x for x in self.vk.commands.values() if x.instance and x.name not in [
'vkCreateInstance',
'vkCreateDevice',
'vkGetPhysicalDeviceProcAddr',
'vkEnumerateInstanceExtensionProperties',
'vkEnumerateInstanceLayerProperties',
'vkEnumerateInstanceVersion',
'vkGetInstanceProcAddr',
]]:
out.extend([f'#ifdef {command.protect}\n'] if command.protect else [])
out.append(f' table->{command.name[2:]} = (PFN_{command.name}) gipa(instance, "{command.name}");\n')
out.extend([f'#endif // {command.protect}\n'] if command.protect else [])
out.append('}\n')

self.write("".join(out))
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# SPDX-License-Identifier: Apache-2.0
add_subdirectory(layer)
add_subdirectory(generated)
add_subdirectory(layer_dispatch_table)
7 changes: 6 additions & 1 deletion tests/add_subdirectory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_compile_features(add_subdirectory_example PRIVATE c_std_99)
target_sources(add_subdirectory_example PRIVATE
client.c
vk_enum_string_helper.c
vk_layer_dispatch_table.c
)

# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries
Expand All @@ -26,4 +27,8 @@ if (NOT TARGET Vulkan::LayerSettings)
message(FATAL_ERROR "Vulkan::LayerSettings target not defined!")
endif()

target_link_libraries(add_subdirectory_example PRIVATE Vulkan::LayerSettings)
if (NOT TARGET Vulkan::UtilityHeaders)
message(FATAL_ERROR "Vulkan::UtilityHeaders target not defined!")
endif()

target_link_libraries(add_subdirectory_example PRIVATE Vulkan::LayerSettings Vulkan::UtilityHeaders)
6 changes: 3 additions & 3 deletions tests/add_subdirectory/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ VkBool32 foobar() {
VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE;
vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", NULL, NULL, NULL, &layerSettingSet);

VkBool32 result = vlHasLayerSetting(layerSettingSet, "setting_key") ? VK_TRUE : VK_FALSE;
VkBool32 result = vlHasLayerSetting(layerSettingSet, "setting_key") ? VK_TRUE : VK_FALSE;

vlDestroyLayerSettingSet(layerSettingSet, NULL);

return result;
}
return result;
}
31 changes: 31 additions & 0 deletions tests/add_subdirectory/vk_layer_dispatch_table.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include <vulkan/utility/vk_layer_dispatch_table.h>

PFN_vkVoidFunction local_vkGetInstanceProcAddr(VkInstance instance, const char *pName) {
(void)instance;
(void)pName;
return nullptr;
}

PFN_vkVoidFunction local_vkGetDeviceProcAddr(VkDevice device, const char *pName) {
(void)device;
(void)pName;
return nullptr;
}

void foobar() {
VkLayerDispatchTable dispatch_table;
VkLayerInstanceDispatchTable instance_dispatch_table = {};

VkInstance instance = {};

layer_init_instance_dispatch_table(instance, &instance_dispatch_table, foobar);

VkDevice device = {};

layer_init_device_dispatch_table(device, &dispatch_table, local_vkGetDeviceProcAddr);
}
7 changes: 6 additions & 1 deletion tests/find_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_compile_features(find_package_example PRIVATE c_std_99)
target_sources(find_package_example PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/client.c
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_enum_string_helper.c
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_layer_dispatch_table.c
)

# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries
Expand All @@ -26,4 +27,8 @@ if (NOT TARGET Vulkan::LayerSettings)
message(FATAL_ERROR "Vulkan::LayerSettings target not defined!")
endif()

target_link_libraries(find_package_example PRIVATE Vulkan::LayerSettings)
if (NOT TARGET Vulkan::UtilityHeaders)
message(FATAL_ERROR "Vulkan::UtilityHeaders target not defined!")
endif()

target_link_libraries(find_package_example PRIVATE Vulkan::LayerSettings Vulkan::UtilityHeaders)
38 changes: 38 additions & 0 deletions tests/layer_dispatch_table/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0

find_package(GTest REQUIRED CONFIG)

include(GoogleTest)

add_executable(test_layer_dispatch_table)

target_sources(test_layer_dispatch_table PRIVATE
test_c_interface.c
test_cpp_interface.cpp
)

target_link_libraries(test_layer_dispatch_table PRIVATE
GTest::gtest
GTest::gtest_main
Vulkan::UtilityHeaders
)

target_include_directories(test_layer_dispatch_table PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

add_executable(test_layer_dispatch_table_c_interface)

target_sources(test_layer_dispatch_table_c_interface PRIVATE
test_c_interface.c)

target_link_libraries(test_layer_dispatch_table_c_interface PRIVATE
GTest::gtest
GTest::gtest_main
Vulkan::UtilityHeaders)

target_compile_features(test_layer_dispatch_table_c_interface PRIVATE c_std_99)

gtest_discover_tests(test_layer_dispatch_table test_layer_dispatch_table_c_interface)
Loading

0 comments on commit 7ccda3a

Please sign in to comment.