blob: 0a9860299b14aa5262283839145e2c1dbbec8ce0 [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"
Chia-I Wu731517d2015-01-03 23:48:15 +0800173 self.qual = "ICD_EXPORT"
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):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600181 return proto.c_pretty_decl(self.prefix + proto.name, attr="VKAPI")
Chia-I Wu30c78292014-08-04 10:08:08 +0800182
183 def _generate_stubs(self):
184 stubs = []
185 for proto in self.protos:
Chia-I Wu30c78292014-08-04 10:08:08 +0800186 decl = self._generate_stub_decl(proto)
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600187 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600188 stmt = " return VK_ERROR_UNKNOWN;\n"
Chia-I Wu30c78292014-08-04 10:08:08 +0800189 else:
190 stmt = ""
191
Chia-I Wu731517d2015-01-03 23:48:15 +0800192 stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt))
Chia-I Wu30c78292014-08-04 10:08:08 +0800193
194 return "\n\n".join(stubs)
195
Chia-I Wu30c78292014-08-04 10:08:08 +0800196 def generate_body(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800197 return self._generate_stubs()
Chia-I Wu30c78292014-08-04 10:08:08 +0800198
Chia-I Wu58f20852015-01-04 00:11:17 +0800199class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand):
200 def generate_header(self):
201 return "\n".join(["#include <string.h>", "#include \"icd.h\""])
202
203 def generate_body(self):
204 for proto in self.protos:
Jon Ashburn1245cec2015-05-18 13:20:15 -0600205 if proto.name == "GetDeviceProcAddr":
Chia-I Wu58f20852015-01-04 00:11:17 +0800206 gpa_proto = proto
Jon Ashburn53c16772015-05-06 10:15:07 -0600207 if proto.name == "GetInstanceProcAddr":
208 gpa_instance_proto = proto
Chia-I Wu58f20852015-01-04 00:11:17 +0800209
Jon Ashburn53c16772015-05-06 10:15:07 -0600210 gpa_instance_decl = self._generate_stub_decl(gpa_instance_proto)
Chia-I Wu58f20852015-01-04 00:11:17 +0800211 gpa_decl = self._generate_stub_decl(gpa_proto)
212 gpa_pname = gpa_proto.params[-1].name
213
214 lookups = []
215 for proto in self.protos:
216 lookups.append("if (!strcmp(%s, \"%s\"))" %
217 (gpa_pname, proto.name))
218 lookups.append(" return (%s) %s%s;" %
219 (gpa_proto.ret, self.prefix, proto.name))
220
221 body = []
Jon Ashburn53c16772015-05-06 10:15:07 -0600222 body.append("%s %s" % (self.qual, gpa_instance_decl))
223 body.append("{")
Jon Ashburn4ca01502015-07-16 10:12:59 -0600224 body.append(generate_get_proc_addr_check(gpa_pname))
225 body.append("")
226 body.append(" %s += 2;" % gpa_pname)
227 body.append(" %s" % "\n ".join(lookups))
228 body.append("")
Jon Ashburn53c16772015-05-06 10:15:07 -0600229 body.append(" return NULL;")
230 body.append("}")
231 body.append("")
232
Chia-I Wu58f20852015-01-04 00:11:17 +0800233 body.append("%s %s" % (self.qual, gpa_decl))
234 body.append("{")
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +0800235 body.append(generate_get_proc_addr_check(gpa_pname))
Chia-I Wu58f20852015-01-04 00:11:17 +0800236 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600237 body.append(" %s += 2;" % gpa_pname)
Chia-I Wu58f20852015-01-04 00:11:17 +0800238 body.append(" %s" % "\n ".join(lookups))
239 body.append("")
240 body.append(" return NULL;")
241 body.append("}")
242
243 return "\n".join(body)
244
Chia-I Wud98a23c2015-04-11 10:56:50 +0800245class WinDefFileSubcommand(Subcommand):
246 def run(self):
247 library_exports = {
248 "all": [],
249 "icd": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700250 "vkEnumeratePhysicalDevices",
251 "vkCreateInstance",
252 "vkDestroyInstance",
253 "vkGetDeviceProcAddr",
254 "vkGetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800255 ],
256 "layer": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700257 "vkGetInstanceProcAddr",
258 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600259 "vkEnumerateInstanceLayerProperties",
260 "vkEnumerateInstanceExtensionProperties"
Chia-I Wud98a23c2015-04-11 10:56:50 +0800261 ],
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700262 "layer_multi": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700263 "multi2GetInstanceProcAddr",
264 "multi1GetDeviceProcAddr"
265 ]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800266 }
267
268 if len(self.argv) != 2 or self.argv[1] not in library_exports:
269 print("WinDefFileSubcommand: <library-name> {%s}" %
270 "|".join(library_exports.keys()))
271 return
272
273 self.library = self.argv[0]
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700274 if self.library == "VkLayer_multi":
275 self.exports = library_exports["layer_multi"]
Jon Ashburn88049b12015-08-13 14:15:07 -0700276 else:
277 self.exports = library_exports[self.argv[1]]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800278
279 super().run()
280
281 def generate_copyright(self):
282 return """; THIS FILE IS GENERATED. DO NOT EDIT.
283
284;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600285; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800286;
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -0600287; Copyright (C) 2015 Valve Corporation
Chia-I Wud98a23c2015-04-11 10:56:50 +0800288;
289; Permission is hereby granted, free of charge, to any person obtaining a
290; copy of this software and associated documentation files (the "Software"),
291; to deal in the Software without restriction, including without limitation
292; the rights to use, copy, modify, merge, publish, distribute, sublicense,
293; and/or sell copies of the Software, and to permit persons to whom the
294; Software is furnished to do so, subject to the following conditions:
295;
296; The above copyright notice and this permission notice shall be included
297; in all copies or substantial portions of the Software.
298;
299; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
300; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
301; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
302; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
303; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
304; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
305; DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchtercf8ff4f2015-12-16 14:57:27 -0700306;
307; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wud98a23c2015-04-11 10:56:50 +0800308;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
309
310 def generate_header(self):
311 return "; The following is required on Windows, for exporting symbols from the DLL"
312
313 def generate_body(self):
314 body = []
315
316 body.append("LIBRARY " + self.library)
317 body.append("EXPORTS")
318
Jon Ashburn88049b12015-08-13 14:15:07 -0700319 for proto in self.exports:
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700320 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott329da012015-09-22 10:51:24 -0600321 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800322
323 return "\n".join(body)
324
Chia-I Wufb2559d2014-08-01 11:19:52 +0800325def main():
326 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800327 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu731517d2015-01-03 23:48:15 +0800328 "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand,
Chia-I Wu58f20852015-01-04 00:11:17 +0800329 "icd-get-proc-addr": IcdGetProcAddrSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800330 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800331 }
332
333 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
334 print("Usage: %s <subcommand> [options]" % sys.argv[0])
335 print
336 print("Available sucommands are: %s" % " ".join(subcommands))
337 exit(1)
338
339 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
340 subcmd.run()
341
342if __name__ == "__main__":
343 main()