blob: bed56645b461cfa29772130464df86a0ef3b115f [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 Goeltzenleuchter8a17da52015-10-29 13:50:34 -06003# Copyright (C) 2015 Valve Corporation
Michael Lentine92689442015-09-09 12:39:13 -07004# Copyright (C) 2015 Google Inc.
Chia-I Wu44e42362014-09-02 08:32:09 +08005#
6# Permission is hereby granted, free of charge, to any person obtaining a
7# copy of this software and associated documentation files (the "Software"),
8# to deal in the Software without restriction, including without limitation
9# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10# and/or sell copies of the Software, and to permit persons to whom the
11# Software is furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22# DEALINGS IN THE SOFTWARE.
23#
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060024# Author: Chia-I Wu <olv@lunarg.com>
25# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
26# Author: Jon Ashburn <jon@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/*
Chia-I Wufb2559d2014-08-01 11:19:52 +080067 *
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -060068 * Copyright (C) 2015 Valve Corporation
Chia-I Wufb2559d2014-08-01 11:19:52 +080069 *
70 * Permission is hereby granted, free of charge, to any person obtaining a
71 * copy of this software and associated documentation files (the "Software"),
72 * to deal in the Software without restriction, including without limitation
73 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
74 * and/or sell copies of the Software, and to permit persons to whom the
75 * Software is furnished to do so, subject to the following conditions:
76 *
77 * The above copyright notice and this permission notice shall be included
78 * in all copies or substantial portions of the Software.
79 *
80 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
83 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
85 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
86 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060087 *
88 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080089 */"""
90
91 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080092 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080093
94 def generate_body(self):
95 pass
96
97 def generate_footer(self):
98 pass
99
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800100class DispatchTableOpsSubcommand(Subcommand):
101 def run(self):
102 if len(self.argv) != 1:
103 print("DispatchTableOpsSubcommand: <prefix> unspecified")
104 return
105
106 self.prefix = self.argv[0]
Michael Lentine92689442015-09-09 12:39:13 -0700107 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800108
109 def generate_header(self):
David Pinedo329ca9e2015-11-06 12:54:48 -0700110 return "\n".join(["#include <vulkan/vulkan.h>",
111 "#include <vulkan/vk_layer.h>",
Jon Ashburnaef65882015-05-04 09:16:41 -0600112 "#include <string.h>"])
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800113
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600114 def _generate_init_dispatch(self, type):
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800115 stmts = []
Jon Ashburnd9564002015-05-07 10:27:37 -0600116 func = []
117 if type == "device":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600118 # GPA has to be first one and uses wrapped object
119 stmts.append("VkDevice device = (VkDevice) devw->nextObject;")
120 stmts.append("PFN_vkGetDeviceProcAddr gpa = (PFN_vkGetDeviceProcAddr) devw->pGPA;")
121 stmts.append("VkDevice baseDevice = (VkDevice) devw->baseObject;")
122 stmts.append("// GPA has to be first entry inited and uses wrapped object since it triggers init")
Courtney Goeltzenleuchterf83cd4a2015-06-26 15:05:29 -0600123 stmts.append("memset(table, 0, sizeof(*table));")
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600124 stmts.append("table->GetDeviceProcAddr =(PFN_vkGetDeviceProcAddr) gpa(device,\"vkGetDeviceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600125 for proto in self.protos:
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600126 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or (proto.params[0].ty == "VkPhysicalDevice" and proto.name != "CreateDevice"):
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600127 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600128 if proto.name != "GetDeviceProcAddr" and 'KHR' not in proto.name:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600129 stmts.append("table->%s = (PFN_vk%s) gpa(baseDevice, \"vk%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800130 (proto.name, proto.name, proto.name))
Jon Ashburnd9564002015-05-07 10:27:37 -0600131 func.append("static inline void %s_initialize_dispatch_table(VkLayerDispatchTable *table,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800132 % self.prefix)
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600133 func.append("%s const VkBaseLayerObject *devw)"
Jon Ashburnd9564002015-05-07 10:27:37 -0600134 % (" " * len(self.prefix)))
135 else:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600136 # GPA has to be first one and uses wrapped object
137 stmts.append("VkInstance instance = (VkInstance) instw->nextObject;")
138 stmts.append("PFN_vkGetInstanceProcAddr gpa = (PFN_vkGetInstanceProcAddr) instw->pGPA;")
139 stmts.append("VkInstance baseInstance = (VkInstance) instw->baseObject;")
140 stmts.append("// GPA has to be first entry inited and uses wrapped object since it triggers init")
141 stmts.append("table->GetInstanceProcAddr =(PFN_vkGetInstanceProcAddr) gpa(instance,\"vkGetInstanceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600142 for proto in self.protos:
Jon Ashburnba4a1952015-06-16 12:44:51 -0600143 if proto.name != "CreateInstance" and proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
Jon Ashburnd9564002015-05-07 10:27:37 -0600144 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600145 if proto.name == "CreateDevice":
146 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600147 if proto.name != "GetInstanceProcAddr" and 'KHR' not in proto.name:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600148 stmts.append("table->%s = (PFN_vk%s) gpa(baseInstance, \"vk%s\");" %
Jon Ashburnd9564002015-05-07 10:27:37 -0600149 (proto.name, proto.name, proto.name))
150 func.append("static inline void %s_init_instance_dispatch_table(VkLayerInstanceDispatchTable *table,"
151 % self.prefix)
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600152 func.append("%s const VkBaseLayerObject *instw)"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800153 % (" " * len(self.prefix)))
154 func.append("{")
155 func.append(" %s" % "\n ".join(stmts))
156 func.append("}")
157
158 return "\n".join(func)
159
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800160 def generate_body(self):
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600161 body = [self._generate_init_dispatch("device"),
162 self._generate_init_dispatch("instance")]
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800163
164 return "\n\n".join(body)
165
Chia-I Wu731517d2015-01-03 23:48:15 +0800166class IcdDummyEntrypointsSubcommand(Subcommand):
Chia-I Wu30c78292014-08-04 10:08:08 +0800167 def run(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800168 if len(self.argv) == 1:
169 self.prefix = self.argv[0]
170 self.qual = "static"
171 else:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600172 self.prefix = "vk"
Jon Ashburn89a1c462016-01-07 09:46:26 -0700173 self.qual = ""
Chia-I Wu30c78292014-08-04 10:08:08 +0800174
175 super().run()
176
177 def generate_header(self):
178 return "#include \"icd.h\""
179
180 def _generate_stub_decl(self, proto):
Jon Ashburn89a1c462016-01-07 09:46:26 -0700181 if proto.name == "GetInstanceProcAddr":
182 return proto.c_pretty_decl(self.prefix + "_icd" + proto.name, attr="ICD_EXPORT VKAPI")
183 else:
184 return proto.c_pretty_decl(self.prefix + proto.name, attr="VKAPI")
Chia-I Wu30c78292014-08-04 10:08:08 +0800185
186 def _generate_stubs(self):
187 stubs = []
188 for proto in self.protos:
Chia-I Wu30c78292014-08-04 10:08:08 +0800189 decl = self._generate_stub_decl(proto)
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600190 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600191 stmt = " return VK_ERROR_UNKNOWN;\n"
Chia-I Wu30c78292014-08-04 10:08:08 +0800192 else:
193 stmt = ""
194
Chia-I Wu731517d2015-01-03 23:48:15 +0800195 stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt))
Chia-I Wu30c78292014-08-04 10:08:08 +0800196
197 return "\n\n".join(stubs)
198
Chia-I Wu30c78292014-08-04 10:08:08 +0800199 def generate_body(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800200 return self._generate_stubs()
Chia-I Wu30c78292014-08-04 10:08:08 +0800201
Chia-I Wu58f20852015-01-04 00:11:17 +0800202class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand):
203 def generate_header(self):
204 return "\n".join(["#include <string.h>", "#include \"icd.h\""])
205
206 def generate_body(self):
207 for proto in self.protos:
Jon Ashburn1245cec2015-05-18 13:20:15 -0600208 if proto.name == "GetDeviceProcAddr":
Chia-I Wu58f20852015-01-04 00:11:17 +0800209 gpa_proto = proto
Jon Ashburn53c16772015-05-06 10:15:07 -0600210 if proto.name == "GetInstanceProcAddr":
211 gpa_instance_proto = proto
Chia-I Wu58f20852015-01-04 00:11:17 +0800212
Jon Ashburn53c16772015-05-06 10:15:07 -0600213 gpa_instance_decl = self._generate_stub_decl(gpa_instance_proto)
Chia-I Wu58f20852015-01-04 00:11:17 +0800214 gpa_decl = self._generate_stub_decl(gpa_proto)
215 gpa_pname = gpa_proto.params[-1].name
216
217 lookups = []
218 for proto in self.protos:
219 lookups.append("if (!strcmp(%s, \"%s\"))" %
220 (gpa_pname, proto.name))
Jon Ashburn89a1c462016-01-07 09:46:26 -0700221 if proto.name != "GetInstanceProcAddr":
222 lookups.append(" return (%s) %s%s;" %
Chia-I Wu58f20852015-01-04 00:11:17 +0800223 (gpa_proto.ret, self.prefix, proto.name))
Jon Ashburn89a1c462016-01-07 09:46:26 -0700224 else:
225 lookups.append(" return (%s) %s%s;" %
226 (gpa_proto.ret, self.prefix, "_icdGetInstanceProcAddr"))
Chia-I Wu58f20852015-01-04 00:11:17 +0800227
228 body = []
Jon Ashburn53c16772015-05-06 10:15:07 -0600229 body.append("%s %s" % (self.qual, gpa_instance_decl))
230 body.append("{")
Jon Ashburn4ca01502015-07-16 10:12:59 -0600231 body.append(generate_get_proc_addr_check(gpa_pname))
232 body.append("")
233 body.append(" %s += 2;" % gpa_pname)
234 body.append(" %s" % "\n ".join(lookups))
235 body.append("")
Jon Ashburn53c16772015-05-06 10:15:07 -0600236 body.append(" return NULL;")
237 body.append("}")
238 body.append("")
239
Chia-I Wu58f20852015-01-04 00:11:17 +0800240 body.append("%s %s" % (self.qual, gpa_decl))
241 body.append("{")
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +0800242 body.append(generate_get_proc_addr_check(gpa_pname))
Chia-I Wu58f20852015-01-04 00:11:17 +0800243 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600244 body.append(" %s += 2;" % gpa_pname)
Chia-I Wu58f20852015-01-04 00:11:17 +0800245 body.append(" %s" % "\n ".join(lookups))
246 body.append("")
247 body.append(" return NULL;")
248 body.append("}")
249
250 return "\n".join(body)
251
Chia-I Wud98a23c2015-04-11 10:56:50 +0800252class WinDefFileSubcommand(Subcommand):
253 def run(self):
254 library_exports = {
255 "all": [],
256 "icd": [
Jon Ashburn89a1c462016-01-07 09:46:26 -0700257 "vk_icdGetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800258 ],
259 "layer": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700260 "vkGetInstanceProcAddr",
261 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600262 "vkEnumerateInstanceLayerProperties",
263 "vkEnumerateInstanceExtensionProperties"
Chia-I Wud98a23c2015-04-11 10:56:50 +0800264 ],
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700265 "layer_multi": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700266 "multi2GetInstanceProcAddr",
267 "multi1GetDeviceProcAddr"
268 ]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800269 }
270
271 if len(self.argv) != 2 or self.argv[1] not in library_exports:
272 print("WinDefFileSubcommand: <library-name> {%s}" %
273 "|".join(library_exports.keys()))
274 return
275
276 self.library = self.argv[0]
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700277 if self.library == "VkLayer_multi":
278 self.exports = library_exports["layer_multi"]
Jon Ashburn88049b12015-08-13 14:15:07 -0700279 else:
280 self.exports = library_exports[self.argv[1]]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800281
282 super().run()
283
284 def generate_copyright(self):
285 return """; THIS FILE IS GENERATED. DO NOT EDIT.
286
287;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600288; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800289;
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -0600290; Copyright (C) 2015 Valve Corporation
Chia-I Wud98a23c2015-04-11 10:56:50 +0800291;
292; Permission is hereby granted, free of charge, to any person obtaining a
293; copy of this software and associated documentation files (the "Software"),
294; to deal in the Software without restriction, including without limitation
295; the rights to use, copy, modify, merge, publish, distribute, sublicense,
296; and/or sell copies of the Software, and to permit persons to whom the
297; Software is furnished to do so, subject to the following conditions:
298;
299; The above copyright notice and this permission notice shall be included
300; in all copies or substantial portions of the Software.
301;
302; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
303; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
304; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
305; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
306; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
307; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
308; DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchtercf8ff4f2015-12-16 14:57:27 -0700309;
310; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wud98a23c2015-04-11 10:56:50 +0800311;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
312
313 def generate_header(self):
314 return "; The following is required on Windows, for exporting symbols from the DLL"
315
316 def generate_body(self):
317 body = []
318
319 body.append("LIBRARY " + self.library)
320 body.append("EXPORTS")
321
Jon Ashburn88049b12015-08-13 14:15:07 -0700322 for proto in self.exports:
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700323 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott329da012015-09-22 10:51:24 -0600324 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800325
326 return "\n".join(body)
327
Chia-I Wufb2559d2014-08-01 11:19:52 +0800328def main():
329 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800330 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu731517d2015-01-03 23:48:15 +0800331 "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand,
Chia-I Wu58f20852015-01-04 00:11:17 +0800332 "icd-get-proc-addr": IcdGetProcAddrSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800333 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800334 }
335
336 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
337 print("Usage: %s <subcommand> [options]" % sys.argv[0])
338 print
339 print("Available sucommands are: %s" % " ".join(subcommands))
340 exit(1)
341
342 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
343 subcmd.run()
344
345if __name__ == "__main__":
346 main()