blob: 4f182e4d5c056405b769a6b094bfc7a87205d68c [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu701f3f62014-09-02 08:32:09 +08002#
Karl Schultz8e42f402016-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 Wu701f3f62014-09-02 08:32:09 +08007#
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
Chia-I Wu701f3f62014-09-02 08:32:09 +080011#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012# http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu701f3f62014-09-02 08:32:09 +080013#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
Chia-I Wu701f3f62014-09-02 08:32:09 +080019#
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060020# Author: Chia-I Wu <olv@lunarg.com>
21# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
22# Author: Jon Ashburn <jon@lunarg.com>
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +090023# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080024
25import sys
26
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060027import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080028
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080029def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060030 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
31 " return NULL;" % ((name,) * 3)
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080032
Chia-I Wufb2559d2014-08-01 11:19:52 +080033class Subcommand(object):
34 def __init__(self, argv):
35 self.argv = argv
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060036 self.headers = vulkan.headers
37 self.protos = vulkan.protos
Jamie Madilldbda66b2016-05-10 07:36:20 -070038 self.outfile = None
Chia-I Wufb2559d2014-08-01 11:19:52 +080039
40 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -070041 if self.outfile:
42 with open(self.outfile, "w") as outfile:
43 outfile.write(self.generate())
44 else:
45 print(self.generate())
Chia-I Wufb2559d2014-08-01 11:19:52 +080046
47 def generate(self):
48 copyright = self.generate_copyright()
49 header = self.generate_header()
50 body = self.generate_body()
51 footer = self.generate_footer()
52
53 contents = []
54 if copyright:
55 contents.append(copyright)
56 if header:
57 contents.append(header)
58 if body:
59 contents.append(body)
60 if footer:
61 contents.append(footer)
62
63 return "\n\n".join(contents)
64
65 def generate_copyright(self):
66 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
67
68/*
Karl Schultz8e42f402016-02-02 19:32:33 -070069 * Copyright (c) 2015-2016 The Khronos Group Inc.
70 * Copyright (c) 2015-2016 Valve Corporation
71 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080072 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060073 * Licensed under the Apache License, Version 2.0 (the "License");
74 * you may not use this file except in compliance with the License.
75 * You may obtain a copy of the License at
Chia-I Wufb2559d2014-08-01 11:19:52 +080076 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060077 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wufb2559d2014-08-01 11:19:52 +080078 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060079 * Unless required by applicable law or agreed to in writing, software
80 * distributed under the License is distributed on an "AS IS" BASIS,
81 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 * See the License for the specific language governing permissions and
83 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060084 *
85 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080086 */"""
87
88 def generate_header(self):
Chia-I Wu6bdf0192014-09-13 13:36:06 +080089 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080090
91 def generate_body(self):
92 pass
93
94 def generate_footer(self):
95 pass
96
Chia-I Wu29271d72015-01-04 10:19:50 +080097class DispatchTableOpsSubcommand(Subcommand):
Mark Youngaa1aa3a2016-07-05 16:41:50 -060098 def __init__(self, argv):
99 self.argv = argv
100 self.headers = vulkan.headers_all
101 self.protos = vulkan.protos_all
102 self.outfile = None
103
Chia-I Wu29271d72015-01-04 10:19:50 +0800104 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700105 if len(self.argv) < 1:
Chia-I Wu29271d72015-01-04 10:19:50 +0800106 print("DispatchTableOpsSubcommand: <prefix> unspecified")
107 return
108
109 self.prefix = self.argv[0]
Jamie Madilldbda66b2016-05-10 07:36:20 -0700110
111 if len(self.argv) > 2:
112 print("DispatchTableOpsSubcommand: <prefix> [outfile]")
113 return
114
115 if len(self.argv) == 2:
116 self.outfile = self.argv[1]
117
Michael Lentine695f2c22015-09-09 12:39:13 -0700118 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu29271d72015-01-04 10:19:50 +0800119
120 def generate_header(self):
David Pinedo9316d3b2015-11-06 12:54:48 -0700121 return "\n".join(["#include <vulkan/vulkan.h>",
122 "#include <vulkan/vk_layer.h>",
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600123 "#include <string.h>"])
Chia-I Wu29271d72015-01-04 10:19:50 +0800124
Jon Ashburn8fd08252015-05-28 16:25:02 -0600125 def _generate_init_dispatch(self, type):
Chia-I Wu29271d72015-01-04 10:19:50 +0800126 stmts = []
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600127 func = []
128 if type == "device":
Jon Ashburn8fd08252015-05-28 16:25:02 -0600129 # GPA has to be first one and uses wrapped object
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600130 stmts.append(" memset(table, 0, sizeof(*table));")
131 stmts.append(" // Core device function pointers")
132 stmts.append(" table->GetDeviceProcAddr = (PFN_vkGetDeviceProcAddr) gpa(device, \"vkGetDeviceProcAddr\");")
133
134 KHR_printed = False
135 EXT_printed = False
136 XLIB_printed = False
137 XCB_printed = False
138 MIR_printed = False
139 WAY_printed = False
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600140 for proto in self.protos:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600141 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or \
142 proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or \
143 proto.params[0].ty == "VkPhysicalDevice" or proto.name == "GetDeviceProcAddr":
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600144 continue
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600145 if 'KHR' in proto.name and not KHR_printed:
146 stmts.append(" // KHR device extension function pointers")
147 KHR_printed = True
148 if 'EXT' in proto.name and not EXT_printed:
149 stmts.append(" // EXT device extension function pointers")
150 EXT_printed = True
151 if XLIB_printed and 'Xlib' not in proto.name:
152 stmts.append("#endif // VK_USE_PLATFORM_XLIB_KHR")
153 XLIB_printed = False
154 if XCB_printed and 'Xcb' not in proto.name:
155 stmts.append("#endif // VK_USE_PLATFORM_XCB_KHR")
156 XCB_printed = False
157 if MIR_printed and 'Mir' not in proto.name:
158 stmts.append("#endif // VK_USE_PLATFORM_MIR_KHR")
159 MIR_printed = False
160 if WAY_printed and 'Wayland' not in proto.name:
161 stmts.append("#endif // VK_USE_PLATFORM_WAYLAND_KHR")
162 WAY_printed = False
163 if 'KHR' in proto.name and 'Xlib' in proto.name:
164 if not XLIB_printed:
165 stmts.append("#ifdef VK_USE_PLATFORM_XLIB_KHR")
166 XLIB_printed = True
167 if 'KHR' in proto.name and 'Xcb' in proto.name:
168 if not XCB_printed:
169 stmts.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
170 XCB_printed = True
171 if 'KHR' in proto.name and 'Mir' in proto.name:
172 if not MIR_printed:
173 stmts.append("#ifdef VK_USE_PLATFORM_MIR_KHR")
174 MIR_printed = True
175 if 'KHR' in proto.name and 'Wayland' in proto.name:
176 if not WAY_printed:
177 stmts.append("#ifdef VK_USE_PLATFORM_WAYLAND_KHR")
178 WAY_printed = True
179 stmts.append(" table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
Chia-I Wu29271d72015-01-04 10:19:50 +0800180 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700181 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu29271d72015-01-04 10:19:50 +0800182 % self.prefix)
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700183 func.append("%s VkLayerDispatchTable *table,"
184 % (" " * len(self.prefix)))
185 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600186 % (" " * len(self.prefix)))
187 else:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600188 stmts.append(" memset(table, 0, sizeof(*table));")
189 stmts.append(" // Core instance function pointers")
190 stmts.append(" table->GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) gpa(instance, \"vkGetInstanceProcAddr\");")
191
192 KHR_printed = False
193 EXT_printed = False
194 XLIB_printed = False
195 XCB_printed = False
196 MIR_printed = False
197 WAY_printed = False
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600198 for proto in self.protos:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600199 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice" or \
200 proto.name == "CreateDevice" or proto.name == "GetInstanceProcAddr":
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600201 continue
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600202 if 'KHR' in proto.name and not KHR_printed:
203 stmts.append(" // KHR instance extension function pointers")
204 KHR_printed = True
205 if 'EXT' in proto.name and not EXT_printed:
206 stmts.append(" // EXT instance extension function pointers")
207 EXT_printed = True
208 if XLIB_printed and 'Xlib' not in proto.name:
209 stmts.append("#endif // VK_USE_PLATFORM_XLIB_KHR")
210 XLIB_printed = False
211 if XCB_printed and 'Xcb' not in proto.name:
212 stmts.append("#endif // VK_USE_PLATFORM_XCB_KHR")
213 XCB_printed = False
214 if MIR_printed and 'Mir' not in proto.name:
215 stmts.append("#endif // VK_USE_PLATFORM_MIR_KHR")
216 MIR_printed = False
217 if WAY_printed and 'Wayland' not in proto.name:
218 stmts.append("#endif // VK_USE_PLATFORM_WAYLAND_KHR")
219 WAY_printed = False
220 if 'KHR' in proto.name and 'Xlib' in proto.name:
221 if not XLIB_printed:
222 stmts.append("#ifdef VK_USE_PLATFORM_XLIB_KHR")
223 XLIB_printed = True
224 if 'KHR' in proto.name and 'Xcb' in proto.name:
225 if not XCB_printed:
226 stmts.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
227 XCB_printed = True
228 if 'KHR' in proto.name and 'Mir' in proto.name:
229 if not MIR_printed:
230 stmts.append("#ifdef VK_USE_PLATFORM_MIR_KHR")
231 MIR_printed = True
232 if 'KHR' in proto.name and 'Wayland' in proto.name:
233 if not WAY_printed:
234 stmts.append("#ifdef VK_USE_PLATFORM_WAYLAND_KHR")
235 WAY_printed = True
236 stmts.append(" table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
237 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700238 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
239 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
240 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
241 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu29271d72015-01-04 10:19:50 +0800242 func.append("{")
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600243 func.append("%s" % "\n".join(stmts))
Chia-I Wu29271d72015-01-04 10:19:50 +0800244 func.append("}")
245
246 return "\n".join(func)
247
Chia-I Wu29271d72015-01-04 10:19:50 +0800248 def generate_body(self):
Jon Ashburn8fd08252015-05-28 16:25:02 -0600249 body = [self._generate_init_dispatch("device"),
250 self._generate_init_dispatch("instance")]
Chia-I Wu29271d72015-01-04 10:19:50 +0800251
252 return "\n\n".join(body)
253
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800254class WinDefFileSubcommand(Subcommand):
255 def run(self):
256 library_exports = {
257 "all": [],
258 "icd": [
Jon Ashburnf5e97542016-01-07 09:46:26 -0700259 "vk_icdGetInstanceProcAddr",
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800260 ],
261 "layer": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700262 "vkGetInstanceProcAddr",
263 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600264 "vkEnumerateInstanceLayerProperties",
265 "vkEnumerateInstanceExtensionProperties"
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800266 ],
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700267 "layer_multi": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700268 "multi2GetInstanceProcAddr",
269 "multi1GetDeviceProcAddr"
270 ]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800271 }
272
Jamie Madilldbda66b2016-05-10 07:36:20 -0700273 if len(self.argv) < 2 or len(self.argv) > 3 or self.argv[1] not in library_exports:
274 print("WinDefFileSubcommand: <library-name> {%s} [outfile]" %
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800275 "|".join(library_exports.keys()))
276 return
277
278 self.library = self.argv[0]
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700279 if self.library == "VkLayer_multi":
280 self.exports = library_exports["layer_multi"]
Jon Ashburn2919a012015-08-13 14:15:07 -0700281 else:
282 self.exports = library_exports[self.argv[1]]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800283
Jamie Madilldbda66b2016-05-10 07:36:20 -0700284 if len(self.argv) == 3:
285 self.outfile = self.argv[2]
286
Jamie Madillc0018852016-04-04 11:20:24 -0400287 super(WinDefFileSubcommand, self).run()
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800288
289 def generate_copyright(self):
290 return """; THIS FILE IS GENERATED. DO NOT EDIT.
291
292;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600293; Vulkan
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800294;
Karl Schultz8e42f402016-02-02 19:32:33 -0700295; Copyright (c) 2015-2016 The Khronos Group Inc.
296; Copyright (c) 2015-2016 Valve Corporation
297; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800298;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600299; Licensed under the Apache License, Version 2.0 (the "License");
300; you may not use this file except in compliance with the License.
301; You may obtain a copy of the License at
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800302;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600303; http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800304;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600305; Unless required by applicable law or agreed to in writing, software
306; distributed under the License is distributed on an "AS IS" BASIS,
307; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
308; See the License for the specific language governing permissions and
309; limitations under the License.
Courtney Goeltzenleuchterbdde0b22015-12-16 14:57:27 -0700310;
311; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800312;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
313
314 def generate_header(self):
315 return "; The following is required on Windows, for exporting symbols from the DLL"
316
317 def generate_body(self):
318 body = []
319
320 body.append("LIBRARY " + self.library)
321 body.append("EXPORTS")
322
Jon Ashburn2919a012015-08-13 14:15:07 -0700323 for proto in self.exports:
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700324 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott0b4d6242015-09-22 10:51:24 -0600325 body.append( proto)
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800326
327 return "\n".join(body)
328
Chia-I Wufb2559d2014-08-01 11:19:52 +0800329def main():
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900330 wsi = {
331 "Win32",
332 "Android",
333 "Xcb",
334 "Xlib",
335 "Wayland",
Petros Bantolas2b40be72016-04-15 11:02:59 +0100336 "Mir",
337 "Display"
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900338 }
Chia-I Wufb2559d2014-08-01 11:19:52 +0800339 subcommands = {
Chia-I Wu29271d72015-01-04 10:19:50 +0800340 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800341 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800342 }
343
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900344 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
345 print("Usage: %s <wsi> <subcommand> [options]" % sys.argv[0])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800346 print
347 print("Available sucommands are: %s" % " ".join(subcommands))
348 exit(1)
349
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900350 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800351 subcmd.run()
352
353if __name__ == "__main__":
354 main()