blob: a1a7165f787969cdcd65b1cd7f08d8f00cde93e1 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu44e42362014-09-02 08:32:09 +08002#
Karl Schultzc85a02f2016-02-02 19:32:33 -07003# Copyright (c) 2015-2016 The Khronos Group Inc.
4# Copyright (c) 2015-2016 Valve Corporation
5# Copyright (c) 2015-2016 LunarG, Inc.
6# Copyright (c) 2015-2016 Google Inc.
Chia-I Wu44e42362014-09-02 08:32:09 +08007#
Jon Ashburn43b53e82016-04-19 11:30:31 -06008# 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
Chia-I Wu44e42362014-09-02 08:32:09 +080011#
Jon Ashburn43b53e82016-04-19 11:30:31 -060012# http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu44e42362014-09-02 08:32:09 +080013#
Jon Ashburn43b53e82016-04-19 11:30:31 -060014# 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.
Chia-I Wu44e42362014-09-02 08:32:09 +080019#
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060020# Author: Chia-I Wu <olv@lunarg.com>
21# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
22# Author: Jon Ashburn <jon@lunarg.com>
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +090023# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080024
25import sys
26
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060027import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080028
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080029def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060030 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
31 " return NULL;" % ((name,) * 3)
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080032
Chia-I Wufb2559d2014-08-01 11:19:52 +080033class Subcommand(object):
34 def __init__(self, argv):
35 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060036 self.headers = vulkan.headers
37 self.protos = vulkan.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080038
39 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080040 print(self.generate())
41
42 def generate(self):
43 copyright = self.generate_copyright()
44 header = self.generate_header()
45 body = self.generate_body()
46 footer = self.generate_footer()
47
48 contents = []
49 if copyright:
50 contents.append(copyright)
51 if header:
52 contents.append(header)
53 if body:
54 contents.append(body)
55 if footer:
56 contents.append(footer)
57
58 return "\n\n".join(contents)
59
60 def generate_copyright(self):
61 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
62
63/*
Karl Schultzc85a02f2016-02-02 19:32:33 -070064 * Copyright (c) 2015-2016 The Khronos Group Inc.
65 * Copyright (c) 2015-2016 Valve Corporation
66 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080067 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060068 * Licensed under the Apache License, Version 2.0 (the "License");
69 * you may not use this file except in compliance with the License.
70 * You may obtain a copy of the License at
Chia-I Wufb2559d2014-08-01 11:19:52 +080071 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060072 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wufb2559d2014-08-01 11:19:52 +080073 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060074 * Unless required by applicable law or agreed to in writing, software
75 * distributed under the License is distributed on an "AS IS" BASIS,
76 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77 * See the License for the specific language governing permissions and
78 * limitations under the License.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060079 *
80 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080081 */"""
82
83 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080084 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080085
86 def generate_body(self):
87 pass
88
89 def generate_footer(self):
90 pass
91
Chia-I Wu28b8d8c2015-01-04 10:19:50 +080092class DispatchTableOpsSubcommand(Subcommand):
93 def run(self):
94 if len(self.argv) != 1:
95 print("DispatchTableOpsSubcommand: <prefix> unspecified")
96 return
97
98 self.prefix = self.argv[0]
Michael Lentine92689442015-09-09 12:39:13 -070099 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800100
101 def generate_header(self):
David Pinedo329ca9e2015-11-06 12:54:48 -0700102 return "\n".join(["#include <vulkan/vulkan.h>",
103 "#include <vulkan/vk_layer.h>",
Jon Ashburnaef65882015-05-04 09:16:41 -0600104 "#include <string.h>"])
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800105
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600106 def _generate_init_dispatch(self, type):
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800107 stmts = []
Jon Ashburnd9564002015-05-07 10:27:37 -0600108 func = []
109 if type == "device":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600110 # GPA has to be first one and uses wrapped object
Courtney Goeltzenleuchterf83cd4a2015-06-26 15:05:29 -0600111 stmts.append("memset(table, 0, sizeof(*table));")
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600112 stmts.append("table->GetDeviceProcAddr =(PFN_vkGetDeviceProcAddr) gpa(device,\"vkGetDeviceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600113 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700114 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice":
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600115 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600116 if proto.name != "GetDeviceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700117 stmts.append("table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800118 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700119 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800120 % self.prefix)
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700121 func.append("%s VkLayerDispatchTable *table,"
122 % (" " * len(self.prefix)))
123 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburnd9564002015-05-07 10:27:37 -0600124 % (" " * len(self.prefix)))
125 else:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600126 stmts.append("table->GetInstanceProcAddr =(PFN_vkGetInstanceProcAddr) gpa(instance,\"vkGetInstanceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600127 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700128 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
Jon Ashburnd9564002015-05-07 10:27:37 -0600129 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600130 if proto.name == "CreateDevice":
131 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600132 if proto.name != "GetInstanceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700133 stmts.append("table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
Jon Ashburnd9564002015-05-07 10:27:37 -0600134 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700135 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
136 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
137 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
138 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800139 func.append("{")
140 func.append(" %s" % "\n ".join(stmts))
141 func.append("}")
142
143 return "\n".join(func)
144
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800145 def generate_body(self):
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600146 body = [self._generate_init_dispatch("device"),
147 self._generate_init_dispatch("instance")]
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800148
149 return "\n\n".join(body)
150
Chia-I Wud98a23c2015-04-11 10:56:50 +0800151class WinDefFileSubcommand(Subcommand):
152 def run(self):
153 library_exports = {
154 "all": [],
155 "icd": [
Jon Ashburn89a1c462016-01-07 09:46:26 -0700156 "vk_icdGetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800157 ],
158 "layer": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700159 "vkGetInstanceProcAddr",
160 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600161 "vkEnumerateInstanceLayerProperties",
162 "vkEnumerateInstanceExtensionProperties"
Chia-I Wud98a23c2015-04-11 10:56:50 +0800163 ],
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700164 "layer_multi": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700165 "multi2GetInstanceProcAddr",
166 "multi1GetDeviceProcAddr"
167 ]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800168 }
169
170 if len(self.argv) != 2 or self.argv[1] not in library_exports:
171 print("WinDefFileSubcommand: <library-name> {%s}" %
172 "|".join(library_exports.keys()))
173 return
174
175 self.library = self.argv[0]
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700176 if self.library == "VkLayer_multi":
177 self.exports = library_exports["layer_multi"]
Jon Ashburn88049b12015-08-13 14:15:07 -0700178 else:
179 self.exports = library_exports[self.argv[1]]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800180
Jamie Madill725788c2016-04-04 11:20:24 -0400181 super(WinDefFileSubcommand, self).run()
Chia-I Wud98a23c2015-04-11 10:56:50 +0800182
183 def generate_copyright(self):
184 return """; THIS FILE IS GENERATED. DO NOT EDIT.
185
186;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600187; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800188;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700189; Copyright (c) 2015-2016 The Khronos Group Inc.
190; Copyright (c) 2015-2016 Valve Corporation
191; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wud98a23c2015-04-11 10:56:50 +0800192;
Jon Ashburn43b53e82016-04-19 11:30:31 -0600193; Licensed under the Apache License, Version 2.0 (the "License");
194; you may not use this file except in compliance with the License.
195; You may obtain a copy of the License at
Chia-I Wud98a23c2015-04-11 10:56:50 +0800196;
Jon Ashburn43b53e82016-04-19 11:30:31 -0600197; http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wud98a23c2015-04-11 10:56:50 +0800198;
Jon Ashburn43b53e82016-04-19 11:30:31 -0600199; Unless required by applicable law or agreed to in writing, software
200; distributed under the License is distributed on an "AS IS" BASIS,
201; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202; See the License for the specific language governing permissions and
203; limitations under the License.
Courtney Goeltzenleuchtercf8ff4f2015-12-16 14:57:27 -0700204;
205; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wud98a23c2015-04-11 10:56:50 +0800206;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
207
208 def generate_header(self):
209 return "; The following is required on Windows, for exporting symbols from the DLL"
210
211 def generate_body(self):
212 body = []
213
214 body.append("LIBRARY " + self.library)
215 body.append("EXPORTS")
216
Jon Ashburn88049b12015-08-13 14:15:07 -0700217 for proto in self.exports:
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700218 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott329da012015-09-22 10:51:24 -0600219 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800220
221 return "\n".join(body)
222
Chia-I Wufb2559d2014-08-01 11:19:52 +0800223def main():
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900224 wsi = {
225 "Win32",
226 "Android",
227 "Xcb",
228 "Xlib",
229 "Wayland",
230 "Mir"
231 }
Chia-I Wufb2559d2014-08-01 11:19:52 +0800232 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800233 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800234 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800235 }
236
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900237 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
238 print("Usage: %s <wsi> <subcommand> [options]" % sys.argv[0])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800239 print
240 print("Available sucommands are: %s" % " ".join(subcommands))
241 exit(1)
242
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900243 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800244 subcmd.run()
245
246if __name__ == "__main__":
247 main()