blob: ac4b49a22d59e8fcd0885723d2426c44b5cf1970 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu44e42362014-09-02 08:32:09 +08002#
Karl Schultzc85a02f2016-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 Wu44e42362014-09-02 08:32:09 +08007#
Karl Schultzc85a02f2016-02-02 19:32:33 -07008# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and/or associated documentation files (the "Materials"), to
10# deal in the Materials without restriction, including without limitation the
11# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12# sell copies of the Materials, and to permit persons to whom the Materials
13# are furnished to do so, subject to the following conditions:
Chia-I Wu44e42362014-09-02 08:32:09 +080014#
Karl Schultzc85a02f2016-02-02 19:32:33 -070015# The above copyright notice(s) and this permission notice shall be included
16# in all copies or substantial portions of the Materials.
Chia-I Wu44e42362014-09-02 08:32:09 +080017#
Karl Schultzc85a02f2016-02-02 19:32:33 -070018# The Materials are Confidential Information as defined by the Khronos
19# Membership Agreement until designated non-confidential by Khronos, at which
20# point this condition clause shall be removed.
21#
22# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wu44e42362014-09-02 08:32:09 +080023# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -070024# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25#
26# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
28# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
29# USE OR OTHER DEALINGS IN THE MATERIALS
Chia-I Wu44e42362014-09-02 08:32:09 +080030#
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060031# Author: Chia-I Wu <olv@lunarg.com>
32# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
33# Author: Jon Ashburn <jon@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080034
35import sys
36
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060037import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080038
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080039def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060040 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
41 " return NULL;" % ((name,) * 3)
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080042
Chia-I Wufb2559d2014-08-01 11:19:52 +080043class Subcommand(object):
44 def __init__(self, argv):
45 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060046 self.headers = vulkan.headers
47 self.protos = vulkan.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080048
49 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080050 print(self.generate())
51
52 def generate(self):
53 copyright = self.generate_copyright()
54 header = self.generate_header()
55 body = self.generate_body()
56 footer = self.generate_footer()
57
58 contents = []
59 if copyright:
60 contents.append(copyright)
61 if header:
62 contents.append(header)
63 if body:
64 contents.append(body)
65 if footer:
66 contents.append(footer)
67
68 return "\n\n".join(contents)
69
70 def generate_copyright(self):
71 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
72
73/*
Karl Schultzc85a02f2016-02-02 19:32:33 -070074 * Copyright (c) 2015-2016 The Khronos Group Inc.
75 * Copyright (c) 2015-2016 Valve Corporation
76 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080077 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070078 * Permission is hereby granted, free of charge, to any person obtaining a copy
79 * of this software and/or associated documentation files (the "Materials"), to
80 * deal in the Materials without restriction, including without limitation the
81 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
82 * sell copies of the Materials, and to permit persons to whom the Materials are
83 * furnished to do so, subject to the following conditions:
Chia-I Wufb2559d2014-08-01 11:19:52 +080084 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070085 * The above copyright notice(s) and this permission notice shall be included in
86 * all copies or substantial portions of the Materials.
Chia-I Wufb2559d2014-08-01 11:19:52 +080087 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070088 * The Materials are Confidential Information as defined by the Khronos
89 * Membership Agreement until designated non-confidential by Khronos, at which
90 * point this condition clause shall be removed.
Chia-I Wufb2559d2014-08-01 11:19:52 +080091 *
Karl Schultzc85a02f2016-02-02 19:32:33 -070092 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wufb2559d2014-08-01 11:19:52 +080093 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -070094 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
95 *
96 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
97 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
98 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
99 * USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -0600100 *
101 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +0800102 */"""
103
104 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +0800105 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800106
107 def generate_body(self):
108 pass
109
110 def generate_footer(self):
111 pass
112
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800113class DispatchTableOpsSubcommand(Subcommand):
114 def run(self):
115 if len(self.argv) != 1:
116 print("DispatchTableOpsSubcommand: <prefix> unspecified")
117 return
118
119 self.prefix = self.argv[0]
Michael Lentine92689442015-09-09 12:39:13 -0700120 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800121
122 def generate_header(self):
David Pinedo329ca9e2015-11-06 12:54:48 -0700123 return "\n".join(["#include <vulkan/vulkan.h>",
124 "#include <vulkan/vk_layer.h>",
Jon Ashburnaef65882015-05-04 09:16:41 -0600125 "#include <string.h>"])
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800126
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600127 def _generate_init_dispatch(self, type):
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800128 stmts = []
Jon Ashburnd9564002015-05-07 10:27:37 -0600129 func = []
130 if type == "device":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600131 # GPA has to be first one and uses wrapped object
Courtney Goeltzenleuchterf83cd4a2015-06-26 15:05:29 -0600132 stmts.append("memset(table, 0, sizeof(*table));")
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600133 stmts.append("table->GetDeviceProcAddr =(PFN_vkGetDeviceProcAddr) gpa(device,\"vkGetDeviceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600134 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700135 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice":
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600136 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600137 if proto.name != "GetDeviceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700138 stmts.append("table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800139 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700140 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800141 % self.prefix)
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700142 func.append("%s VkLayerDispatchTable *table,"
143 % (" " * len(self.prefix)))
144 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburnd9564002015-05-07 10:27:37 -0600145 % (" " * len(self.prefix)))
146 else:
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600147 stmts.append("table->GetInstanceProcAddr =(PFN_vkGetInstanceProcAddr) gpa(instance,\"vkGetInstanceProcAddr\");")
Jon Ashburnd9564002015-05-07 10:27:37 -0600148 for proto in self.protos:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700149 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
Jon Ashburnd9564002015-05-07 10:27:37 -0600150 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600151 if proto.name == "CreateDevice":
152 continue
Jon Ashburn83334db2015-09-16 18:08:32 -0600153 if proto.name != "GetInstanceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700154 stmts.append("table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
Jon Ashburnd9564002015-05-07 10:27:37 -0600155 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700156 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
157 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
158 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
159 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800160 func.append("{")
161 func.append(" %s" % "\n ".join(stmts))
162 func.append("}")
163
164 return "\n".join(func)
165
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800166 def generate_body(self):
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600167 body = [self._generate_init_dispatch("device"),
168 self._generate_init_dispatch("instance")]
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800169
170 return "\n\n".join(body)
171
Chia-I Wud98a23c2015-04-11 10:56:50 +0800172class WinDefFileSubcommand(Subcommand):
173 def run(self):
174 library_exports = {
175 "all": [],
176 "icd": [
Jon Ashburn89a1c462016-01-07 09:46:26 -0700177 "vk_icdGetInstanceProcAddr",
Chia-I Wud98a23c2015-04-11 10:56:50 +0800178 ],
179 "layer": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700180 "vkGetInstanceProcAddr",
181 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600182 "vkEnumerateInstanceLayerProperties",
183 "vkEnumerateInstanceExtensionProperties"
Chia-I Wud98a23c2015-04-11 10:56:50 +0800184 ],
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700185 "layer_multi": [
Jon Ashburn88049b12015-08-13 14:15:07 -0700186 "multi2GetInstanceProcAddr",
187 "multi1GetDeviceProcAddr"
188 ]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800189 }
190
191 if len(self.argv) != 2 or self.argv[1] not in library_exports:
192 print("WinDefFileSubcommand: <library-name> {%s}" %
193 "|".join(library_exports.keys()))
194 return
195
196 self.library = self.argv[0]
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700197 if self.library == "VkLayer_multi":
198 self.exports = library_exports["layer_multi"]
Jon Ashburn88049b12015-08-13 14:15:07 -0700199 else:
200 self.exports = library_exports[self.argv[1]]
Chia-I Wud98a23c2015-04-11 10:56:50 +0800201
202 super().run()
203
204 def generate_copyright(self):
205 return """; THIS FILE IS GENERATED. DO NOT EDIT.
206
207;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600208; Vulkan
Chia-I Wud98a23c2015-04-11 10:56:50 +0800209;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700210; Copyright (c) 2015-2016 The Khronos Group Inc.
211; Copyright (c) 2015-2016 Valve Corporation
212; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wud98a23c2015-04-11 10:56:50 +0800213;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700214; Permission is hereby granted, free of charge, to any person obtaining a copy
215; of this software and/or associated documentation files (the "Materials"), to
216; deal in the Materials without restriction, including without limitation the
217; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
218; sell copies of the Materials, and to permit persons to whom the Materials are
219; furnished to do so, subject to the following conditions:
Chia-I Wud98a23c2015-04-11 10:56:50 +0800220;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700221; The above copyright notice(s) and this permission notice shall be included in
222; all copies or substantial portions of the Materials.
Chia-I Wud98a23c2015-04-11 10:56:50 +0800223;
Karl Schultzc85a02f2016-02-02 19:32:33 -0700224; The Materials are Confidential Information as defined by the Khronos
225; Membership Agreement until designated non-confidential by Khronos, at which
226; point this condition clause shall be removed.
227;
228; THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wud98a23c2015-04-11 10:56:50 +0800229; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultzc85a02f2016-02-02 19:32:33 -0700230; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
231;
232; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
233; DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
234; OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
235; USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchtercf8ff4f2015-12-16 14:57:27 -0700236;
237; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wud98a23c2015-04-11 10:56:50 +0800238;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
239
240 def generate_header(self):
241 return "; The following is required on Windows, for exporting symbols from the DLL"
242
243 def generate_body(self):
244 body = []
245
246 body.append("LIBRARY " + self.library)
247 body.append("EXPORTS")
248
Jon Ashburn88049b12015-08-13 14:15:07 -0700249 for proto in self.exports:
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700250 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott329da012015-09-22 10:51:24 -0600251 body.append( proto)
Chia-I Wud98a23c2015-04-11 10:56:50 +0800252
253 return "\n".join(body)
254
Chia-I Wufb2559d2014-08-01 11:19:52 +0800255def main():
256 subcommands = {
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800257 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wud98a23c2015-04-11 10:56:50 +0800258 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800259 }
260
261 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
262 print("Usage: %s <subcommand> [options]" % sys.argv[0])
263 print
264 print("Available sucommands are: %s" % " ".join(subcommands))
265 exit(1)
266
267 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
268 subcmd.run()
269
270if __name__ == "__main__":
271 main()