Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 2 | # |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 3 | # VK |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 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 | # Authors: |
| 26 | # Chia-I Wu <olv@lunarg.com> |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 27 | |
| 28 | import sys |
| 29 | |
| 30 | import xgl |
| 31 | |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 32 | def generate_get_proc_addr_check(name): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 33 | return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \ |
| 34 | " return NULL;" % ((name,) * 3) |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 35 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 36 | class Subcommand(object): |
| 37 | def __init__(self, argv): |
| 38 | self.argv = argv |
Chia-I Wu | ec30dcb | 2015-01-01 08:46:31 +0800 | [diff] [blame] | 39 | self.headers = xgl.headers |
| 40 | self.protos = xgl.protos |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 41 | |
| 42 | def run(self): |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 43 | print(self.generate()) |
| 44 | |
| 45 | def generate(self): |
| 46 | copyright = self.generate_copyright() |
| 47 | header = self.generate_header() |
| 48 | body = self.generate_body() |
| 49 | footer = self.generate_footer() |
| 50 | |
| 51 | contents = [] |
| 52 | if copyright: |
| 53 | contents.append(copyright) |
| 54 | if header: |
| 55 | contents.append(header) |
| 56 | if body: |
| 57 | contents.append(body) |
| 58 | if footer: |
| 59 | contents.append(footer) |
| 60 | |
| 61 | return "\n\n".join(contents) |
| 62 | |
| 63 | def generate_copyright(self): |
| 64 | return """/* THIS FILE IS GENERATED. DO NOT EDIT. */ |
| 65 | |
| 66 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 67 | * Vulkan |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 68 | * |
| 69 | * Copyright (C) 2014 LunarG, Inc. |
| 70 | * |
| 71 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 72 | * copy of this software and associated documentation files (the "Software"), |
| 73 | * to deal in the Software without restriction, including without limitation |
| 74 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 75 | * and/or sell copies of the Software, and to permit persons to whom the |
| 76 | * Software is furnished to do so, subject to the following conditions: |
| 77 | * |
| 78 | * The above copyright notice and this permission notice shall be included |
| 79 | * in all copies or substantial portions of the Software. |
| 80 | * |
| 81 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 82 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 83 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 84 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 85 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 86 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 87 | * DEALINGS IN THE SOFTWARE. |
| 88 | */""" |
| 89 | |
| 90 | def generate_header(self): |
Chia-I Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame] | 91 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 92 | |
| 93 | def generate_body(self): |
| 94 | pass |
| 95 | |
| 96 | def generate_footer(self): |
| 97 | pass |
| 98 | |
Chia-I Wu | d4a76ae | 2015-01-04 10:15:48 +0800 | [diff] [blame] | 99 | class LoaderEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 100 | def generate_header(self): |
Chia-I Wu | 1d6731b | 2015-04-11 15:34:07 +0800 | [diff] [blame] | 101 | return "#include \"loader.h\"" |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 102 | |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 103 | def _is_dispatchable(self, proto): |
Courtney Goeltzenleuchter | 9d36ef4 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 104 | if proto.name in ["GetProcAddr", "DestroyInstance", "EnumerateGpus", |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 105 | "EnumerateLayers", "DbgRegisterMsgCallback", "GetExtensionSupport", |
Courtney Goeltzenleuchter | 9d36ef4 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 106 | "DbgUnregisterMsgCallback", "DbgSetGlobalOption"]: |
Chia-I Wu | 31b115a | 2015-01-04 15:38:47 +0800 | [diff] [blame] | 107 | return False |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 108 | |
Chia-I Wu | 31b115a | 2015-01-04 15:38:47 +0800 | [diff] [blame] | 109 | in_objs = proto.object_in_params() |
| 110 | return in_objs and in_objs[0] == proto.params[0] |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 111 | |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 112 | def _generate_object_setup(self, proto): |
| 113 | method = "loader_init_data" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 114 | cond = "res == VK_SUCCESS" |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 115 | |
| 116 | if "Get" in proto.name: |
| 117 | method = "loader_set_data" |
| 118 | |
| 119 | setup = [] |
| 120 | |
| 121 | if proto.name == "AllocDescriptorSets": |
| 122 | psets = proto.params[-2].name |
| 123 | pcount = proto.params[-1].name |
| 124 | setup.append("uint32_t i;") |
| 125 | setup.append("for (i = 0; i < *%s; i++)" % pcount) |
| 126 | setup.append(" %s(%s[i], disp);" % (method, psets)) |
| 127 | else: |
| 128 | obj_params = proto.object_out_params() |
| 129 | for param in obj_params: |
| 130 | setup.append("%s(*%s, disp);" % (method, param.name)) |
| 131 | |
| 132 | if setup: |
| 133 | joined = "\n ".join(setup) |
| 134 | setup = [] |
| 135 | setup.append(" if (%s) {" % cond) |
| 136 | setup.append(" " + joined) |
| 137 | setup.append(" }") |
| 138 | |
| 139 | return "\n".join(setup) |
| 140 | |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 141 | def _generate_loader_dispatch_entrypoints(self, qual=""): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 142 | if qual: |
| 143 | qual += " " |
| 144 | |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 145 | funcs = [] |
| 146 | for proto in self.protos: |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 147 | if not self._is_dispatchable(proto): |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 148 | continue |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 149 | func = [] |
| 150 | |
| 151 | obj_setup = self._generate_object_setup(proto) |
| 152 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 153 | func.append(qual + proto.c_func(prefix="vk", attr="VKAPI")) |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 154 | func.append("{") |
| 155 | |
| 156 | # declare local variables |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 157 | func.append(" const VK_LAYER_DISPATCH_TABLE *disp;") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 158 | if proto.ret != 'void' and obj_setup: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 159 | func.append(" VK_RESULT res;") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 160 | func.append("") |
| 161 | |
| 162 | # active layers before dispatching CreateDevice |
| 163 | if proto.name == "CreateDevice": |
| 164 | func.append(" loader_activate_layers(%s, %s);" % |
| 165 | (proto.params[0].name, proto.params[1].name)) |
| 166 | func.append("") |
| 167 | |
| 168 | # get dispatch table and unwrap GPUs |
| 169 | for param in proto.params: |
| 170 | stmt = "" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 171 | if param.ty == "VK_PHYSICAL_GPU": |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 172 | stmt = "loader_unwrap_gpu(&%s);" % param.name |
| 173 | if param == proto.params[0]: |
| 174 | stmt = "disp = " + stmt |
| 175 | elif param == proto.params[0]: |
| 176 | stmt = "disp = loader_get_data(%s);" % param.name |
| 177 | |
| 178 | if stmt: |
| 179 | func.append(" " + stmt) |
| 180 | func.append("") |
| 181 | |
| 182 | # dispatch! |
| 183 | dispatch = "disp->%s;" % proto.c_call() |
| 184 | if proto.ret == 'void': |
| 185 | func.append(" " + dispatch) |
| 186 | elif not obj_setup: |
| 187 | func.append(" return " + dispatch) |
| 188 | else: |
| 189 | func.append(" res = " + dispatch) |
| 190 | func.append(obj_setup) |
| 191 | func.append("") |
| 192 | func.append(" return res;") |
| 193 | |
| 194 | func.append("}") |
| 195 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 196 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 197 | funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 198 | |
| 199 | funcs.append("\n".join(func)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 200 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 201 | funcs.append("#endif") |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 202 | return "\n\n".join(funcs) |
| 203 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 204 | def generate_body(self): |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 205 | body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")] |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 206 | |
| 207 | return "\n\n".join(body) |
| 208 | |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 209 | class DispatchTableOpsSubcommand(Subcommand): |
| 210 | def run(self): |
| 211 | if len(self.argv) != 1: |
| 212 | print("DispatchTableOpsSubcommand: <prefix> unspecified") |
| 213 | return |
| 214 | |
| 215 | self.prefix = self.argv[0] |
| 216 | super().run() |
| 217 | |
| 218 | def generate_header(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 219 | return "\n".join(["#include <vulkan.h>", |
| 220 | "#include <vkLayer.h>", |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 221 | "#include <string.h>", |
| 222 | "#include \"loader_platform.h\""]) |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 223 | |
| 224 | def _generate_init(self): |
| 225 | stmts = [] |
| 226 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 227 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 228 | stmts.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 229 | if proto.name == "GetProcAddr": |
| 230 | stmts.append("table->%s = gpa; /* direct assignment */" % |
| 231 | proto.name) |
| 232 | else: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 233 | stmts.append("table->%s = (vk%sType) gpa(gpu, \"vk%s\");" % |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 234 | (proto.name, proto.name, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 235 | stmts.append("#endif") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 236 | |
| 237 | func = [] |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 238 | func.append("static inline void %s_initialize_dispatch_table(VK_LAYER_DISPATCH_TABLE *table," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 239 | % self.prefix) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 240 | func.append("%s vkGetProcAddrType gpa," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 241 | % (" " * len(self.prefix))) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 242 | func.append("%s VK_PHYSICAL_GPU gpu)" |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 243 | % (" " * len(self.prefix))) |
| 244 | func.append("{") |
| 245 | func.append(" %s" % "\n ".join(stmts)) |
| 246 | func.append("}") |
| 247 | |
| 248 | return "\n".join(func) |
| 249 | |
| 250 | def _generate_lookup(self): |
| 251 | lookups = [] |
| 252 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 253 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 254 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 255 | lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name)) |
| 256 | lookups.append(" return (void *) table->%s;" |
| 257 | % (proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 258 | lookups.append("#endif") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 259 | |
| 260 | func = [] |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 261 | func.append("static inline void *%s_lookup_dispatch_table(const VK_LAYER_DISPATCH_TABLE *table," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 262 | % self.prefix) |
| 263 | func.append("%s const char *name)" |
| 264 | % (" " * len(self.prefix))) |
| 265 | func.append("{") |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 266 | func.append(generate_get_proc_addr_check("name")) |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 267 | func.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 268 | func.append(" name += 2;") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 269 | func.append(" %s" % "\n ".join(lookups)) |
| 270 | func.append("") |
| 271 | func.append(" return NULL;") |
| 272 | func.append("}") |
| 273 | |
| 274 | return "\n".join(func) |
| 275 | |
| 276 | def generate_body(self): |
| 277 | body = [self._generate_init(), |
| 278 | self._generate_lookup()] |
| 279 | |
| 280 | return "\n\n".join(body) |
| 281 | |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 282 | class IcdDummyEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 283 | def run(self): |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 284 | if len(self.argv) == 1: |
| 285 | self.prefix = self.argv[0] |
| 286 | self.qual = "static" |
| 287 | else: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 288 | self.prefix = "vk" |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 289 | self.qual = "ICD_EXPORT" |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 290 | |
| 291 | super().run() |
| 292 | |
| 293 | def generate_header(self): |
| 294 | return "#include \"icd.h\"" |
| 295 | |
| 296 | def _generate_stub_decl(self, proto): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 297 | return proto.c_pretty_decl(self.prefix + proto.name, attr="VKAPI") |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 298 | |
| 299 | def _generate_stubs(self): |
| 300 | stubs = [] |
| 301 | for proto in self.protos: |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 302 | decl = self._generate_stub_decl(proto) |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 303 | if proto.ret != "void": |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 304 | stmt = " return VK_ERROR_UNKNOWN;\n" |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 305 | else: |
| 306 | stmt = "" |
| 307 | |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 308 | stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt)) |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 309 | |
| 310 | return "\n\n".join(stubs) |
| 311 | |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 312 | def generate_body(self): |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 313 | return self._generate_stubs() |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 314 | |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 315 | class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand): |
| 316 | def generate_header(self): |
| 317 | return "\n".join(["#include <string.h>", "#include \"icd.h\""]) |
| 318 | |
| 319 | def generate_body(self): |
| 320 | for proto in self.protos: |
| 321 | if proto.name == "GetProcAddr": |
| 322 | gpa_proto = proto |
| 323 | |
| 324 | gpa_decl = self._generate_stub_decl(gpa_proto) |
| 325 | gpa_pname = gpa_proto.params[-1].name |
| 326 | |
| 327 | lookups = [] |
| 328 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 329 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 330 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 331 | lookups.append("if (!strcmp(%s, \"%s\"))" % |
| 332 | (gpa_pname, proto.name)) |
| 333 | lookups.append(" return (%s) %s%s;" % |
| 334 | (gpa_proto.ret, self.prefix, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 335 | lookups.append("#endif") |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 336 | |
| 337 | body = [] |
| 338 | body.append("%s %s" % (self.qual, gpa_decl)) |
| 339 | body.append("{") |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 340 | body.append(generate_get_proc_addr_check(gpa_pname)) |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 341 | body.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 342 | body.append(" %s += 2;" % gpa_pname) |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 343 | body.append(" %s" % "\n ".join(lookups)) |
| 344 | body.append("") |
| 345 | body.append(" return NULL;") |
| 346 | body.append("}") |
| 347 | |
| 348 | return "\n".join(body) |
| 349 | |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 350 | class LayerInterceptProcSubcommand(Subcommand): |
| 351 | def run(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 352 | self.prefix = "vk" |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 353 | |
| 354 | # we could get the list from argv if wanted |
| 355 | self.intercepted = [proto.name for proto in self.protos |
Jon Ashburn | 457c5d1 | 2015-01-29 16:54:38 -0700 | [diff] [blame] | 356 | if proto.name not in ["EnumerateGpus"]] |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 357 | |
| 358 | for proto in self.protos: |
| 359 | if proto.name == "GetProcAddr": |
| 360 | self.gpa = proto |
| 361 | |
| 362 | super().run() |
| 363 | |
| 364 | def generate_header(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 365 | return "\n".join(["#include <string.h>", "#include \"vkLayer.h\""]) |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 366 | |
| 367 | def generate_body(self): |
| 368 | lookups = [] |
| 369 | for proto in self.protos: |
| 370 | if proto.name not in self.intercepted: |
| 371 | lookups.append("/* no %s%s */" % (self.prefix, proto.name)) |
| 372 | continue |
| 373 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 374 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 375 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 376 | lookups.append("if (!strcmp(name, \"%s\"))" % proto.name) |
| 377 | lookups.append(" return (%s) %s%s;" % |
| 378 | (self.gpa.ret, self.prefix, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 379 | lookups.append("#endif") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 380 | |
| 381 | body = [] |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 382 | body.append("static inline %s layer_intercept_proc(const char *name)" % |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 383 | self.gpa.ret) |
| 384 | body.append("{") |
| 385 | body.append(generate_get_proc_addr_check("name")) |
| 386 | body.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 387 | body.append(" name += 2;") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 388 | body.append(" %s" % "\n ".join(lookups)) |
| 389 | body.append("") |
| 390 | body.append(" return NULL;") |
| 391 | body.append("}") |
| 392 | |
| 393 | return "\n".join(body) |
| 394 | |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 395 | class WinDefFileSubcommand(Subcommand): |
| 396 | def run(self): |
| 397 | library_exports = { |
| 398 | "all": [], |
| 399 | "icd": [ |
| 400 | "EnumerateGpus", |
| 401 | "CreateInstance", |
| 402 | "DestroyInstance", |
| 403 | "GetProcAddr", |
| 404 | ], |
| 405 | "layer": [ |
| 406 | "GetProcAddr", |
| 407 | "EnumerateLayers", |
| 408 | ], |
| 409 | } |
| 410 | |
| 411 | if len(self.argv) != 2 or self.argv[1] not in library_exports: |
| 412 | print("WinDefFileSubcommand: <library-name> {%s}" % |
| 413 | "|".join(library_exports.keys())) |
| 414 | return |
| 415 | |
| 416 | self.library = self.argv[0] |
| 417 | self.exports = library_exports[self.argv[1]] |
| 418 | |
| 419 | super().run() |
| 420 | |
| 421 | def generate_copyright(self): |
| 422 | return """; THIS FILE IS GENERATED. DO NOT EDIT. |
| 423 | |
| 424 | ;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 425 | ; Vulkan |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 426 | ; |
| 427 | ; Copyright (C) 2015 LunarG, Inc. |
| 428 | ; |
| 429 | ; Permission is hereby granted, free of charge, to any person obtaining a |
| 430 | ; copy of this software and associated documentation files (the "Software"), |
| 431 | ; to deal in the Software without restriction, including without limitation |
| 432 | ; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 433 | ; and/or sell copies of the Software, and to permit persons to whom the |
| 434 | ; Software is furnished to do so, subject to the following conditions: |
| 435 | ; |
| 436 | ; The above copyright notice and this permission notice shall be included |
| 437 | ; in all copies or substantial portions of the Software. |
| 438 | ; |
| 439 | ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 440 | ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 441 | ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 442 | ; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 443 | ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 444 | ; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 445 | ; DEALINGS IN THE SOFTWARE. |
| 446 | ;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;""" |
| 447 | |
| 448 | def generate_header(self): |
| 449 | return "; The following is required on Windows, for exporting symbols from the DLL" |
| 450 | |
| 451 | def generate_body(self): |
| 452 | body = [] |
| 453 | |
| 454 | body.append("LIBRARY " + self.library) |
| 455 | body.append("EXPORTS") |
| 456 | |
| 457 | for proto in self.protos: |
| 458 | if self.exports and proto.name not in self.exports: |
| 459 | continue |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 460 | body.append(" vk" + proto.name) |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 461 | |
| 462 | return "\n".join(body) |
| 463 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 464 | def main(): |
| 465 | subcommands = { |
Chia-I Wu | d4a76ae | 2015-01-04 10:15:48 +0800 | [diff] [blame] | 466 | "loader-entrypoints": LoaderEntrypointsSubcommand, |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 467 | "dispatch-table-ops": DispatchTableOpsSubcommand, |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 468 | "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand, |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 469 | "icd-get-proc-addr": IcdGetProcAddrSubcommand, |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 470 | "layer-intercept-proc": LayerInterceptProcSubcommand, |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 471 | "win-def-file": WinDefFileSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 475 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 476 | print |
| 477 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 478 | exit(1) |
| 479 | |
| 480 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 481 | subcmd.run() |
| 482 | |
| 483 | if __name__ == "__main__": |
| 484 | main() |