blob: 2fcb06987b6357325658f14b8c6567be4dcfecaf [file] [log] [blame]
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -06001#!/usr/bin/env python3
2#
3# VK
4#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25
26import os, sys
27
28# add main repo directory so vulkan.py can be imported. This needs to be a complete path.
29ld_path = os.path.dirname(os.path.abspath(__file__))
30main_path = os.path.abspath(ld_path + "/../")
31sys.path.append(main_path)
32
33import vulkan
34
35def generate_get_proc_addr_check(name):
36 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
37 " return NULL;" % ((name,) * 3)
38
39class Subcommand(object):
40 def __init__(self, argv):
41 self.argv = argv
42 self.headers = vulkan.headers
43 self.protos = vulkan.protos
44
45 def run(self):
46 print(self.generate())
47
Jon Ashburn82974f42015-05-05 09:37:01 -060048 def _requires_special_trampoline_code(self, name):
49 # Dont be cute trying to use a general rule to programmatically populate this list
50 # it just obsfucates what is going on!
Ian Elliott7e40db92015-08-21 15:09:33 -060051 wsi_creates_dispatchable_object = ["CreateSwapchainKHR"]
Chia-I Wu3432a0c2015-10-27 18:04:07 +080052 creates_dispatchable_object = ["CreateDevice", "GetDeviceQueue", "AllocateCommandBuffers"] + wsi_creates_dispatchable_object
Jon Ashburn82974f42015-05-05 09:37:01 -060053 if name in creates_dispatchable_object:
54 return True
55 else:
56 return False
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -060057
Jon Ashburn82974f42015-05-05 09:37:01 -060058 def _is_loader_non_trampoline_entrypoint(self, proto):
Jon Ashburn8d1b0b52015-05-18 13:20:15 -060059 if proto.name in ["GetDeviceProcAddr", "EnumeratePhysicalDevices", "EnumerateLayers", "DbgRegisterMsgCallback", "DbgUnregisterMsgCallback", "DbgSetGlobalOption", "DestroyInstance"]:
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -060060 return True
61 return not self.is_dispatchable_object_first_param(proto)
62
63
64 def is_dispatchable_object_first_param(self, proto):
65 in_objs = proto.object_in_params()
66 non_dispatch_objs = []
67 param0 = proto.params[0]
68 return (len(in_objs) > 0) and (in_objs[0].ty == param0.ty) and (param0.ty not in non_dispatch_objs)
69
70 def generate(self):
71 copyright = self.generate_copyright()
72 header = self.generate_header()
73 body = self.generate_body()
74 footer = self.generate_footer()
75
76 contents = []
77 if copyright:
78 contents.append(copyright)
79 if header:
80 contents.append(header)
81 if body:
82 contents.append(body)
83 if footer:
84 contents.append(footer)
85
86 return "\n\n".join(contents)
87
88 def generate_copyright(self):
89 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
90
91/*
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -060092 *
93 * Copyright (C) 2014 LunarG, Inc.
94 *
95 * Permission is hereby granted, free of charge, to any person obtaining a
96 * copy of this software and associated documentation files (the "Software"),
97 * to deal in the Software without restriction, including without limitation
98 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
99 * and/or sell copies of the Software, and to permit persons to whom the
100 * Software is furnished to do so, subject to the following conditions:
101 *
102 * The above copyright notice and this permission notice shall be included
103 * in all copies or substantial portions of the Software.
104 *
105 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
106 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
107 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
108 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
109 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
110 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
111 * DEALINGS IN THE SOFTWARE.
112 */"""
113
114 def generate_header(self):
115 return "\n".join(["#include <" + h + ">" for h in self.headers])
116
117 def generate_body(self):
118 pass
119
120 def generate_footer(self):
121 pass
122
123class LoaderEntrypointsSubcommand(Subcommand):
124 def generate_header(self):
125 return "#include \"loader.h\""
126
127 def _generate_object_setup(self, proto):
128 method = "loader_init_dispatch"
129 cond = "res == VK_SUCCESS"
Jon Ashburn82974f42015-05-05 09:37:01 -0600130 setup = []
131
132 if not self._requires_special_trampoline_code(proto.name):
133 return setup
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600134
135 if "Get" in proto.name:
136 method = "loader_set_dispatch"
137
Ian Elliott7e40db92015-08-21 15:09:33 -0600138 if proto.name == "GetSwapchainInfoKHR":
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600139 ptype = proto.params[-3].name
140 psize = proto.params[-2].name
141 pdata = proto.params[-1].name
Ian Elliott7e40db92015-08-21 15:09:33 -0600142 cond = ("%s == VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_KHR && "
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600143 "%s && %s" % (ptype, pdata, cond))
Ian Elliott7e40db92015-08-21 15:09:33 -0600144 setup.append("VkSwapchainImageInfoKHR *info = %s;" % pdata)
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600145 setup.append("size_t count = *%s / sizeof(*info), i;" % psize)
146 setup.append("for (i = 0; i < count; i++) {")
147 setup.append(" %s(info[i].image, disp);" % method)
148 setup.append(" %s(info[i].memory, disp);" % method)
149 setup.append("}")
Jon Ashburn82974f42015-05-05 09:37:01 -0600150 else:
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600151 obj_params = proto.object_out_params()
152 for param in obj_params:
153 setup.append("%s(*%s, disp);" % (method, param.name))
154
155 if setup:
156 joined = "\n ".join(setup)
157 setup = []
158 setup.append(" if (%s) {" % cond)
159 setup.append(" " + joined)
160 setup.append(" }")
161
162 return "\n".join(setup)
163
164 def _generate_loader_dispatch_entrypoints(self, qual=""):
165 if qual:
166 qual += " "
167
168 funcs = []
169 for proto in self.protos:
Jon Ashburn82974f42015-05-05 09:37:01 -0600170 if self._is_loader_non_trampoline_entrypoint(proto):
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600171 continue
172 func = []
173
174 obj_setup = self._generate_object_setup(proto)
175
176 func.append(qual + proto.c_func(prefix="vk", attr="VKAPI"))
177 func.append("{")
178
179 # declare local variables
180 func.append(" const VkLayerDispatchTable *disp;")
181 if proto.ret != 'void' and obj_setup:
182 func.append(" VkResult res;")
183 func.append("")
184
185 # get dispatch table
186 func.append(" disp = loader_get_dispatch(%s);" %
187 proto.params[0].name)
188 func.append("")
189
190 # dispatch!
191 dispatch = "disp->%s;" % proto.c_call()
192 if proto.ret == 'void':
193 func.append(" " + dispatch)
194 elif not obj_setup:
195 func.append(" return " + dispatch)
196 else:
197 func.append(" res = " + dispatch)
198 func.append(obj_setup)
199 func.append("")
200 func.append(" return res;")
201
202 func.append("}")
203
204 funcs.append("\n".join(func))
205
206 return "\n\n".join(funcs)
207
208 def generate_body(self):
209 body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")]
210
211 return "\n\n".join(body)
212
213class DispatchTableOpsSubcommand(Subcommand):
214 def run(self):
215 if len(self.argv) != 1:
216 print("DispatchTableOpsSubcommand: <prefix> unspecified")
217 return
218
219 self.prefix = self.argv[0]
220 super().run()
221
222 def generate_header(self):
223 return "\n".join(["#include <vulkan.h>",
224 "#include <vkLayer.h>",
225 "#include <string.h>",
226 "#include \"loader_platform.h\""])
227
Jon Ashburnfbb4e252015-05-04 16:27:53 -0600228 def _generate_init(self, type):
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600229 stmts = []
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600230 func = []
Jon Ashburnfbb4e252015-05-04 16:27:53 -0600231 if type == "device":
232 for proto in self.protos:
233 if self.is_dispatchable_object_first_param(proto) or proto.name == "CreateInstance":
234 stmts.append("table->%s = (PFN_vk%s) gpa(gpu, \"vk%s\");" %
235 (proto.name, proto.name, proto.name))
236 else:
237 stmts.append("table->%s = vk%s; /* non-dispatchable */" %
238 (proto.name, proto.name))
239 func.append("static inline void %s_init_device_dispatch_table(VkLayerDispatchTable *table,"
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600240 % self.prefix)
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600241 func.append("%s PFN_vkGetDeviceProcAddr gpa,"
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600242 % (" " * len(self.prefix)))
Jon Ashburnfbb4e252015-05-04 16:27:53 -0600243 func.append("%s VkPhysicalDevice gpu)"
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600244 % (" " * len(self.prefix)))
Jon Ashburnfbb4e252015-05-04 16:27:53 -0600245 else:
246 for proto in self.protos:
247 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
248 continue
249 stmts.append("table->%s = vk%s;" % (proto.name, proto.name))
250 func.append("static inline void %s_init_instance_dispatch_table(VkLayerInstanceDispatchTable *table)"
251 % self.prefix)
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600252 func.append("{")
253 func.append(" %s" % "\n ".join(stmts))
254 func.append("}")
255
256 return "\n".join(func)
257
258 def _generate_lookup(self):
259 lookups = []
260 for proto in self.protos:
261 if self.is_dispatchable_object_first_param(proto):
262 lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name))
263 lookups.append(" return (void *) table->%s;"
264 % (proto.name))
265
266 func = []
267 func.append("static inline void *%s_lookup_dispatch_table(const VkLayerDispatchTable *table,"
268 % self.prefix)
269 func.append("%s const char *name)"
270 % (" " * len(self.prefix)))
271 func.append("{")
272 func.append(generate_get_proc_addr_check("name"))
273 func.append("")
274 func.append(" name += 2;")
275 func.append(" %s" % "\n ".join(lookups))
276 func.append("")
277 func.append(" return NULL;")
278 func.append("}")
279
280 return "\n".join(func)
281
282 def generate_body(self):
Jon Ashburnfbb4e252015-05-04 16:27:53 -0600283 body = [self._generate_init("device"),
284 self._generate_lookup(),
285 self._generate_init("instance")]
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600286
287 return "\n\n".join(body)
288
289class WinDefFileSubcommand(Subcommand):
290 def run(self):
291 library_exports = {
292 "all": [],
293 }
294
295 if len(self.argv) != 2 or self.argv[1] not in library_exports:
296 print("WinDefFileSubcommand: <library-name> {%s}" %
297 "|".join(library_exports.keys()))
298 return
299
300 self.library = self.argv[0]
301 self.exports = library_exports[self.argv[1]]
302
303 super().run()
304
305 def generate_copyright(self):
306 return """; THIS FILE IS GENERATED. DO NOT EDIT.
307
308;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
309; Vulkan
310;
311; Copyright (C) 2015 LunarG, Inc.
312;
313; Permission is hereby granted, free of charge, to any person obtaining a
314; copy of this software and associated documentation files (the "Software"),
315; to deal in the Software without restriction, including without limitation
316; the rights to use, copy, modify, merge, publish, distribute, sublicense,
317; and/or sell copies of the Software, and to permit persons to whom the
318; Software is furnished to do so, subject to the following conditions:
319;
320; The above copyright notice and this permission notice shall be included
321; in all copies or substantial portions of the Software.
322;
323; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
324; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
325; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
326; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
327; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
328; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
329; DEALINGS IN THE SOFTWARE.
330;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
331
332 def generate_header(self):
333 return "; The following is required on Windows, for exporting symbols from the DLL"
334
335 def generate_body(self):
336 body = []
337
338 body.append("LIBRARY " + self.library)
339 body.append("EXPORTS")
340
341 for proto in self.protos:
342 if self.exports and proto.name not in self.exports:
343 continue
Ian Elliott7e40db92015-08-21 15:09:33 -0600344 if proto.name.endswith("KHR"):
Tony Barbour1d825c72015-06-18 16:29:32 -0600345 continue
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600346 body.append(" vk" + proto.name)
347
348 return "\n".join(body)
349
350class LoaderGetProcAddrSubcommand(Subcommand):
351 def run(self):
352 self.prefix = "vk"
353
354 # we could get the list from argv if wanted
355 self.intercepted = [proto.name for proto in self.protos]
356
357 for proto in self.protos:
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600358 if proto.name == "GetDeviceProcAddr":
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600359 self.gpa = proto
360
361 super().run()
362
363 def generate_header(self):
364 return "\n".join(["#include <string.h>"])
365
366 def generate_body(self):
367 lookups = []
368 for proto in self.protos:
369 if proto.name not in self.intercepted:
370 lookups.append("/* no %s%s */" % (self.prefix, proto.name))
371 continue
372
373 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
374 lookups.append(" return (%s) %s%s;" %
375 (self.gpa.ret, self.prefix, proto.name))
376
377 special_lookups = []
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600378 for proto in self.protos:
Jon Ashburn82974f42015-05-05 09:37:01 -0600379 if self._is_loader_non_trampoline_entrypoint(proto) or self._requires_special_trampoline_code(proto.name):
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600380 special_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
381 special_lookups.append(" return (%s) %s%s;" %
382 (self.gpa.ret, self.prefix, proto.name))
383 else:
384 continue
385 body = []
386 body.append("static inline %s globalGetProcAddr(const char *name)" %
387 self.gpa.ret)
388 body.append("{")
389 body.append(generate_get_proc_addr_check("name"))
390 body.append("")
391 body.append(" name += 2;")
392 body.append(" %s" % "\n ".join(lookups))
393 body.append("")
394 body.append(" return NULL;")
395 body.append("}")
396 body.append("")
397 body.append("static inline void *loader_non_passthrough_gpa(const char *name)")
398 body.append("{")
399 body.append(generate_get_proc_addr_check("name"))
400 body.append("")
401 body.append(" name += 2;")
402 body.append(" %s" % "\n ".join(special_lookups))
403 body.append("")
404 body.append(" return NULL;")
405 body.append("}")
406
407 return "\n".join(body)
408
409def main():
410 subcommands = {
411 "loader-entrypoints": LoaderEntrypointsSubcommand,
412 "dispatch-table-ops": DispatchTableOpsSubcommand,
413 "win-def-file": WinDefFileSubcommand,
414 "loader-get-proc-addr": LoaderGetProcAddrSubcommand,
415 }
416
417 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
418 print("Usage: %s <subcommand> [options]" % sys.argv[0])
419 print
420 print("Available sucommands are: %s" % " ".join(subcommands))
421 exit(1)
422
423 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
424 subcmd.run()
425
426if __name__ == "__main__":
427 main()