Skip to content

Commit

Permalink
util: Fixup and add 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 this header and
 vk_dispatch_table_helper.h have been moved into a new header
 vul_dispatch_table.h.

The structs VulDeviceDispatchTable and VulInstanceDispatchTable
struct contain function pointers for the device and instance, respectively.

The functions vul_init_device_dispatch_table and
vul_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 c8b671c
Show file tree
Hide file tree
Showing 15 changed files with 1,813 additions and 212 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()
4 changes: 2 additions & 2 deletions docs/generated_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ If only dealing with a single file, run `scripts/generate_source.py` with `--ta

```bash
# Example - only generates chassis.h
scripts/generate_source.py external/Vulkan-Headers/registry/ --target vk_layer_dispatch_table.h
scripts/generate_source.py external/Vulkan-Headers/registry/ --target vul_dispatch_table.h
```

When making change to the `scripts/` folder, make sure to run `generate_source.py`
(Code generation does **not** happen automatically at build time.)
(Code generation does **not** happen automatically at build time.)
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/vul_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/vul_dispatch_table.h

Large diffs are not rendered by default.

18 changes: 5 additions & 13 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.dispatch_table_generator import DispatchTableOutputGenerator
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',
#},
'vul_dispatch_table.h' : {
'generator' : DispatchTableOutputGenerator,
'directory' : 'include/vulkan/utility',
},
'vk_enum_string_helper.h' : {
'generator' : EnumStringHelperOutputGenerator,
'directory' : 'include/vulkan',
Expand Down
93 changes: 93 additions & 0 deletions scripts/generators/dispatch_table_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/python3 -i
#
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0

import os
from generators.base_generator import BaseGenerator

class DispatchTableOutputGenerator(BaseGenerator):
def __init__(self):
BaseGenerator.__init__(self)

def generate(self):
out = []
out.append(f'''// *** THIS FILE IS GENERATED - DO NOT EDIT ***
// See {os.path.basename(__file__)} for modifications
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
\n''')

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('''
// Instance function pointer dispatch table
typedef struct VulInstanceDispatchTable_ {
PFN_GetPhysicalDeviceProcAddr GetPhysicalDeviceProcAddr;
''')
for command in [x for x in self.vk.commands.values() if x.instance]:
out.extend([f'#ifdef {command.protect}\n'] if command.protect else [])
out.append(f' PFN_{command.name} {command.name[2:]};\n')
out.extend([f'#endif //{command.protect}\n'] if command.protect else [])
out.append('} VulInstanceDispatchTable;\n')

out.append('''
// Device function pointer dispatch table
typedef struct VulDeviceDispatchTable_ {
''')
for command in [x for x in self.vk.commands.values() if x.device]:
out.extend([f'#ifdef {command.protect}\n'] if command.protect else [])
out.append(f' PFN_{command.name} {command.name[2:]};\n')
out.extend([f'#endif //{command.protect}\n'] if command.protect else [])
out.append('} VulDeviceDispatchTable;\n')

out.append('''
static inline void vul_init_device_dispatch_table(VkDevice device, VulDeviceDispatchTable *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 vul_init_instance_dispatch_table(VkInstance instance, VulInstanceDispatchTable_ *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))
134 changes: 0 additions & 134 deletions scripts/generators/dispatch_table_helper_generator.py

This file was deleted.

56 changes: 0 additions & 56 deletions scripts/generators/layer_dispatch_table_generator.py

This file was deleted.

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(vul_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
vul_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)
Loading

0 comments on commit c8b671c

Please sign in to comment.