Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 1 | #!/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 | |
| 26 | import os, sys |
| 27 | |
| 28 | # add main repo directory so vulkan.py can be imported. This needs to be a complete path. |
| 29 | ld_path = os.path.dirname(os.path.abspath(__file__)) |
| 30 | main_path = os.path.abspath(ld_path + "/../") |
| 31 | sys.path.append(main_path) |
| 32 | |
| 33 | import vulkan |
| 34 | |
| 35 | def generate_get_proc_addr_check(name): |
| 36 | return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \ |
| 37 | " return NULL;" % ((name,) * 3) |
| 38 | |
| 39 | class 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 Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 48 | 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 Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 57 | |
Jon Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 58 | def _is_loader_non_trampoline_entrypoint(self, proto): |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 59 | 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 | |
| 124 | class 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 Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 131 | setup = [] |
| 132 | |
| 133 | if not self._requires_special_trampoline_code(proto.name): |
| 134 | return setup |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 135 | |
| 136 | if "Get" in proto.name: |
| 137 | method = "loader_set_dispatch" |
| 138 | |
Jon Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 139 | if proto.name == "GetPhysicalDeviceInfo": |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 140 | 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 Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 162 | else: |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 163 | 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 Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 182 | if self._is_loader_non_trampoline_entrypoint(proto): |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 183 | 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 | |
| 225 | class 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 | |
| 240 | def _generate_init(self): |
| 241 | stmts = [] |
| 242 | for proto in self.protos: |
| 243 | if self.is_dispatchable_object_first_param(proto) or proto.name == "CreateInstance": |
| 244 | stmts.append("table->%s = (PFN_vk%s) gpa(gpu, \"vk%s\");" % |
| 245 | (proto.name, proto.name, proto.name)) |
| 246 | else: |
| 247 | stmts.append("table->%s = vk%s; /* non-dispatchable */" % |
| 248 | (proto.name, proto.name)) |
| 249 | |
| 250 | func = [] |
| 251 | func.append("static inline void %s_initialize_dispatch_table(VkLayerDispatchTable *table," |
| 252 | % self.prefix) |
| 253 | func.append("%s PFN_vkGetProcAddr gpa," |
| 254 | % (" " * len(self.prefix))) |
| 255 | func.append("%s VkPhysicalDevice gpu)" |
| 256 | % (" " * len(self.prefix))) |
| 257 | func.append("{") |
| 258 | func.append(" %s" % "\n ".join(stmts)) |
| 259 | func.append("}") |
| 260 | |
| 261 | return "\n".join(func) |
| 262 | |
| 263 | def _generate_lookup(self): |
| 264 | lookups = [] |
| 265 | for proto in self.protos: |
| 266 | if self.is_dispatchable_object_first_param(proto): |
| 267 | lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name)) |
| 268 | lookups.append(" return (void *) table->%s;" |
| 269 | % (proto.name)) |
| 270 | |
| 271 | func = [] |
| 272 | func.append("static inline void *%s_lookup_dispatch_table(const VkLayerDispatchTable *table," |
| 273 | % self.prefix) |
| 274 | func.append("%s const char *name)" |
| 275 | % (" " * len(self.prefix))) |
| 276 | func.append("{") |
| 277 | func.append(generate_get_proc_addr_check("name")) |
| 278 | func.append("") |
| 279 | func.append(" name += 2;") |
| 280 | func.append(" %s" % "\n ".join(lookups)) |
| 281 | func.append("") |
| 282 | func.append(" return NULL;") |
| 283 | func.append("}") |
| 284 | |
| 285 | return "\n".join(func) |
| 286 | |
| 287 | def generate_body(self): |
| 288 | body = [self._generate_init(), |
| 289 | self._generate_lookup()] |
| 290 | |
| 291 | return "\n\n".join(body) |
| 292 | |
| 293 | class WinDefFileSubcommand(Subcommand): |
| 294 | def run(self): |
| 295 | library_exports = { |
| 296 | "all": [], |
| 297 | } |
| 298 | |
| 299 | if len(self.argv) != 2 or self.argv[1] not in library_exports: |
| 300 | print("WinDefFileSubcommand: <library-name> {%s}" % |
| 301 | "|".join(library_exports.keys())) |
| 302 | return |
| 303 | |
| 304 | self.library = self.argv[0] |
| 305 | self.exports = library_exports[self.argv[1]] |
| 306 | |
| 307 | super().run() |
| 308 | |
| 309 | def generate_copyright(self): |
| 310 | return """; THIS FILE IS GENERATED. DO NOT EDIT. |
| 311 | |
| 312 | ;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 313 | ; Vulkan |
| 314 | ; |
| 315 | ; Copyright (C) 2015 LunarG, Inc. |
| 316 | ; |
| 317 | ; Permission is hereby granted, free of charge, to any person obtaining a |
| 318 | ; copy of this software and associated documentation files (the "Software"), |
| 319 | ; to deal in the Software without restriction, including without limitation |
| 320 | ; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 321 | ; and/or sell copies of the Software, and to permit persons to whom the |
| 322 | ; Software is furnished to do so, subject to the following conditions: |
| 323 | ; |
| 324 | ; The above copyright notice and this permission notice shall be included |
| 325 | ; in all copies or substantial portions of the Software. |
| 326 | ; |
| 327 | ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 328 | ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 329 | ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 330 | ; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 331 | ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 332 | ; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 333 | ; DEALINGS IN THE SOFTWARE. |
| 334 | ;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;""" |
| 335 | |
| 336 | def generate_header(self): |
| 337 | return "; The following is required on Windows, for exporting symbols from the DLL" |
| 338 | |
| 339 | def generate_body(self): |
| 340 | body = [] |
| 341 | |
| 342 | body.append("LIBRARY " + self.library) |
| 343 | body.append("EXPORTS") |
| 344 | |
| 345 | for proto in self.protos: |
| 346 | if self.exports and proto.name not in self.exports: |
| 347 | continue |
| 348 | body.append(" vk" + proto.name) |
| 349 | |
| 350 | return "\n".join(body) |
| 351 | |
| 352 | class LoaderGetProcAddrSubcommand(Subcommand): |
| 353 | def run(self): |
| 354 | self.prefix = "vk" |
| 355 | |
| 356 | # we could get the list from argv if wanted |
| 357 | self.intercepted = [proto.name for proto in self.protos] |
| 358 | |
| 359 | for proto in self.protos: |
| 360 | if proto.name == "GetProcAddr": |
| 361 | self.gpa = proto |
| 362 | |
| 363 | super().run() |
| 364 | |
| 365 | def generate_header(self): |
| 366 | return "\n".join(["#include <string.h>"]) |
| 367 | |
| 368 | def generate_body(self): |
| 369 | lookups = [] |
| 370 | for proto in self.protos: |
| 371 | if proto.name not in self.intercepted: |
| 372 | lookups.append("/* no %s%s */" % (self.prefix, proto.name)) |
| 373 | continue |
| 374 | |
| 375 | lookups.append("if (!strcmp(name, \"%s\"))" % proto.name) |
| 376 | lookups.append(" return (%s) %s%s;" % |
| 377 | (self.gpa.ret, self.prefix, proto.name)) |
| 378 | |
| 379 | special_lookups = [] |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 380 | for proto in self.protos: |
Jon Ashburn | 9bba6dc | 2015-05-05 09:37:01 -0600 | [diff] [blame^] | 381 | if self._is_loader_non_trampoline_entrypoint(proto) or self._requires_special_trampoline_code(proto.name): |
Jon Ashburn | aef6588 | 2015-05-04 09:16:41 -0600 | [diff] [blame] | 382 | special_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name) |
| 383 | special_lookups.append(" return (%s) %s%s;" % |
| 384 | (self.gpa.ret, self.prefix, proto.name)) |
| 385 | else: |
| 386 | continue |
| 387 | body = [] |
| 388 | body.append("static inline %s globalGetProcAddr(const char *name)" % |
| 389 | self.gpa.ret) |
| 390 | body.append("{") |
| 391 | body.append(generate_get_proc_addr_check("name")) |
| 392 | body.append("") |
| 393 | body.append(" name += 2;") |
| 394 | body.append(" %s" % "\n ".join(lookups)) |
| 395 | body.append("") |
| 396 | body.append(" return NULL;") |
| 397 | body.append("}") |
| 398 | body.append("") |
| 399 | body.append("static inline void *loader_non_passthrough_gpa(const char *name)") |
| 400 | body.append("{") |
| 401 | body.append(generate_get_proc_addr_check("name")) |
| 402 | body.append("") |
| 403 | body.append(" name += 2;") |
| 404 | body.append(" %s" % "\n ".join(special_lookups)) |
| 405 | body.append("") |
| 406 | body.append(" return NULL;") |
| 407 | body.append("}") |
| 408 | |
| 409 | return "\n".join(body) |
| 410 | |
| 411 | def main(): |
| 412 | subcommands = { |
| 413 | "loader-entrypoints": LoaderEntrypointsSubcommand, |
| 414 | "dispatch-table-ops": DispatchTableOpsSubcommand, |
| 415 | "win-def-file": WinDefFileSubcommand, |
| 416 | "loader-get-proc-addr": LoaderGetProcAddrSubcommand, |
| 417 | } |
| 418 | |
| 419 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 420 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 421 | print |
| 422 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 423 | exit(1) |
| 424 | |
| 425 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 426 | subcmd.run() |
| 427 | |
| 428 | if __name__ == "__main__": |
| 429 | main() |