blob: c6f70f619b24088e4ff414aba00a673e533cd530 [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#
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06003# VK
Chia-I Wu44e42362014-09-02 08:32:09 +08004#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080027
28import sys
29
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060030import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080031
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080032def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060033 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
34 " return NULL;" % ((name,) * 3)
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080035
Chia-I Wufb2559d2014-08-01 11:19:52 +080036class Subcommand(object):
37 def __init__(self, argv):
38 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060039 self.headers = vulkan.headers
40 self.protos = vulkan.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080041
42 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080043 print(self.generate())
44
45 def generate(self):
46 copyright = self.generate_copyright()
47 header = self.generate_header()
48 body = self.generate_body()
49 footer = self.generate_footer()
50
51 contents = []
52 if copyright:
53 contents.append(copyright)
54 if header:
55 contents.append(header)
56 if body:
57 contents.append(body)
58 if footer:
59 contents.append(footer)
60
61 return "\n\n".join(contents)
62
63 def generate_copyright(self):
64 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
65
66/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060067 * Vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080068 *
69 * Copyright (C) 2014 LunarG, Inc.
70 *
71 * Permission is hereby granted, free of charge, to any person obtaining a
72 * copy of this software and associated documentation files (the "Software"),
73 * to deal in the Software without restriction, including without limitation
74 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
75 * and/or sell copies of the Software, and to permit persons to whom the
76 * Software is furnished to do so, subject to the following conditions:
77 *
78 * The above copyright notice and this permission notice shall be included
79 * in all copies or substantial portions of the Software.
80 *
81 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
82 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
83 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
84 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
85 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
86 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
87 * DEALINGS IN THE SOFTWARE.
88 */"""
89
90 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080091 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080092
93 def generate_body(self):
94 pass
95
96 def generate_footer(self):
97 pass
98
Chia-I Wu28b8d8c2015-01-04 10:19:50 +080099class DispatchTableOpsSubcommand(Subcommand):
100 def run(self):
101 if len(self.argv) != 1:
102 print("DispatchTableOpsSubcommand: <prefix> unspecified")
103 return
104
105 self.prefix = self.argv[0]
106 super().run()
107
108 def generate_header(self):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600109 return "\n".join(["#include <vulkan.h>",
110 "#include <vkLayer.h>",
Jon Ashburnaef65882015-05-04 09:16:41 -0600111 "#include <string.h>"])
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800112
Jon Ashburnd9564002015-05-07 10:27:37 -0600113 def _generate_init(self, type):
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800114 stmts = []
Jon Ashburnd9564002015-05-07 10:27:37 -0600115 func = []
116 if type == "device":
117 for proto in self.protos:
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600118 stmts.append("table->%s = (PFN_vk%s) gpa(gpu, \"vk%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800119 (proto.name, proto.name, proto.name))
Jon Ashburnd9564002015-05-07 10:27:37 -0600120 func.append("static inline void %s_initialize_dispatch_table(VkLayerDispatchTable *table,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800121 % self.prefix)
Jon Ashburnd9564002015-05-07 10:27:37 -0600122 func.append("%s PFN_vkGetProcAddr gpa,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800123 % (" " * len(self.prefix)))
Jon Ashburnd9564002015-05-07 10:27:37 -0600124 func.append("%s VkPhysicalDevice gpu)"
125 % (" " * len(self.prefix)))
126 else:
127 for proto in self.protos:
128 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
129 continue
130 stmts.append("table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
131 (proto.name, proto.name, proto.name))
132 func.append("static inline void %s_init_instance_dispatch_table(VkLayerInstanceDispatchTable *table,"
133 % self.prefix)
134 func.append("%s PFN_vkGetInstanceProcAddr gpa,"
135 % (" " * len(self.prefix)))
136 func.append("%s VkInstance instance)"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800137 % (" " * len(self.prefix)))
138 func.append("{")
139 func.append(" %s" % "\n ".join(stmts))
140 func.append("}")
141
142 return "\n".join(func)
143
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800144 def generate_body(self):
Jon Ashburnd9564002015-05-07 10:27:37 -0600145 body = [self._generate_init("device"),
146 self._generate_init("instance")]
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800147
148 return "\n\n".join(body)
149
Chia-I Wu731517d2015-01-03 23:48:15 +0800150class IcdDummyEntrypointsSubcommand(Subcommand):
Chia-I Wu30c78292014-08-04 10:08:08 +0800151 def run(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800152 if len(self.argv) == 1:
153 self.prefix = self.argv[0]
154 self.qual = "static"
155 else:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600156 self.prefix = "vk"
Chia-I Wu731517d2015-01-03 23:48:15 +0800157 self.qual = "ICD_EXPORT"
Chia-I Wu30c78292014-08-04 10:08:08 +0800158
159 super().run()
160
161 def generate_header(self):
162 return "#include \"icd.h\""
163
164 def _generate_stub_decl(self, proto):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600165 return proto.c_pretty_decl(self.prefix + proto.name, attr="VKAPI")
Chia-I Wu30c78292014-08-04 10:08:08 +0800166
167 def _generate_stubs(self):
168 stubs = []
169 for proto in self.protos:
Chia-I Wu30c78292014-08-04 10:08:08 +0800170 decl = self._generate_stub_decl(proto)
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600171 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600172 stmt = " return VK_ERROR_UNKNOWN;\n"
Chia-I Wu30c78292014-08-04 10:08:08 +0800173 else:
174 stmt = ""
175
Chia-I Wu731517d2015-01-03 23:48:15 +0800176 stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt))
Chia-I Wu30c78292014-08-04 10:08:08 +0800177
178 return "\n\n".join(stubs)
179
Chia-I Wu30c78292014-08-04 10:08:08 +0800180 def generate_body(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800181 return self._generate_stubs()
Chia-I Wu30c78292014-08-04 10:08:08 +0800182
Chia-I Wu58f20852015-01-04 00:11:17 +0800183class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand):
184 def generate_header(self):
185 return "\n".join(["#include <string.h>", "#include \"icd.h\""])
186
187 def generate_body(self):
188 for proto in self.protos:
189 if proto.name == "GetProcAddr":
190 gpa_proto = proto
Jon Ashburn53c16772015-05-06 10:15:07 -0600191 if proto.name == "GetInstanceProcAddr":
192 gpa_instance_proto = proto
Chia-I Wu58f20852015-01-04 00:11:17 +0800193
Jon Ashburn53c16772015-05-06 10:15:07 -0600194 gpa_instance_decl = self._generate_stub_decl(gpa_instance_proto)
Chia-I Wu58f20852015-01-04 00:11:17 +0800195 gpa_decl = self._generate_stub_decl(gpa_proto)
196 gpa_pname = gpa_proto.params[-1].name
197
198 lookups = []
199 for proto in self.protos:
200 lookups.append("if (!strcmp(%s, \"%s\"))" %
201 (gpa_pname, proto.name))
202 lookups.append(" return (%s) %s%s;" %
203 (gpa_proto.ret, self.prefix, proto.name))
204
205 body = []
Jon Ashburn53c16772015-05-06 10:15:07 -0600206 body.append("%s %s" % (self.qual, gpa_instance_decl))
207 body.append("{")
208 body.append(" return NULL;")
209 body.append("}")
210 body.append("")
211
Chia-I Wu58f20852015-01-04 00:11:17 +0800212 body.append("%s %s" % (self.qual, gpa_decl))
213 body.append("{")
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +0800214 body.append(generate_get_proc_addr_check(gpa_pname))
Chia-I Wu58f20852015-01-04 00:11:17 +0800215 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600216 body.append(" %s += 2;" % gpa_pname)
Chia-I Wu58f20852015-01-04 00:11:17 +0800217 body.append(" %s" % "\n ".join(lookups))
218 body.append("")
219 body.append(" return NULL;")
220 body.append("}")
221
222 return "\n".join(body)
223
Chia-I Wud98a23c2015-04-11 10:56:50 +0800224class WinDefFileSubcommand(Subcommand):
225 def run(self):
226 library_exports = {
227 "all": [],
228 "icd": [
Tony Barbour8205d902015-04-16 15:59:00 -0600229 "EnumeratePhysicalDevices",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800230 "CreateInstance",
231 "DestroyInstance",
232 "GetProcAddr",
233 ],
234 "layer": [
Jon Ashburn3feb7312015-05-06 14:49:55 -0600235 "GetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800236 "GetProcAddr",
237 "EnumerateLayers",
Jon Ashburnb67859d2015-04-24 14:10:50 -0700238 "GetGlobalExtensionInfo",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800239 ],
240 }
241
242 if len(self.argv) != 2 or self.argv[1] not in library_exports:
243 print("WinDefFileSubcommand: <library-name> {%s}" %
244 "|".join(library_exports.keys()))
245 return
246
247 self.library = self.argv[0]
248 self.exports = library_exports[self.argv[1]]
249
250 super().run()
251
252 def generate_copyright(self):
253 return """; THIS FILE IS GENERATED. DO NOT EDIT.
254
255;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600256; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800257;
258; Copyright (C) 2015 LunarG, Inc.
259;
260; Permission is hereby granted, free of charge, to any person obtaining a
261; copy of this software and associated documentation files (the "Software"),
262; to deal in the Software without restriction, including without limitation
263; the rights to use, copy, modify, merge, publish, distribute, sublicense,
264; and/or sell copies of the Software, and to permit persons to whom the
265; Software is furnished to do so, subject to the following conditions:
266;
267; The above copyright notice and this permission notice shall be included
268; in all copies or substantial portions of the Software.
269;
270; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
271; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
272; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
273; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
274; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
275; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
276; DEALINGS IN THE SOFTWARE.
277;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
278
279 def generate_header(self):
280 return "; The following is required on Windows, for exporting symbols from the DLL"
281
282 def generate_body(self):
283 body = []
284
285 body.append("LIBRARY " + self.library)
286 body.append("EXPORTS")
287
288 for proto in self.protos:
289 if self.exports and proto.name not in self.exports:
290 continue
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600291 body.append(" vk" + proto.name)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800292
293 return "\n".join(body)
294
Chia-I Wufb2559d2014-08-01 11:19:52 +0800295def main():
296 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800297 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu731517d2015-01-03 23:48:15 +0800298 "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand,
Chia-I Wu58f20852015-01-04 00:11:17 +0800299 "icd-get-proc-addr": IcdGetProcAddrSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800300 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800301 }
302
303 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
304 print("Usage: %s <subcommand> [options]" % sys.argv[0])
305 print
306 print("Available sucommands are: %s" % " ".join(subcommands))
307 exit(1)
308
309 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
310 subcmd.run()
311
312if __name__ == "__main__":
313 main()