blob: 091450bcd35a4178b35d30aaa65701fe1005cdc1 [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.
Michael Lentine92689442015-09-09 12:39:13 -07006# Copyright (C) 2015 Google Inc.
Chia-I Wu44e42362014-09-02 08:32:09 +08007#
8# Permission is hereby granted, free of charge, to any person obtaining a
9# copy of this software and associated documentation files (the "Software"),
10# to deal in the Software without restriction, including without limitation
11# the rights to use, copy, modify, merge, publish, distribute, sublicense,
12# and/or sell copies of the Software, and to permit persons to whom the
13# Software is furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included
16# in all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24# DEALINGS IN THE SOFTWARE.
25#
26# Authors:
27# Chia-I Wu <olv@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080028
29import sys
30
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060031import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080032
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080033def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060034 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
35 " return NULL;" % ((name,) * 3)
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080036
Chia-I Wufb2559d2014-08-01 11:19:52 +080037class Subcommand(object):
38 def __init__(self, argv):
39 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060040 self.headers = vulkan.headers
41 self.protos = vulkan.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080042
43 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080044 print(self.generate())
45
46 def generate(self):
47 copyright = self.generate_copyright()
48 header = self.generate_header()
49 body = self.generate_body()
50 footer = self.generate_footer()
51
52 contents = []
53 if copyright:
54 contents.append(copyright)
55 if header:
56 contents.append(header)
57 if body:
58 contents.append(body)
59 if footer:
60 contents.append(footer)
61
62 return "\n\n".join(contents)
63
64 def generate_copyright(self):
65 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
66
67/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060068 * Vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080069 *
70 * Copyright (C) 2014 LunarG, Inc.
71 *
72 * Permission is hereby granted, free of charge, to any person obtaining a
73 * copy of this software and associated documentation files (the "Software"),
74 * to deal in the Software without restriction, including without limitation
75 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
76 * and/or sell copies of the Software, and to permit persons to whom the
77 * Software is furnished to do so, subject to the following conditions:
78 *
79 * The above copyright notice and this permission notice shall be included
80 * in all copies or substantial portions of the Software.
81 *
82 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
85 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
87 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
88 * DEALINGS IN THE SOFTWARE.
89 */"""
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):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600110 return "\n".join(["#include <vulkan.h>",
Tobin Ehlis2d1d9702015-07-03 09:42:57 -0600111 "#include <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 ],
Jon Ashburn88049b12015-08-13 14:15:07 -0700262 "layerMulti": [
263 "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]
Jon Ashburn88049b12015-08-13 14:15:07 -0700274 if self.library == "VKLayerMulti":
275 self.exports = library_exports["layerMulti"]
276 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;
287; Copyright (C) 2015 LunarG, Inc.
288;
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.
306;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
307
308 def generate_header(self):
309 return "; The following is required on Windows, for exporting symbols from the DLL"
310
311 def generate_body(self):
312 body = []
313
314 body.append("LIBRARY " + self.library)
315 body.append("EXPORTS")
316
Jon Ashburn88049b12015-08-13 14:15:07 -0700317 for proto in self.exports:
Ian Elliott329da012015-09-22 10:51:24 -0600318 if self.library != "VKLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
319 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800320
321 return "\n".join(body)
322
Chia-I Wufb2559d2014-08-01 11:19:52 +0800323def main():
324 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800325 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu731517d2015-01-03 23:48:15 +0800326 "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand,
Chia-I Wu58f20852015-01-04 00:11:17 +0800327 "icd-get-proc-addr": IcdGetProcAddrSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800328 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800329 }
330
331 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
332 print("Usage: %s <subcommand> [options]" % sys.argv[0])
333 print
334 print("Available sucommands are: %s" % " ".join(subcommands))
335 exit(1)
336
337 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
338 subcmd.run()
339
340if __name__ == "__main__":
341 main()