Vulkan-Utility-Libraries/scripts/generators/dispatch_table_generator.py
unknown b0712dfdb1 headers: Fix dispatch table formatting
The code did not follow the provided clang-format file, which causes
conflict when a developer runs clang-format on the repo. This commit fixes
the generator so that the output is what clang-format would generate.
One issue is that clang-format's ColumnLimit will wrap long lines, and is
very difficult to replicate in python code without excessive changes. The
chosen solution is to use a custom clang-format file for the Utilities
folder which ignores the ColumnLimit.
2023-08-25 14:35:36 -06:00

93 lines
3.7 KiB
Python

#!/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
''')
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 vulInitDeviceDispatchTable(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 vulInitInstanceDispatchTable(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('}')
self.write("".join(out))