blob: 6f478a6f98ee97df22b31a601eae9f697b4cea01 [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#
Karl Schultzc85a02f2016-02-02 19:32:33 -07008# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and/or associated documentation files (the "Materials"), to
10# deal in the Materials without restriction, including without limitation the
11# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12# sell copies of the Materials, and to permit persons to whom the Materials
13# are furnished to do so, subject to the following conditions:
Chia-I Wu44e42362014-09-02 08:32:09 +080014#
Karl Schultzc85a02f2016-02-02 19:32:33 -070015# The above copyright notice(s) and this permission notice shall be included
16# in all copies or substantial portions of the Materials.
Chia-I Wu44e42362014-09-02 08:32:09 +080017#
Karl Schultzc85a02f2016-02-02 19:32:33 -070018# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wu44e42362014-09-02 08:32:09 +080019# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -070020# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21#
22# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25# USE OR OTHER DEALINGS IN THE MATERIALS
Chia-I Wu44e42362014-09-02 08:32:09 +080026#
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060027# Author: Chia-I Wu <olv@lunarg.com>
28# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
29# Author: Jon Ashburn <jon@lunarg.com>
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +090030# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080031
32import sys
33
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060034import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080035
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080036def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060037 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
38 " return NULL;" % ((name,) * 3)
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080039
Chia-I Wufb2559d2014-08-01 11:19:52 +080040class Subcommand(object):
41 def __init__(self, argv):
42 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060043 self.headers = vulkan.headers
44 self.protos = vulkan.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080045
46 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080047 print(self.generate())
48
49 def generate(self):
50 copyright = self.generate_copyright()
51 header = self.generate_header()
52 body = self.generate_body()
53 footer = self.generate_footer()
54
55 contents = []
56 if copyright:
57 contents.append(copyright)
58 if header:
59 contents.append(header)
60 if body:
61 contents.append(body)
62 if footer:
63 contents.append(footer)
64
65 return "\n\n".join(contents)
66
67 def generate_copyright(self):
68 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
69
70/*
Karl Schultzc85a02f2016-02-02 19:32:33 -070071 * Copyright (c) 2015-2016 The Khronos Group Inc.
72 * Copyright (c) 2015-2016 Valve Corporation
73 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080074 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070075 * Permission is hereby granted, free of charge, to any person obtaining a copy
76 * of this software and/or associated documentation files (the "Materials"), to
77 * deal in the Materials without restriction, including without limitation the
78 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
79 * sell copies of the Materials, and to permit persons to whom the Materials are
80 * furnished to do so, subject to the following conditions:
Chia-I Wufb2559d2014-08-01 11:19:52 +080081 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070082 * The above copyright notice(s) and this permission notice shall be included in
83 * all copies or substantial portions of the Materials.
Chia-I Wufb2559d2014-08-01 11:19:52 +080084 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070085 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wufb2559d2014-08-01 11:19:52 +080086 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -070087 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
88 *
89 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
90 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
91 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
92 * USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060093 *
94 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080095 */"""
96
97 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080098 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080099
100 def generate_body(self):
101 pass
102
103 def generate_footer(self):
104 pass
105
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800106class DispatchTableOpsSubcommand(Subcommand):
107 def run(self):
108 if len(self.argv) != 1:
109 print("DispatchTableOpsSubcommand: <prefix> unspecified")
110 return
111
112 self.prefix = self.argv[0]
Michael Lentine92689442015-09-09 12:39:13 -0700113 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800114
115 def generate_header(self):
David Pinedo329ca9e2015-11-06 12:54:48 -0700116 return "\n".join(["#include <vulkan/vulkan.h>",
117 "#include <vulkan/vk_layer.h>",
Jon Ashburnaef65882015-05-04 09:16:41 -0600118 "#include <string.h>"])
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800119
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600120 def _generate_init_dispatch(self, type):
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800121 stmts = []
Jon Ashburnd9564002015-05-07 10:27:37 -0600122 func = []
123 if type == "device":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600124 # GPA has to be first one and uses wrapped object
Courtney Goeltzenleuchterf83cd4a2015-06-26 15:05:29 -0600125 stmts.append("memset(table, 0, sizeof(*table));")
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600126 stmts.append("table->GetDeviceProcAddr =(PFN_vkGetDeviceProcAddr) gpa(device,\"vkGetDeviceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600127 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700128 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 -0600129 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600130 if proto.name != "GetDeviceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700131 stmts.append("table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800132 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700133 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800134 % self.prefix)
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700135 func.append("%s VkLayerDispatchTable *table,"
136 % (" " * len(self.prefix)))
137 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburnd9564002015-05-07 10:27:37 -0600138 % (" " * len(self.prefix)))
139 else:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600140 stmts.append("table->GetInstanceProcAddr =(PFN_vkGetInstanceProcAddr) gpa(instance,\"vkGetInstanceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600141 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700142 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
Jon Ashburnd9564002015-05-07 10:27:37 -0600143 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600144 if proto.name == "CreateDevice":
145 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600146 if proto.name != "GetInstanceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700147 stmts.append("table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
Jon Ashburnd9564002015-05-07 10:27:37 -0600148 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700149 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
150 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
151 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
152 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800153 func.append("{")
154 func.append(" %s" % "\n ".join(stmts))
155 func.append("}")
156
157 return "\n".join(func)
158
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800159 def generate_body(self):
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600160 body = [self._generate_init_dispatch("device"),
161 self._generate_init_dispatch("instance")]
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800162
163 return "\n\n".join(body)
164
Chia-I Wud98a23c2015-04-11 10:56:50 +0800165class WinDefFileSubcommand(Subcommand):
166 def run(self):
167 library_exports = {
168 "all": [],
169 "icd": [
Jon Ashburn89a1c462016-01-07 09:46:26 -0700170 "vk_icdGetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800171 ],
172 "layer": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700173 "vkGetInstanceProcAddr",
174 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600175 "vkEnumerateInstanceLayerProperties",
176 "vkEnumerateInstanceExtensionProperties"
Chia-I Wud98a23c2015-04-11 10:56:50 +0800177 ],
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700178 "layer_multi": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700179 "multi2GetInstanceProcAddr",
180 "multi1GetDeviceProcAddr"
181 ]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800182 }
183
184 if len(self.argv) != 2 or self.argv[1] not in library_exports:
185 print("WinDefFileSubcommand: <library-name> {%s}" %
186 "|".join(library_exports.keys()))
187 return
188
189 self.library = self.argv[0]
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700190 if self.library == "VkLayer_multi":
191 self.exports = library_exports["layer_multi"]
Jon Ashburn88049b12015-08-13 14:15:07 -0700192 else:
193 self.exports = library_exports[self.argv[1]]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800194
195 super().run()
196
197 def generate_copyright(self):
198 return """; THIS FILE IS GENERATED. DO NOT EDIT.
199
200;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600201; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800202;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700203; Copyright (c) 2015-2016 The Khronos Group Inc.
204; Copyright (c) 2015-2016 Valve Corporation
205; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wud98a23c2015-04-11 10:56:50 +0800206;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700207; Permission is hereby granted, free of charge, to any person obtaining a copy
208; of this software and/or associated documentation files (the "Materials"), to
209; deal in the Materials without restriction, including without limitation the
210; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
211; sell copies of the Materials, and to permit persons to whom the Materials are
212; furnished to do so, subject to the following conditions:
Chia-I Wud98a23c2015-04-11 10:56:50 +0800213;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700214; The above copyright notice(s) and this permission notice shall be included in
215; all copies or substantial portions of the Materials.
Chia-I Wud98a23c2015-04-11 10:56:50 +0800216;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700217; THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wud98a23c2015-04-11 10:56:50 +0800218; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -0700219; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
220;
221; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
222; DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
223; OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
224; USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchtercf8ff4f2015-12-16 14:57:27 -0700225;
226; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wud98a23c2015-04-11 10:56:50 +0800227;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
228
229 def generate_header(self):
230 return "; The following is required on Windows, for exporting symbols from the DLL"
231
232 def generate_body(self):
233 body = []
234
235 body.append("LIBRARY " + self.library)
236 body.append("EXPORTS")
237
Jon Ashburn88049b12015-08-13 14:15:07 -0700238 for proto in self.exports:
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700239 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott329da012015-09-22 10:51:24 -0600240 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800241
242 return "\n".join(body)
243
Chia-I Wufb2559d2014-08-01 11:19:52 +0800244def main():
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900245 wsi = {
246 "Win32",
247 "Android",
248 "Xcb",
249 "Xlib",
250 "Wayland",
251 "Mir"
252 }
Chia-I Wufb2559d2014-08-01 11:19:52 +0800253 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800254 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800255 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800256 }
257
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900258 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
259 print("Usage: %s <wsi> <subcommand> [options]" % sys.argv[0])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800260 print
261 print("Available sucommands are: %s" % " ".join(subcommands))
262 exit(1)
263
Mun, Gwan-gyeong83ae0c02016-02-22 20:23:59 +0900264 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800265 subcmd.run()
266
267if __name__ == "__main__":
268 main()