blob: 6fafc19efb1c0244e568f3f331581f3a43130198 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu701f3f62014-09-02 08:32:09 +08002#
Karl Schultz8e42f402016-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 Wu701f3f62014-09-02 08:32:09 +08007#
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
Chia-I Wu701f3f62014-09-02 08:32:09 +080011#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012# http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu701f3f62014-09-02 08:32:09 +080013#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
Chia-I Wu701f3f62014-09-02 08:32:09 +080019#
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060020# Author: Chia-I Wu <olv@lunarg.com>
21# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
22# Author: Jon Ashburn <jon@lunarg.com>
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +090023# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080024
25import sys
26
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060027import vulkan
Chia-I Wufb2559d2014-08-01 11:19:52 +080028
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080029def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060030 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
31 " return NULL;" % ((name,) * 3)
Chia-I Wu6cdaedb2015-01-05 12:55:13 +080032
Chia-I Wufb2559d2014-08-01 11:19:52 +080033class Subcommand(object):
34 def __init__(self, argv):
35 self.argv = argv
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060036 self.headers = vulkan.headers
37 self.protos = vulkan.protos
Jamie Madilldbda66b2016-05-10 07:36:20 -070038 self.outfile = None
Chia-I Wufb2559d2014-08-01 11:19:52 +080039
40 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -070041 if self.outfile:
42 with open(self.outfile, "w") as outfile:
43 outfile.write(self.generate())
44 else:
45 print(self.generate())
Chia-I Wufb2559d2014-08-01 11:19:52 +080046
47 def generate(self):
48 copyright = self.generate_copyright()
49 header = self.generate_header()
50 body = self.generate_body()
51 footer = self.generate_footer()
52
53 contents = []
54 if copyright:
55 contents.append(copyright)
56 if header:
57 contents.append(header)
58 if body:
59 contents.append(body)
60 if footer:
61 contents.append(footer)
62
63 return "\n\n".join(contents)
64
65 def generate_copyright(self):
66 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
67
68/*
Karl Schultz8e42f402016-02-02 19:32:33 -070069 * Copyright (c) 2015-2016 The Khronos Group Inc.
70 * Copyright (c) 2015-2016 Valve Corporation
71 * Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wufb2559d2014-08-01 11:19:52 +080072 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060073 * Licensed under the Apache License, Version 2.0 (the "License");
74 * you may not use this file except in compliance with the License.
75 * You may obtain a copy of the License at
Chia-I Wufb2559d2014-08-01 11:19:52 +080076 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060077 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wufb2559d2014-08-01 11:19:52 +080078 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060079 * Unless required by applicable law or agreed to in writing, software
80 * distributed under the License is distributed on an "AS IS" BASIS,
81 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 * See the License for the specific language governing permissions and
83 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060084 *
85 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080086 */"""
87
88 def generate_header(self):
Chia-I Wu6bdf0192014-09-13 13:36:06 +080089 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080090
91 def generate_body(self):
92 pass
93
94 def generate_footer(self):
95 pass
96
Chia-I Wu29271d72015-01-04 10:19:50 +080097class DispatchTableOpsSubcommand(Subcommand):
98 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -070099 if len(self.argv) < 1:
Chia-I Wu29271d72015-01-04 10:19:50 +0800100 print("DispatchTableOpsSubcommand: <prefix> unspecified")
101 return
102
103 self.prefix = self.argv[0]
Jamie Madilldbda66b2016-05-10 07:36:20 -0700104
105 if len(self.argv) > 2:
106 print("DispatchTableOpsSubcommand: <prefix> [outfile]")
107 return
108
109 if len(self.argv) == 2:
110 self.outfile = self.argv[1]
111
Michael Lentine695f2c22015-09-09 12:39:13 -0700112 super(DispatchTableOpsSubcommand, self).run()
Chia-I Wu29271d72015-01-04 10:19:50 +0800113
114 def generate_header(self):
David Pinedo9316d3b2015-11-06 12:54:48 -0700115 return "\n".join(["#include <vulkan/vulkan.h>",
116 "#include <vulkan/vk_layer.h>",
Jon Ashburn1dd0a5c2015-05-04 09:16:41 -0600117 "#include <string.h>"])
Chia-I Wu29271d72015-01-04 10:19:50 +0800118
Jon Ashburn8fd08252015-05-28 16:25:02 -0600119 def _generate_init_dispatch(self, type):
Chia-I Wu29271d72015-01-04 10:19:50 +0800120 stmts = []
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600121 func = []
122 if type == "device":
Jon Ashburn8fd08252015-05-28 16:25:02 -0600123 # GPA has to be first one and uses wrapped object
Courtney Goeltzenleuchter47fbcf32015-06-26 15:05:29 -0600124 stmts.append("memset(table, 0, sizeof(*table));")
Jon Ashburn8fd08252015-05-28 16:25:02 -0600125 stmts.append("table->GetDeviceProcAddr =(PFN_vkGetDeviceProcAddr) gpa(device,\"vkGetDeviceProcAddr\");")
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600126 for proto in self.protos:
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700127 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 Ashburn95a77ba2015-05-15 15:09:35 -0600128 continue
Jon Ashburn8acd2332015-09-16 18:08:32 -0600129 if proto.name != "GetDeviceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700130 stmts.append("table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
Chia-I Wu29271d72015-01-04 10:19:50 +0800131 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700132 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
Chia-I Wu29271d72015-01-04 10:19:50 +0800133 % self.prefix)
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700134 func.append("%s VkLayerDispatchTable *table,"
135 % (" " * len(self.prefix)))
136 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600137 % (" " * len(self.prefix)))
138 else:
Jon Ashburn8fd08252015-05-28 16:25:02 -0600139 stmts.append("table->GetInstanceProcAddr =(PFN_vkGetInstanceProcAddr) gpa(instance,\"vkGetInstanceProcAddr\");")
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600140 for proto in self.protos:
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700141 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice":
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600142 continue
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600143 if proto.name == "CreateDevice":
144 continue
Jon Ashburn8acd2332015-09-16 18:08:32 -0600145 if proto.name != "GetInstanceProcAddr" and 'KHR' not in proto.name:
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700146 stmts.append("table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600147 (proto.name, proto.name, proto.name))
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700148 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
149 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
150 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
151 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
Chia-I Wu29271d72015-01-04 10:19:50 +0800152 func.append("{")
153 func.append(" %s" % "\n ".join(stmts))
154 func.append("}")
155
156 return "\n".join(func)
157
Chia-I Wu29271d72015-01-04 10:19:50 +0800158 def generate_body(self):
Jon Ashburn8fd08252015-05-28 16:25:02 -0600159 body = [self._generate_init_dispatch("device"),
160 self._generate_init_dispatch("instance")]
Chia-I Wu29271d72015-01-04 10:19:50 +0800161
162 return "\n\n".join(body)
163
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800164class WinDefFileSubcommand(Subcommand):
165 def run(self):
166 library_exports = {
167 "all": [],
168 "icd": [
Jon Ashburnf5e97542016-01-07 09:46:26 -0700169 "vk_icdGetInstanceProcAddr",
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800170 ],
171 "layer": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700172 "vkGetInstanceProcAddr",
173 "vkGetDeviceProcAddr",
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600174 "vkEnumerateInstanceLayerProperties",
175 "vkEnumerateInstanceExtensionProperties"
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800176 ],
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700177 "layer_multi": [
Jon Ashburn2919a012015-08-13 14:15:07 -0700178 "multi2GetInstanceProcAddr",
179 "multi1GetDeviceProcAddr"
180 ]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800181 }
182
Jamie Madilldbda66b2016-05-10 07:36:20 -0700183 if len(self.argv) < 2 or len(self.argv) > 3 or self.argv[1] not in library_exports:
184 print("WinDefFileSubcommand: <library-name> {%s} [outfile]" %
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800185 "|".join(library_exports.keys()))
186 return
187
188 self.library = self.argv[0]
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700189 if self.library == "VkLayer_multi":
190 self.exports = library_exports["layer_multi"]
Jon Ashburn2919a012015-08-13 14:15:07 -0700191 else:
192 self.exports = library_exports[self.argv[1]]
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800193
Jamie Madilldbda66b2016-05-10 07:36:20 -0700194 if len(self.argv) == 3:
195 self.outfile = self.argv[2]
196
Jamie Madillc0018852016-04-04 11:20:24 -0400197 super(WinDefFileSubcommand, self).run()
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800198
199 def generate_copyright(self):
200 return """; THIS FILE IS GENERATED. DO NOT EDIT.
201
202;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600203; Vulkan
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800204;
Karl Schultz8e42f402016-02-02 19:32:33 -0700205; Copyright (c) 2015-2016 The Khronos Group Inc.
206; Copyright (c) 2015-2016 Valve Corporation
207; Copyright (c) 2015-2016 LunarG, Inc.
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800208;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600209; Licensed under the Apache License, Version 2.0 (the "License");
210; you may not use this file except in compliance with the License.
211; You may obtain a copy of the License at
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800212;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600213; http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800214;
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600215; Unless required by applicable law or agreed to in writing, software
216; distributed under the License is distributed on an "AS IS" BASIS,
217; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
218; See the License for the specific language governing permissions and
219; limitations under the License.
Courtney Goeltzenleuchterbdde0b22015-12-16 14:57:27 -0700220;
221; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800222;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
223
224 def generate_header(self):
225 return "; The following is required on Windows, for exporting symbols from the DLL"
226
227 def generate_body(self):
228 body = []
229
230 body.append("LIBRARY " + self.library)
231 body.append("EXPORTS")
232
Jon Ashburn2919a012015-08-13 14:15:07 -0700233 for proto in self.exports:
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700234 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
Ian Elliott0b4d6242015-09-22 10:51:24 -0600235 body.append( proto)
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800236
237 return "\n".join(body)
238
Chia-I Wufb2559d2014-08-01 11:19:52 +0800239def main():
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900240 wsi = {
241 "Win32",
242 "Android",
243 "Xcb",
244 "Xlib",
245 "Wayland",
Petros Bantolas2b40be72016-04-15 11:02:59 +0100246 "Mir",
247 "Display"
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900248 }
Chia-I Wufb2559d2014-08-01 11:19:52 +0800249 subcommands = {
Chia-I Wu29271d72015-01-04 10:19:50 +0800250 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu0a6644b2015-04-11 10:56:50 +0800251 "win-def-file": WinDefFileSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800252 }
253
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900254 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
255 print("Usage: %s <wsi> <subcommand> [options]" % sys.argv[0])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800256 print
257 print("Available sucommands are: %s" % " ".join(subcommands))
258 exit(1)
259
Mun, Gwan-gyeong82a244a2016-02-22 20:23:59 +0900260 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Chia-I Wufb2559d2014-08-01 11:19:52 +0800261 subcmd.run()
262
263if __name__ == "__main__":
264 main()