Debian Codegraph @

scripts/generators/dispatch_table_helper_generator.py

raw
1 #!/usr/bin/python3 -i
2 #
3 # Copyright (c) 2015-2021 The Khronos Group Inc.
4 # Copyright (c) 2015-2021 Valve Corporation
5 # Copyright (c) 2015-2021 LunarG, Inc.
6 # Copyright (c) 2015-2021 Google Inc.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 # Author: Mark Lobodzinski <[email protected]>
21 # Author: Charles Giessen <[email protected]>
22
23 import os
24 from base_generator import BaseGenerator
25
26 class DispatchTableHelperGenerator(BaseGenerator):
27 def __init__(self):
28 BaseGenerator.__init__(self)
29
30 def generate(self):
31 out = []
32
33 out.append(f'''#pragma once
34 // *** THIS FILE IS GENERATED - DO NOT EDIT ***
35 // See {os.path.basename(__file__)} for modifications
36
37 /*
38 * Copyright (c) 2015-2025 The Khronos Group Inc.
39 * Copyright (c) 2015-2025 Valve Corporation
40 * Copyright (c) 2015-2025 LunarG, Inc.
41 *
42 * Licensed under the Apache License, Version 2.0 (the "License");
43 * you may not use this file except in compliance with the License.
44 * You may obtain a copy of the License at
45 *
46 * http://www.apache.org/licenses/LICENSE-2.0
47 *
48 * Unless required by applicable law or agreed to in writing, software
49 * distributed under the License is distributed on an "AS IS" BASIS,
50 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51 * See the License for the specific language governing permissions and
52 * limitations under the License.
53 *
54 * Author: Courtney Goeltzenleuchter <[email protected]>
55 * Author: Jon Ashburn <[email protected]>
56 * Author: Mark Lobodzinski <[email protected]>
57 * Author: Charles Giessen <[email protected]>
58 */
59
60 #include <vulkan/vulkan.h>
61 #include <vulkan/vk_layer.h>
62 #include <string.h>
63 #include "loader/generated/vk_layer_dispatch_table.h"
64
65 ''')
66
67 self.OutputDispatchTableHelper(out, 'device')
68 out.append('\n\n')
69 self.OutputDispatchTableHelper(out, 'instance')
70
71 self.write(''.join(out))
72
73 # Create a dispatch table from the corresponding table_type and append it to out
74 def OutputDispatchTableHelper(self, out: list, table_type: str):
75 out.append(' // clang-format off\n')
76 if table_type == 'device':
77 out.append('static inline void layer_init_device_dispatch_table(VkDevice device, VkLayerDispatchTable *table, PFN_vkGetDeviceProcAddr gpa) {\n')
78 out.append(' memset(table, 0, sizeof(*table));\n')
79 out.append(' table->magic = DEVICE_DISP_TABLE_MAGIC_NUMBER;\n\n')
80 out.append(' // Device function pointers\n')
81 else:
82 out.append('static inline void layer_init_instance_dispatch_table(VkInstance instance, VkLayerInstanceDispatchTable *table, PFN_vkGetInstanceProcAddr gpa) {\n')
83 out.append(' memset(table, 0, sizeof(*table));\n\n')
84 out.append(' // Instance function pointers\n')
85
86 for command_name, command in self.vk.commands.items():
87 if (table_type == 'device' and not command.device) or (table_type == 'instance' and command.device):
88 continue
89
90 if command.protect is not None:
91 out.append( f'#if defined({command.protect})\n')
92
93 # If we're looking for the proc we are passing in, just point the table to it. This fixes the issue where
94 # a layer overrides the function name for the loader.
95 if (table_type == 'device' and command_name == 'vkGetDeviceProcAddr'):
96 out.append( ' table->GetDeviceProcAddr = gpa;\n')
97 elif (table_type != 'device' and command_name == 'vkGetInstanceProcAddr'):
98 out.append( ' table->GetInstanceProcAddr = gpa;\n')
99 else:
100 out.append( f' table->{command_name[2:]} = (PFN_{command_name})gpa({table_type}, "{command_name}");\n')
101 if command.protect is not None:
102 out.append( f'#endif // {command.protect}\n')
103 out.append(' // clang-format on\n')
104 out.append('}')