blob: e2f37d9ab66a9f2d99633e582ca28dcf9c9c40cf [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>
Mark Lobodzinskicef655d2016-10-27 08:33:09 -060024# Author: Mark Lobodzinski <mark@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080025
26import sys
27
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060028import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080029
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080030def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060031 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
32 " return NULL;" % ((name,) * 3)
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080033
Chia-I Wufb2559d2014-08-01 11:19:52 +080034class Subcommand(object):
35 def __init__(self, argv):
36 self.argv = argv
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060037 self.headers = vulkan.headers
38 self.protos = vulkan.protos
Jamie Madilldbda66b2016-05-10 07:36:20 -070039 self.outfile = None
Chia-I Wufb2559d2014-08-01 11:19:52 +080040
41 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -070042 if self.outfile:
43 with open(self.outfile, "w") as outfile:
44 outfile.write(self.generate())
45 else:
46 print(self.generate())
Chia-I Wufb2559d2014-08-01 11:19:52 +080047
48 def generate(self):
49 copyright = self.generate_copyright()
50 header = self.generate_header()
51 body = self.generate_body()
52 footer = self.generate_footer()
53
54 contents = []
55 if copyright:
56 contents.append(copyright)
57 if header:
58 contents.append(header)
59 if body:
60 contents.append(body)
61 if footer:
62 contents.append(footer)
63
64 return "\n\n".join(contents)
65
66 def generate_copyright(self):
67 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
68
69/*
Karl Schultz8e42f402016-02-02 19:32:33 -070070 * Copyright (c) 2015-2016 The Khronos Group Inc.
71 * Copyright (c) 2015-2016 Valve Corporation
72 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080073 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060074 * Licensed under the Apache License, Version 2.0 (the "License");
75 * you may not use this file except in compliance with the License.
76 * You may obtain a copy of the License at
Chia-I Wufb2559d2014-08-01 11:19:52 +080077 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060078 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wufb2559d2014-08-01 11:19:52 +080079 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060080 * Unless required by applicable law or agreed to in writing, software
81 * distributed under the License is distributed on an "AS IS" BASIS,
82 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83 * See the License for the specific language governing permissions and
84 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060085 *
86 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Mark Lobodzinskib654fe92016-11-02 15:25:26 -060087 * Author: Jon Ashburn <jon@lunarg.com>
88 * Author: Mark Lobodzinski <mark@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080089 */"""
90
91 def generate_header(self):
Chia-I Wu6bdf0192014-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 Wu29271d72015-01-04 10:19:50 +0800100class DispatchTableOpsSubcommand(Subcommand):
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600101 def __init__(self, argv):
102 self.argv = argv
103 self.headers = vulkan.headers_all
104 self.protos = vulkan.protos_all
105 self.outfile = None
106
Chia-I Wu29271d72015-01-04 10:19:50 +0800107 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700108 if len(self.argv) < 1:
Chia-I Wu29271d72015-01-04 10:19:50 +0800109 print("DispatchTableOpsSubcommand: <prefix> unspecified")
110 return
111
112 self.prefix = self.argv[0]
Jamie Madilldbda66b2016-05-10 07:36:20 -0700113
114 if len(self.argv) > 2:
115 print("DispatchTableOpsSubcommand: <prefix> [outfile]")
116 return
117
118 if len(self.argv) == 2:
119 self.outfile = self.argv[1]
120
Michael Lentine695f2c22015-09-09 12:39:13 -0700121 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu29271d72015-01-04 10:19:50 +0800122
123 def generate_header(self):
David Pinedo9316d3b2015-11-06 12:54:48 -0700124 return "\n".join(["#include <vulkan/vulkan.h>",
125 "#include <vulkan/vk_layer.h>",
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600126 "#include <string.h>"])
Chia-I Wu29271d72015-01-04 10:19:50 +0800127
Mark Lobodzinskid4a511c2016-10-31 16:53:40 -0600128 # Determine if a prototype belongs to an extension in a list of extensions
129 def proto_in_ext(self, name, ext_list):
130 for ext in ext_list:
131 for api in ext.protos:
132 if name == api.name:
133 return True
134 return False
135
136 # Pull out wsi flavor from a linux WSI extension API name
137 def extract_wsi_type(self, name):
138 wsi_name_map = [("Xcb", "VK_USE_PLATFORM_XCB_KHR"),
139 ("Xlib", "VK_USE_PLATFORM_XLIB_KHR"),
140 ("Wayland", "VK_USE_PLATFORM_WAYLAND_KHR"),
141 ("Mir", "VK_USE_PLATFORM_MIR_KHR")]
142 result = [item[1] for item in wsi_name_map if item[0] in name]
143 return result[0]
144
Jon Ashburn8fd08252015-05-28 16:25:02 -0600145 def _generate_init_dispatch(self, type):
Chia-I Wu29271d72015-01-04 10:19:50 +0800146 stmts = []
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600147 func = []
148 if type == "device":
Jon Ashburn8fd08252015-05-28 16:25:02 -0600149 # GPA has to be first one and uses wrapped object
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600150 stmts.append(" memset(table, 0, sizeof(*table));")
151 stmts.append(" // Core device function pointers")
152 stmts.append(" table->GetDeviceProcAddr = (PFN_vkGetDeviceProcAddr) gpa(device, \"vkGetDeviceProcAddr\");")
153
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600154 for proto in self.protos:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600155 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or \
156 proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or \
157 proto.params[0].ty == "VkPhysicalDevice" or proto.name == "GetDeviceProcAddr":
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600158 continue
Mark Lobodzinskid4a511c2016-10-31 16:53:40 -0600159
160 # Conditionally compile platform-specific APIs
161 protect = ''
162 if self.proto_in_ext(proto.name, vulkan.win32_only_exts):
163 protect = "VK_USE_PLATFORM_WIN32_KHR"
164 elif self.proto_in_ext(proto.name, vulkan.android_only_exts):
165 protect = "VK_USE_PLATFORM_ANDROID_KHR"
166
167 # Output table entry, with an ifdef if needed
168 if protect != '':
169 stmts.append("#ifdef %s" % protect)
Mark Lobodzinskicef655d2016-10-27 08:33:09 -0600170 stmts.append(" table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" % (proto.name, proto.name, proto.name))
Mark Lobodzinskid4a511c2016-10-31 16:53:40 -0600171 # If platform-specific entry was protected by an #ifdef, close with a #endif
172 if protect != '':
173 stmts.append("#endif // %s" % protect)
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700174 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu29271d72015-01-04 10:19:50 +0800175 % self.prefix)
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700176 func.append("%s VkLayerDispatchTable *table,"
177 % (" " * len(self.prefix)))
178 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600179 % (" " * len(self.prefix)))
180 else:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600181 stmts.append(" memset(table, 0, sizeof(*table));")
182 stmts.append(" // Core instance function pointers")
183 stmts.append(" table->GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) gpa(instance, \"vkGetInstanceProcAddr\");")
184
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600185 for proto in self.protos:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600186 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice" or \
187 proto.name == "CreateDevice" or proto.name == "GetInstanceProcAddr":
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600188 continue
Mark Lobodzinskid4a511c2016-10-31 16:53:40 -0600189
190 protect = ''
191 # Protect platform-dependent WSI APIs with #ifdef
192 if self.proto_in_ext(proto.name, vulkan.win32_wsi_exts):
193 protect = "VK_USE_PLATFORM_WIN32_KHR"
194 elif self.proto_in_ext(proto.name, vulkan.linux_wsi_exts):
195 protect = self.extract_wsi_type(proto.name)
196 elif self.proto_in_ext(proto.name, vulkan.android_wsi_exts):
197 protect = "VK_USE_PLATFORM_ANDROID_KHR"
198 # Protect non-WSI platform-dependent APIs with #ifdef
199 elif self.proto_in_ext(proto.name, vulkan.win32_only_exts):
200 protect = "VK_USE_PLATFORM_WIN32_KHR"
201 elif self.proto_in_ext(proto.name, vulkan.android_only_exts):
202 protect = "VK_USE_PLATFORM_ANDROID_KHR"
203
204 # Output dispatch table entry, with an ifdef if needed
205 if protect != '':
206 stmts.append("#ifdef %s" % protect)
207 stmts.append(" table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" % (proto.name, proto.name, proto.name))
208 if protect != '':
209 stmts.append("#endif // %s" % protect)
210
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700211 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
212 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
213 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
214 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu29271d72015-01-04 10:19:50 +0800215 func.append("{")
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600216 func.append("%s" % "\n".join(stmts))
Chia-I Wu29271d72015-01-04 10:19:50 +0800217 func.append("}")
218
219 return "\n".join(func)
220
Chia-I Wu29271d72015-01-04 10:19:50 +0800221 def generate_body(self):
Jon Ashburn8fd08252015-05-28 16:25:02 -0600222 body = [self._generate_init_dispatch("device"),
223 self._generate_init_dispatch("instance")]
Chia-I Wu29271d72015-01-04 10:19:50 +0800224
225 return "\n\n".join(body)
226
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800227class WinDefFileSubcommand(Subcommand):
228 def run(self):
229 library_exports = {
230 "all": [],
231 "icd": [
Jon Ashburnf5e97542016-01-07 09:46:26 -0700232 "vk_icdGetInstanceProcAddr",
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800233 ],
234 "layer": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700235 "vkGetInstanceProcAddr",
236 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600237 "vkEnumerateInstanceLayerProperties",
238 "vkEnumerateInstanceExtensionProperties"
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800239 ],
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700240 "layer_multi": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700241 "multi2GetInstanceProcAddr",
242 "multi1GetDeviceProcAddr"
243 ]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800244 }
245
Jamie Madilldbda66b2016-05-10 07:36:20 -0700246 if len(self.argv) < 2 or len(self.argv) > 3 or self.argv[1] not in library_exports:
247 print("WinDefFileSubcommand: <library-name> {%s} [outfile]" %
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800248 "|".join(library_exports.keys()))
249 return
250
251 self.library = self.argv[0]
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700252 if self.library == "VkLayer_multi":
253 self.exports = library_exports["layer_multi"]
Jon Ashburn2919a012015-08-13 14:15:07 -0700254 else:
255 self.exports = library_exports[self.argv[1]]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800256
Jamie Madilldbda66b2016-05-10 07:36:20 -0700257 if len(self.argv) == 3:
258 self.outfile = self.argv[2]
259
Jamie Madillc0018852016-04-04 11:20:24 -0400260 super(WinDefFileSubcommand, self).run()
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800261
262 def generate_copyright(self):
263 return """; THIS FILE IS GENERATED. DO NOT EDIT.
264
265;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600266; Vulkan
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800267;
Karl Schultz8e42f402016-02-02 19:32:33 -0700268; Copyright (c) 2015-2016 The Khronos Group Inc.
269; Copyright (c) 2015-2016 Valve Corporation
270; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800271;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600272; Licensed under the Apache License, Version 2.0 (the "License");
273; you may not use this file except in compliance with the License.
274; You may obtain a copy of the License at
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800275;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600276; http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800277;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600278; Unless required by applicable law or agreed to in writing, software
279; distributed under the License is distributed on an "AS IS" BASIS,
280; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
281; See the License for the specific language governing permissions and
282; limitations under the License.
Courtney Goeltzenleuchterbdde0b22015-12-16 14:57:27 -0700283;
284; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800285;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
286
287 def generate_header(self):
288 return "; The following is required on Windows, for exporting symbols from the DLL"
289
290 def generate_body(self):
291 body = []
292
293 body.append("LIBRARY " + self.library)
294 body.append("EXPORTS")
295
Mark Lobodzinskib654fe92016-11-02 15:25:26 -0600296 if self.argv[1] != "all":
297 for proto in self.exports:
298 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
299 body.append(proto)
300 else:
301 for proto in self.protos:
302 if self.exports and proto.name not in self.exports:
303 continue
304 body.append(" vk" + proto.name)
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800305
306 return "\n".join(body)
307
Chia-I Wufb2559d2014-08-01 11:19:52 +0800308def main():
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900309 wsi = {
310 "Win32",
311 "Android",
312 "Xcb",
313 "Xlib",
314 "Wayland",
Petros Bantolas2b40be72016-04-15 11:02:59 +0100315 "Mir",
Johannes van Waverenaabf1fd2016-07-22 16:10:11 -0500316 "Display",
Johannes van Waveren05c25362016-10-30 05:47:59 +0100317 "AllPlatforms",
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900318 }
Chia-I Wufb2559d2014-08-01 11:19:52 +0800319 subcommands = {
Chia-I Wu29271d72015-01-04 10:19:50 +0800320 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800321 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800322 }
323
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900324 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
Johannes van Waveren05c25362016-10-30 05:47:59 +0100325 print("Usage: %s <wsi> <subcommand> <option>" % sys.argv[0])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800326 print
Johannes van Waveren05c25362016-10-30 05:47:59 +0100327 print("Available wsi are: %s" % " ".join(wsi))
328 print("Available subcommands are: %s" % " ".join(subcommands))
Chia-I Wufb2559d2014-08-01 11:19:52 +0800329 exit(1)
330
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900331 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800332 subcmd.run()
333
334if __name__ == "__main__":
335 main()