blob: 1d55d5611bfbb586f906158ab5965c9cbc9fc152 [file] [log] [blame]
Jon Ashburnaef65882015-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 Ashburn9bba6dc2015-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!
51 wsi_creates_dispatchable_object = ["GetPhysicalDeviceInfo", "CreateSwapChainWSI"]
52 creates_dispatchable_object = ["CreateDevice", "GetDeviceQueue", "CreateCommandBuffer"] + wsi_creates_dispatchable_object
53 if name in creates_dispatchable_object:
54 return True
55 else:
56 return False
Jon Ashburnaef65882015-05-04 09:16:41 -060057
Jon Ashburn9bba6dc2015-05-05 09:37:01 -060058 def _is_loader_non_trampoline_entrypoint(self, proto):
Jon Ashburnaef65882015-05-04 09:16:41 -060059 if proto.name in ["GetProcAddr", "EnumeratePhysicalDevices", "EnumerateLayers", "DbgRegisterMsgCallback", "DbgUnregisterMsgCallback", "DbgSetGlobalOption", "DestroyInstance"]:
60 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/*
92 * Vulkan
93 *
94 * Copyright (C) 2014 LunarG, Inc.
95 *
96 * Permission is hereby granted, free of charge, to any person obtaining a
97 * copy of this software and associated documentation files (the "Software"),
98 * to deal in the Software without restriction, including without limitation
99 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
100 * and/or sell copies of the Software, and to permit persons to whom the
101 * Software is furnished to do so, subject to the following conditions:
102 *
103 * The above copyright notice and this permission notice shall be included
104 * in all copies or substantial portions of the Software.
105 *
106 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
107 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
108 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
109 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
110 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
111 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
112 * DEALINGS IN THE SOFTWARE.
113 */"""
114
115 def generate_header(self):
116 return "\n".join(["#include <" + h + ">" for h in self.headers])
117
118 def generate_body(self):
119 pass
120
121 def generate_footer(self):
122 pass
123
124class LoaderEntrypointsSubcommand(Subcommand):
125 def generate_header(self):
126 return "#include \"loader.h\""
127
128 def _generate_object_setup(self, proto):
129 method = "loader_init_dispatch"
130 cond = "res == VK_SUCCESS"
Jon Ashburn9bba6dc2015-05-05 09:37:01 -0600131 setup = []
132
133 if not self._requires_special_trampoline_code(proto.name):
134 return setup
Jon Ashburnaef65882015-05-04 09:16:41 -0600135
136 if "Get" in proto.name:
137 method = "loader_set_dispatch"
138
Jon Ashburn9bba6dc2015-05-05 09:37:01 -0600139 if proto.name == "GetPhysicalDeviceInfo":
Jon Ashburnaef65882015-05-04 09:16:41 -0600140 ptype = proto.params[-3].name
141 psize = proto.params[-2].name
142 pdata = proto.params[-1].name
143 cond = ("%s == VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI && "
144 "%s && %s" % (ptype, pdata, cond))
145 setup.append("VkDisplayPropertiesWSI *info = %s;" % pdata)
146 setup.append("size_t count = *%s / sizeof(*info), i;" % psize)
147 setup.append("for (i = 0; i < count; i++) {")
148 setup.append(" %s(info[i].display, disp);" % method)
149 setup.append("}")
150 elif proto.name == "GetSwapChainInfoWSI":
151 ptype = proto.params[-3].name
152 psize = proto.params[-2].name
153 pdata = proto.params[-1].name
154 cond = ("%s == VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI && "
155 "%s && %s" % (ptype, pdata, cond))
156 setup.append("VkSwapChainImageInfoWSI *info = %s;" % pdata)
157 setup.append("size_t count = *%s / sizeof(*info), i;" % psize)
158 setup.append("for (i = 0; i < count; i++) {")
159 setup.append(" %s(info[i].image, disp);" % method)
160 setup.append(" %s(info[i].memory, disp);" % method)
161 setup.append("}")
Jon Ashburn9bba6dc2015-05-05 09:37:01 -0600162 else:
Jon Ashburnaef65882015-05-04 09:16:41 -0600163 obj_params = proto.object_out_params()
164 for param in obj_params:
165 setup.append("%s(*%s, disp);" % (method, param.name))
166
167 if setup:
168 joined = "\n ".join(setup)
169 setup = []
170 setup.append(" if (%s) {" % cond)
171 setup.append(" " + joined)
172 setup.append(" }")
173
174 return "\n".join(setup)
175
176 def _generate_loader_dispatch_entrypoints(self, qual=""):
177 if qual:
178 qual += " "
179
180 funcs = []
181 for proto in self.protos:
Jon Ashburn9bba6dc2015-05-05 09:37:01 -0600182 if self._is_loader_non_trampoline_entrypoint(proto):
Jon Ashburnaef65882015-05-04 09:16:41 -0600183 continue
184 func = []
185
186 obj_setup = self._generate_object_setup(proto)
187
188 func.append(qual + proto.c_func(prefix="vk", attr="VKAPI"))
189 func.append("{")
190
191 # declare local variables
192 func.append(" const VkLayerDispatchTable *disp;")
193 if proto.ret != 'void' and obj_setup:
194 func.append(" VkResult res;")
195 func.append("")
196
197 # get dispatch table
198 func.append(" disp = loader_get_dispatch(%s);" %
199 proto.params[0].name)
200 func.append("")
201
202 # dispatch!
203 dispatch = "disp->%s;" % proto.c_call()
204 if proto.ret == 'void':
205 func.append(" " + dispatch)
206 elif not obj_setup:
207 func.append(" return " + dispatch)
208 else:
209 func.append(" res = " + dispatch)
210 func.append(obj_setup)
211 func.append("")
212 func.append(" return res;")
213
214 func.append("}")
215
216 funcs.append("\n".join(func))
217
218 return "\n\n".join(funcs)
219
220 def generate_body(self):
221 body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")]
222
223 return "\n\n".join(body)
224
225class DispatchTableOpsSubcommand(Subcommand):
226 def run(self):
227 if len(self.argv) != 1:
228 print("DispatchTableOpsSubcommand: <prefix> unspecified")
229 return
230
231 self.prefix = self.argv[0]
232 super().run()
233
234 def generate_header(self):
235 return "\n".join(["#include <vulkan.h>",
236 "#include <vkLayer.h>",
237 "#include <string.h>",
238 "#include \"loader_platform.h\""])
239
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600240 def _generate_init(self, type):
Jon Ashburnaef65882015-05-04 09:16:41 -0600241 stmts = []
Jon Ashburnaef65882015-05-04 09:16:41 -0600242 func = []
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600243 if type == "device":
244 for proto in self.protos:
245 if self.is_dispatchable_object_first_param(proto) or proto.name == "CreateInstance":
246 stmts.append("table->%s = (PFN_vk%s) gpa(gpu, \"vk%s\");" %
247 (proto.name, proto.name, proto.name))
248 else:
249 stmts.append("table->%s = vk%s; /* non-dispatchable */" %
250 (proto.name, proto.name))
251 func.append("static inline void %s_init_device_dispatch_table(VkLayerDispatchTable *table,"
Jon Ashburnaef65882015-05-04 09:16:41 -0600252 % self.prefix)
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600253 func.append("%s PFN_vkGetProcAddr gpa,"
Jon Ashburnaef65882015-05-04 09:16:41 -0600254 % (" " * len(self.prefix)))
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600255 func.append("%s VkPhysicalDevice gpu)"
Jon Ashburnaef65882015-05-04 09:16:41 -0600256 % (" " * len(self.prefix)))
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600257 else:
258 for proto in self.protos:
259 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
260 continue
261 stmts.append("table->%s = vk%s;" % (proto.name, proto.name))
262 func.append("static inline void %s_init_instance_dispatch_table(VkLayerInstanceDispatchTable *table)"
263 % self.prefix)
Jon Ashburnaef65882015-05-04 09:16:41 -0600264 func.append("{")
265 func.append(" %s" % "\n ".join(stmts))
266 func.append("}")
267
268 return "\n".join(func)
269
270 def _generate_lookup(self):
271 lookups = []
272 for proto in self.protos:
273 if self.is_dispatchable_object_first_param(proto):
274 lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name))
275 lookups.append(" return (void *) table->%s;"
276 % (proto.name))
277
278 func = []
279 func.append("static inline void *%s_lookup_dispatch_table(const VkLayerDispatchTable *table,"
280 % self.prefix)
281 func.append("%s const char *name)"
282 % (" " * len(self.prefix)))
283 func.append("{")
284 func.append(generate_get_proc_addr_check("name"))
285 func.append("")
286 func.append(" name += 2;")
287 func.append(" %s" % "\n ".join(lookups))
288 func.append("")
289 func.append(" return NULL;")
290 func.append("}")
291
292 return "\n".join(func)
293
294 def generate_body(self):
Jon Ashburn9a9bb642015-05-04 16:27:53 -0600295 body = [self._generate_init("device"),
296 self._generate_lookup(),
297 self._generate_init("instance")]
Jon Ashburnaef65882015-05-04 09:16:41 -0600298
299 return "\n\n".join(body)
300
301class WinDefFileSubcommand(Subcommand):
302 def run(self):
303 library_exports = {
304 "all": [],
305 }
306
307 if len(self.argv) != 2 or self.argv[1] not in library_exports:
308 print("WinDefFileSubcommand: <library-name> {%s}" %
309 "|".join(library_exports.keys()))
310 return
311
312 self.library = self.argv[0]
313 self.exports = library_exports[self.argv[1]]
314
315 super().run()
316
317 def generate_copyright(self):
318 return """; THIS FILE IS GENERATED. DO NOT EDIT.
319
320;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321; Vulkan
322;
323; Copyright (C) 2015 LunarG, Inc.
324;
325; Permission is hereby granted, free of charge, to any person obtaining a
326; copy of this software and associated documentation files (the "Software"),
327; to deal in the Software without restriction, including without limitation
328; the rights to use, copy, modify, merge, publish, distribute, sublicense,
329; and/or sell copies of the Software, and to permit persons to whom the
330; Software is furnished to do so, subject to the following conditions:
331;
332; The above copyright notice and this permission notice shall be included
333; in all copies or substantial portions of the Software.
334;
335; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
336; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
337; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
338; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
339; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
340; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
341; DEALINGS IN THE SOFTWARE.
342;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
343
344 def generate_header(self):
345 return "; The following is required on Windows, for exporting symbols from the DLL"
346
347 def generate_body(self):
348 body = []
349
350 body.append("LIBRARY " + self.library)
351 body.append("EXPORTS")
352
353 for proto in self.protos:
354 if self.exports and proto.name not in self.exports:
355 continue
356 body.append(" vk" + proto.name)
357
358 return "\n".join(body)
359
360class LoaderGetProcAddrSubcommand(Subcommand):
361 def run(self):
362 self.prefix = "vk"
363
364 # we could get the list from argv if wanted
365 self.intercepted = [proto.name for proto in self.protos]
366
367 for proto in self.protos:
368 if proto.name == "GetProcAddr":
369 self.gpa = proto
370
371 super().run()
372
373 def generate_header(self):
374 return "\n".join(["#include <string.h>"])
375
376 def generate_body(self):
377 lookups = []
378 for proto in self.protos:
379 if proto.name not in self.intercepted:
380 lookups.append("/* no %s%s */" % (self.prefix, proto.name))
381 continue
382
383 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
384 lookups.append(" return (%s) %s%s;" %
385 (self.gpa.ret, self.prefix, proto.name))
386
387 special_lookups = []
Jon Ashburnaef65882015-05-04 09:16:41 -0600388 for proto in self.protos:
Jon Ashburn9bba6dc2015-05-05 09:37:01 -0600389 if self._is_loader_non_trampoline_entrypoint(proto) or self._requires_special_trampoline_code(proto.name):
Jon Ashburnaef65882015-05-04 09:16:41 -0600390 special_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
391 special_lookups.append(" return (%s) %s%s;" %
392 (self.gpa.ret, self.prefix, proto.name))
393 else:
394 continue
395 body = []
396 body.append("static inline %s globalGetProcAddr(const char *name)" %
397 self.gpa.ret)
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(lookups))
403 body.append("")
404 body.append(" return NULL;")
405 body.append("}")
406 body.append("")
407 body.append("static inline void *loader_non_passthrough_gpa(const char *name)")
408 body.append("{")
409 body.append(generate_get_proc_addr_check("name"))
410 body.append("")
411 body.append(" name += 2;")
412 body.append(" %s" % "\n ".join(special_lookups))
413 body.append("")
414 body.append(" return NULL;")
415 body.append("}")
416
417 return "\n".join(body)
418
419def main():
420 subcommands = {
421 "loader-entrypoints": LoaderEntrypointsSubcommand,
422 "dispatch-table-ops": DispatchTableOpsSubcommand,
423 "win-def-file": WinDefFileSubcommand,
424 "loader-get-proc-addr": LoaderGetProcAddrSubcommand,
425 }
426
427 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
428 print("Usage: %s <subcommand> [options]" % sys.argv[0])
429 print
430 print("Available sucommands are: %s" % " ".join(subcommands))
431 exit(1)
432
433 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
434 subcmd.run()
435
436if __name__ == "__main__":
437 main()