File: dispatch_table_generator.py

package info (click to toggle)
vulkan-utility-libraries 1.4.335.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,476 kB
  • sloc: cpp: 106,816; ansic: 16,788; python: 2,218; sh: 23; makefile: 6
file content (99 lines) | stat: -rw-r--r-- 3,675 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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 base_generator import BaseGenerator
from generators.generator_utils import PlatformGuardHelper

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>

// clang-format off

typedef PFN_vkVoidFunction(VKAPI_PTR *PFN_GetPhysicalDeviceProcAddr)(VkInstance instance, const char *pName);
''')
        out.append('''
// Instance function pointer dispatch table
typedef struct VkuInstanceDispatchTable_ {
    PFN_GetPhysicalDeviceProcAddr GetPhysicalDeviceProcAddr;

''')
        guard_helper = PlatformGuardHelper()
        for command in [x for x in self.vk.commands.values() if x.instance]:
            out.extend(guard_helper.add_guard(command.protect))
            out.append(f'    PFN_{command.name} {command.name[2:]};\n')
        out.extend(guard_helper.add_guard(None))
        out.append('} VkuInstanceDispatchTable;\n')

        out.append('''
// Device function pointer dispatch table
typedef struct VkuDeviceDispatchTable_ {
''')
        for command in [x for x in self.vk.commands.values() if x.device]:
            out.extend(guard_helper.add_guard(command.protect))
            out.append(f'    PFN_{command.name} {command.name[2:]};\n')
        out.extend(guard_helper.add_guard(None))
        out.append('} VkuDeviceDispatchTable;\n')

        out.append('''
static inline void vkuInitDeviceDispatchTable(VkDevice device, VkuDeviceDispatchTable *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(guard_helper.add_guard(command.protect))
            out.append(f'    table->{command.name[2:]} = (PFN_{command.name})gdpa(device, "{command.name}");\n')
        out.extend(guard_helper.add_guard(None))
        out.append('}\n')

        out.append('''
static inline void vkuInitInstanceDispatchTable(VkInstance instance, VkuInstanceDispatchTable *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(guard_helper.add_guard(command.protect))
            out.append(f'    table->{command.name[2:]} = (PFN_{command.name})gipa(instance, "{command.name}");\n')
        out.extend(guard_helper.add_guard(None))
        out.append('}\n')

        out.append('// clang-format on')

        self.write("".join(out))